【问题标题】:keras lstm error: expected to see 1 arraykeras lstm 错误:预计会看到 1 个数组
【发布时间】:2020-05-20 02:05:23
【问题描述】:

所以我想创建一个 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)

【问题讨论】:

    标签: python arrays machine-learning keras lstm


    【解决方案1】:

    您需要更改LSTM 层的input_shape 值。此外,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) )))
    

    【讨论】:

    • 谢谢,但现在我收到了这个错误:ValueError: Error when checks input: expected lstm_1_input to have 3 dimensions, but got array with shape (7352, 557)
    猜你喜欢
    • 2017-07-24
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    相关资源
    最近更新 更多