python - 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)
解决方案
您需要更改图层的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) )))
推荐阅读
- javascript - MPAndroidChart Intent 传递数据错误空对象引用
- python - 获取英国财政年度的一周
- c++ - 使用 cmake 所需的 Boost 组件
- vuejs2 - Vuex 分离文件 getters.js 和带参数的 getter
- fortran - Fortran DATA 语句中的字符串连接
- html - CSS 搜索按钮样式被用户代理样式表覆盖
- bash - Bash试图将转换后文件的名称写入文本
- python - 为什么 QWidget 不显示在 QMainWindow 上?
- spring - Spring Controller 从不同的网站获取变量
- r - 如果我的操作按钮导致应用程序崩溃,我如何在 Shiny 中返回错误