keras lstm error: expected to see 1 array

Tags: , , , ,



so i want to make a lstm network to run on my data but i get this message:

ValueError: Error when checking input: expected lstm_1_input to have shape (None, 1) but got array with shape (1, 557)

this is my code:

x_train=numpy.array(x_train)
x_test=numpy.array(x_test)
x_train = numpy.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
x_test = numpy.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(50, input_shape=(1,len(x_train[0]) )))
model.add(Dense(1))
model.add(Dropout(0.2))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, numpy.array(y_train), epochs=100, batch_size=1, verbose=2)

Answer

You need to change the input_shape value for LSTM layer. Also, x_train must have the following shape.

x_train = x_train.reshape(len(x_train), x_train.shape[1], 1)

So, change

x_train = numpy.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
model.add(LSTM(50, input_shape=(1,len(x_train[0]) )))

to

x_train = x_train.reshape(len(x_train), x_train.shape[1], 1)
model.add(LSTM(50, input_shape=(x_train.shape[1], 1) )))


Source: stackoverflow