Keras 中 Vanilla RNN 密集层的形状错误?

Shape error for Vanilla RNN's dense layer in Keras?

我正在尝试使用 Vanilla RNN(无 LSTM)进行时间序列预测。

Error when checking target: expected dense_2 to have shape (2,) but got array with shape (1,)

我从linkhere学习了LSTM时间预测。我使用自己的数据集对其进行了尝试和测试。现在我想使用 RNN 实现时间序列来学习自己(并比较 LSTM 和 Vanilaa RNN 之间的差异)。但是我遇到了上述错误。

通过网络研究,我发现问题在于选择正确的误差函数(我想)。但我不确定。以下是我的代码片段。

注意因为是时间序列预测 Y(t) = X(T-1)

#X_train.shape = (7141, 1)
#y_train.shape = (7141, 1)
model = Sequential()
model.add(Dense(5, activation='relu'))
model.add(Dense(2))
model.compile(loss='mean_absolute_error', optimizer = 'adam')
history = model.fit(X_train, y_train, epochs=10, batch_size=64, verbose=1, shuffle=False)

问题是,正如错误所述,您的输出具有形状 (2,),因为它是由 Dense(2) 层生成的。如果将其替换为 Dense(1),形状将匹配。

但是请注意,目前还没有任何循环,您只是将 y(t_i) 建模为 x(t_i) 的函数,即单个先前样本。