Minh-Long Luu
Minh-Long Luu

Reputation: 2731

Keras: ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected ndim=3, found ndim=2

I have a dataset that has shape X: (1146165, 19, 22) and Y: (1146165,). This is my model code:

import tensorflow as tf

train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
valid_data = tf.data.Dataset.from_tensor_slices((x_valid, y_valid))

def create_model(shape=(19, 22)):
    tfkl = tf.keras.layers
    model = tf.keras.Sequential([
        tfkl.LSTM(128, return_sequences=True, input_shape=shape),
        tfkl.LSTM(64),
        tfkl.Dropout(0.3),
        tfkl.Dense(64, activation="linear"),
        tfkl.Dense(1)
    ])
    
    model.compile(loss='mean_absolute_error', optimizer="adam")
    return model

model = create_model()
model.summary()

As you can see the input_shape is (19, 22), which is right, but when I use fit I get the error ValueError: Input 0 of layer sequential_15 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [19, 22]
I search some answers on Stack, but most of them is because the input dimension is (a, b) instead of (a,b,c). Any help is appreciated.

Upvotes: 1

Views: 1288

Answers (1)

Alperino
Alperino

Reputation: 558

If you want to fit your model with a tf.data.Dataset, you'll need to make sure it is batched before using it in model.fit. For a batch_size of your choice, try

train_data = train_data.batch(batch_size)
model.fit(train_data)

Upvotes: 2

Related Questions