将输入馈入keras顺序模型

时间:2019-03-04 05:25:06

标签: python tensorflow keras

我正在与Keras进行时间序列预测。 我的输入数据中有10个时间步,它们的形状为(2688, 10, 1),即train_x.shape和train_y.shape为(2688,10,1)。 尝试将其输入模型时出现以下错误。

ValueError: Error when checking target: expected activation_1 to have 2 dimensions, but got array with shape (2688, 10, 1)

我给第一个lstm层输入的形状:input_shape=(1, time_steps) **

  

我可以正确地重塑train_y吗?

**

    time_steps=10
    train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
    train_y = np.reshape(train_y, (train_y.shape[0], train_y.shape[1], 1))

    # lstm model
    model = Sequential()
    model.add(LSTM(128, input_shape=(time_steps, 1), return_sequences=True))
    model.add(LSTM(64))
    model.add(Dense(1))
    model.add(Activation('linear'))

    model.compile(loss='mse', optimizer='adam')
    history = model.fit(train_x, train_y, epochs=10, validation_data=(test_x, 
    test_y), batch_size=64, verbose=1)

2 个答案:

答案 0 :(得分:0)

如果您期望形状(2688、10、1),则不能输入_shape =(1,time_steps)。它应该input_shape =(time_steps,1)

答案 1 :(得分:0)

train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
train_y = np.reshape(train_y, (train_y.shape[0], train_y.shape[1], 1)

我认为错误在这里,您应该给定shape [0]和shape [1]之间的时间步长,即...

train_x = np.reshape(train_x, (train_x.shape[0], 10,train_x.shape[1]))
train_y = np.reshape(train_y, (train_y.shape[0],10, train_y.shape[1]))

这里的值“ 10”表示时间步长!