我正在尝试创建一个 keras LSTM 来预测时间序列。我的 x_train 形状像 3000,15,10 (示例、时间步长、特征),y_train 形状像 3000,15,1,我正在尝试构建一个多对多模型(每个序列 10 个输入特征产生 1 个输出/序列)。
我正在使用的代码是这样的:
model = Sequential()
model.add(LSTM(
10,
input_shape=(15, 10),
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
100,
return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))
model.compile(loss="mse", optimizer="rmsprop")
model.fit(
X_train, y_train,
batch_size=512, nb_epoch=1, validation_split=0.05)
但是,使用时我无法拟合模型:
model.add(Dense(1, activation='linear'))
>> Error when checking model target: expected dense_1 to have 2 dimensions, but got array with shape (3000, 15, 1)
或者以这种方式格式化时:
model.add(Dense(1))
model.add(Activation("linear"))
>> Error when checking model target: expected activation_1 to have 2 dimensions, but got array with shape (3000, 15, 1)
我已经尝试过压平模型(model.add(Flatten())
)在添加致密层之前但这只是给了我ValueError: Input 0 is incompatible with layer flatten_1: expected ndim >= 3, found ndim=2
。这让我很困惑,因为我认为我的数据实际上是 3 维的,不是吗?
代码源自https://github.com/Vict0rSch/deep_learning/tree/master/keras/recurrent https://github.com/Vict0rSch/deep_learning/tree/master/keras/recurrent