Home > front end >  Input 0 of layer "lstm" is incompatible with the layer: expected shape=(1, None, 1), found
Input 0 of layer "lstm" is incompatible with the layer: expected shape=(1, None, 1), found

Time:12-16

This is the code in question:

batch_size = 1
epochs = 1
begin_from_row = 3
rows_to_train = 1000

data = data.loc[begin_from_row:rows_to_train, :]

data['Close_next'] = data['Close'].shift(-1)
data = data.dropna()
output_data = data['Close_next']
input_data = data.drop(columns=['Close_next'])
input_size = 9
output_size = 1
hidden_size_1 = 9

input_layer = tf.keras.Input(batch_shape=(batch_size, input_size))
input_layer_expanded = tf.expand_dims(input_layer, axis=-1)
hidden_1 = tf.keras.layers.LSTM(hidden_size_1, stateful=True)(input_layer_expanded)
output_layer = tf.keras.layers.Dense(1, activation='relu')(hidden_1)

model = tf.keras.Model(inputs=input_layer, outputs=output_layer)

model.compile(loss='mean_squared_error', optimizer='adam', run_eagerly=True)

model.fit(input_data, output_data, epochs=epochs)

model.save("model_1.h5")

It returns the following error:

Input 0 of layer "lstm" is incompatible with the layer: expected shape=(1, None, 1), found shape=(32, 9, 1)

I can’t quite get where it gets the number 32 from, since it soesn’™ appear anywhere in my code

The code works when I specify the batch_size=32, just for one batch. The number 32 doesn’t appear anywhere in the code, so I would like to know where it’s coming from.

CodePudding user response:

The vector (32, 9, 1) represents the size of your input data, whereas (1, None, 1) is the expected shape that you defined in the InputLayer (batch_size = 1).

Batch_size 32 is the default value in the fit method. The default value is being used since you did not specify the batch_size argument and you are not using tensorflow datasets:

batch_size: Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32. Do not specify the batch_size if your data is in the form of datasets, generators, or keras.utils.Sequence instances (since they generate batches).

From: Tensorflow fit function

  • Related