Home > Blockchain >  Error when trying to predict with new data in Keras model
Error when trying to predict with new data in Keras model

Time:10-18

I got a dataset composed of 432 batches of 24 points each one of them. Shape of the entire dataset: (432, 24)

To put an example, this would be one batch:

array([917,  15, 829,  87, 693,  71, 627, 359, 770, 303, 667, 367, 754,
       359, 532,  39, 683, 407, 333, 551, 516,  31, 675,  39])

with shape (24,)

I am feeding a Keras model with this info. No issues. When I try to predict with new data with the same shape (24,):

array([176,  71, 152,  63, 200,  71, 120,  87, 128,  87, 216, 103, 248,
       126, 144, 150, 128, 206, 192, 206, 112, 277, 216, 269])

My model:

  model = keras.Sequential([
        keras.layers.Flatten(batch_input_shape=(None,24)),
        keras.layers.Dense(64, activation=tf.nn.relu),

        keras.layers.Dense(2, activation=tf.nn.sigmoid),
    ])

  model.compile(optimizer='adam',
                loss=tf.losses.categorical_crossentropy,
                metrics=['accuracy'])

The error raised:

ValueError: Input 0 of layer dense_24 is incompatible with the layer: expected axis -1 of input shape to have value 24 but received input with shape (None, 1)

enter image description here

CodePudding user response:

Maybe try adding a dimension to your data sample and then feed your new_data into your model to make a prediction:

import numpy as np


new_data= np.array([176,  71, 152,  63, 200,  71, 120,  87, 128,  87, 216, 103, 248,
       126, 144, 150, 128, 206, 192, 206, 112, 277, 216, 269])

new_data= np.expand_dims(new_data, axis=0)

prediction = model.predict(new_data)
print(prediction)
  • Related