简体   繁体   中英

Dimension error when using model.predict in Keras

My train set has 10 columns including a target column that I'm trying to predict, while my test set ( dataframe_test ) has 9 columns. When I run the code I receive this error:

Input 0 of layer "Hidden1" is incompatible with the layer: expected axis -1 of input shape to have value 10, but received input with shape (None, 9)

Call arguments received:
  • inputs=tf.Tensor(shape=(None, 9), dtype=float64)
  • training=False
  • mask=None**

My model looks like this:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(units=10,
                                activation='relu',
                                kernel_regularizer=tf.keras.regularizers.l2(l=0.01),
                                name='Hidden1'))
model.add(tf.keras.layers.Dense(units=6,
                                activation='relu',
                                kernel_regularizer=tf.keras.regularizers.l2(l=0.01),
                                name='Hidden2'))

model.add(tf.keras.layers.Dense(units=1,
                                name='Output'))
my_learning_rate = 0.3    
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=my_learning_rate),
              loss="categorical_crossentropy",
              metrics='accuracy')

epochs = 10
batch_size = 32
history = model.fit(train, y_train, epochs = epochs, batch_size = batch_size)
epochs = history.epoch
print(epochs)
score = model.predict(dataframe_test)

Try using sigmoid

  input_size=len(X.columns)
  model.add(Dense(10,activation='sigmoid', input_shape=(input_size,)))
  model.add(Dense(10,activation='relu'))
  model.add(Dense(10,activation='relu'))
  model.add(Dense(1))

You must to split your train set in a 9 columns input matrix x_train = train[:, :10] and a single column training target matrix y_train = train[:, 10].reshape((-1, 1)) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM