【发布时间】:2021-01-24 09:35:02
【问题描述】:
我已经尝试了各种方法来为 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))
【问题讨论】:
标签: python tensorflow lstm recurrent-neural-network