Search code examples
python-3.xtensorflowkeraslstmrecurrent-neural-network

Keras model.predict - expected ndim=3, found ndim=2


I have used the linked code to train my model. RNN Prediction

Now I want to use my trained model to make a prediction by loading the model, which I do with predict.py

However I seem to get dim errors and I have tried everything and just about tearing my hair out. Is anyone able to help me solve this?

PS: You can download the model, test.csv & predict.py to reproduce & please include the code for correctly loading the pandas dataframe in predict.py to have answer marked as complete


Solution

  • You have to properly reshape the data before sending it to model.predict() .

    import numpy as np
    #dummy data
    df1 = np.array([[[1,23,7,9,61,5,5,6]]])
    print(df1.shape)  # gives (1, 1, 8)
    model = tf.keras.models.load_model('RNN_Final-01-0.534.model')
    prediction = model.predict(df1)
    print("---->",prediction)