首页 > 解决方案 > keras lstm 错误:预计会看到 1 个数组

问题描述

所以我想让一个 lstm 网络在我的数据上运行,但我收到了这条消息:

ValueError:检查输入时出错:预期 lstm_1_input 的形状为 (None, 1) 但得到的数组的形状为 (1, 557)

这是我的代码:

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)

标签: pythonarraysmachine-learningkeraslstm

解决方案


您需要更改图层的input_shape值。LSTM另外,x_train必须具备以下shape.

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

所以,改变

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]) )))

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

推荐阅读