mimi
mimi

Reputation: 57

Error when checking input: expected lstm_28_input to have shape (5739, 8) but got array with shape (1, 8)

i got keras dimension error

the input shape is like this

print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

result

(5739, 1, 8) (5739,) (1435, 1, 8) (1435,)

and model is belows

batch_size=128
epochs=20
from keras_self_attention import SeqSelfAttention
from keras.layers import Flatten
model = keras.models.Sequential()
model.add(keras.layers.LSTM(epochs, input_shape=(train_X.shape[0], train_X.shape[2]), return_sequences=True))
model.add(Flatten())
model.add(keras.layers.Dense(units=1))
model.compile(loss='mse', optimizer='adam')
model.summary()

result

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_33 (LSTM)               (None, 5739, 20)          2320      
_________________________________________________________________
seq_self_attention_35 (SeqSe (None, 5739, 20)          1345      
_________________________________________________________________
flatten_8 (Flatten)          (None, 114780)            0         
_________________________________________________________________
dense_33 (Dense)             (None, 1)                 114781    
=================================================================
Total params: 118,446
Trainable params: 118,446
Non-trainable params: 0
_________________________________________________________________

but i got error in fit step

history = model.fit(train_X, train_y, epochs=epochs, batch_size=batch_size, validation_data=(test_X, test_y), verbose=2, shuffle=False)

error

ValueError: Error when checking input: expected lstm_33_input to have shape (5739, 8) but got array with shape (1, 8)

but i print input shape which is (5739,8), and i can't understand where (1,8) is coming from. and how to fix it.

input_shape=(train_X.shape[0], train_X.shape[2])
print(input_shape)
(5739, 8)

is it problem of input shape in test_X, test_Y or train? and how should i fix it?

Upvotes: 1

Views: 454

Answers (1)

sdcbr
sdcbr

Reputation: 7129

An LSTM layer in Keras expects batches of data with shape (n_timesteps, n_features). You are constructing your layer with the wrong dimensions.

First, reshape your training data to the shape n_data_points, n_timesteps, n_features:

train_X_ = np.swapaxes(train_X, 1, 2)
train_X_.shape # now of shape (5739, 8, 1)

Then specify your model with the correct dimensions:

model = keras.models.Sequential()
# input shape for the LSTM layer will be (8,1). No need to specify the batch shape.
model.add(keras.layers.LSTM(20, input_shape=(train_X_.shape[1], train_X_.shape[2]), return_sequences=True)) 
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(1))

model.compile(optimizer='adam', loss='mse')

This will work properly:

model.fit(train_X_, train_y)

Upvotes: 2

Related Questions