Home>

I tried to predict using the predict function using the NN model created by keras, but I got the following error.

ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 22 but received input with shape [None, 1]

I think this is because the model was trained with a multidimensional matrix of n rows x 22 columns, and this time it was an array of 1 row x 22 columns, but I couldn't think of a way to solve it.

Please let me know if there is any good solution.

train_input = [0.6571429 0. 0.11428571 0. 0.4857143 0.02857143
 0.01428571 0.01428571 0. 0. 0. 0.
 0.0. 0.01428571 0.08571429 0.15714286 0.01428571
 0.04285714 0.6857143 1.]
pred = model.predict (train_input)


What I tried

train = []
train = np.append (train, train_input)
pred = model.predict (train_input)
  • Answer # 1

    If you replace "tried" with the following so that the shape of the train is (1,22), it will work.
    For this kind of thing, it is good to try various functions of numpy's matrix stacking system while examining the shape. Surprisingly, np.empty can be used as the "initial value of stacking".

    train = np.empty ((0, 22))
    train = np.vstack ([train, train_input])
    pred = model.predict (train)

    If you just want to move, you can do the following.

    pred = model.predict (np.array ([train_input]))