Search code examples
tensorflowkerastime-serieslstmprediction

Failed to convert a NumPy array to a Tensor (Unsupported object type float). Datetime and time-series


I've trained my tf model normally (Bidirectional LSTM). But when I try to predict on X_train, it gives an error. What can I do to fix it. Here's the X_train and the error:

array([[['2021-12-01 22:00:00', 0.3333333333333333, 0.39166666666666644,
     0.7701149425287357],
    ['2021-12-01 21:30:00', 0.3518518518518518, 0.37500000000000067,
     0.6551724137931034],
    ['2021-12-01 21:00:00', 0.3518518518518518, 0.37500000000000067,
     0.6551724137931034],

Here's the error:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).

Solution

  • Date format cannot be directly convert to Tensor. Instead use tfa.text.parse_time library to parse date and time.

    import tensorflow as tf
    import tensorflow_addons as tfa
    tfa.text.parse_time(
        time_string: str,
        time_format: str,
        output_unit: str
    )
    

    For more details about library find here.

    Once the date and time is parsed, pass this object with other python objects to convert it to Tensor.