ValueError: Error when checking input: expected lstm_11_input to have shape (1, 1) but got array with shape (1, 3841)

ValueError: Error when checking input: expected lstm_11_input to have shape (1, 1) but got array with shape (1, 3841)

我尝试了多种方法来重塑 LTSM 神经网络的数据,但我仍然遇到以下错误:

ValueError: Error when checking input: expected lstm_29_input to have shape (1, 1) but got array with shape (1, 3841)

我原来的x_train数据是整形的:

array([[-1.86994147, -2.28655553, -2.13504696, ...,  2.38827443,
         1.29554594, 46.        ],
       [-1.99436975, -2.41145158, -1.93463695, ...,  2.57659554,
         1.27274537, 68.        ],
       [-2.19207883, -2.24740434, -1.73407733, ...,  2.66955543,
         1.80862379, 50.        ]

x_train 形状:(1125, 3841)

x_valid 形状:(375, 3841)

y_train 形状:(1125,)

所以我重塑了我的 x_train 和 x_valid 数据(用于预测)以便正确编译模型。我看过各种有类似错误的例子,他们按如下所示重塑它,他们通常让他们的模型正常工作,但就我而言,我不确定发生了什么。

# reshape input to be [samples, time steps, features]
trainX = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
validX = np.reshape(x_valid, (x_valid.shape[0], 1, x_valid.shape[1]))

模型构建

def cnn_model():
    
    """"
    Creates the model of the CNN.

    :param nr_measures: the number of output nodes needed
    :return: the model of the CNN
    """
    # Initializing the ANN
    model = Sequential()

    # Adding the input layer and the first hidden layer
    model.add(Conv1D(64, 2, activation="relu", input_shape=(x_train.shape[1], 1)))
    model.add(Flatten())
    model.add(BatchNormalization())

    # Adding the second hidden layer
    model.add(Dense(128, activation="relu"))
    model.add(BatchNormalization())

    # Adding the output layer
    model.add(Dense(1, activation = "softplus"))
    model.compile(loss="mse", optimizer="adam", metrics = ['mse'])

    return model

调用训练模型

#Call Model
cnn_model= cnn_model(trainX)

#Train Model
history = cnn_model.fit(trainX, y_train, batch_size = 50, epochs = 150, verbose = 0 ,validation_data = (validX, y_valid))

当您使用以下行

更改 trainX 的形状时
trainX = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))

变成了 1125, 1, 3841.

现在当你写下一行后

model.add(LSTM(units = 50, return_sequences = True, input_shape = (1, trainX.shape[1])))

模型预计 input_shape(batch_size, 1, 1)

你应该写

model.add(LSTM(units = 50, return_sequences = True, input_shape = (1, trainX.shape[2])))

现在模型将期望 input_shape(batch_size, 1, 3841),这将对应于您的新 tranX

附带说明,1 的序列 length/lookback/lag 时间和 3841 的输入特征,使用 LSTM 并没有真正意义,因为 LSTM 旨在提取时间特征和这里没有 temporal/historical 数据。在将输入重塑为 (batch_size, 3148, 1) 之前,可能 Conv1D 层会更相关。这只是一个建议。