我正在尝试预测下一天的开盘价。我能够正确地格式化输入数据,即每天(n个时间段)的“开盘价”和“最高价”列。然而,当我将数据格式化为三维数组时,我的形状如下:
(1200, 60, 2)
X_train有1200个样本,每个样本包含60个时间步(过去60天的历史数据)和2个特征(开盘价和最高价)。
然而,当我开始编写Keras代码并实现层时,问题出现了。这是我使用的代码:
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 2)))regressor.add(Dropout(0.2))regressor.add(LSTM(units = 50, return_sequences = True))regressor.add(Dropout(0.2))regressor.add(LSTM(units = 50, return_sequences = True))regressor.add(Dropout(0.2))regressor.add(LSTM(units = 50))regressor.add(Dropout(0.2))regressor.add(Dense(units = 1))
问题出现在最后一行。我希望输出只有一个值。也就是说,我希望使用输入序列的开盘价和最高价来计算最终的单一输出,即开盘价。然而,通过设置Dense(units = 1)
,出现了以下错误:
ValueError: Error when checking target: expected dense_1 to have shape (1,) but got array with shape (2,)
为了解决这个问题,我尝试将其改为2,即Dense(units=2)
,然而,最终输出在图表上显示两条线,一条是开盘价,一条是最高价,这不是我想要的。我想要的是一个输出,而不是两个。我不确定在这种情况下该怎么做。
regressor.summary()
Model: "sequential_1"_________________________________________________________________Layer (type) Output Shape Param # =================================================================lstm_1 (LSTM) (None, 60, 50) 10600 _________________________________________________________________dropout_1 (Dropout) (None, 60, 50) 0 _________________________________________________________________lstm_2 (LSTM) (None, 60, 50) 20200 _________________________________________________________________dropout_2 (Dropout) (None, 60, 50) 0 _________________________________________________________________lstm_3 (LSTM) (None, 60, 50) 20200 _________________________________________________________________dropout_3 (Dropout) (None, 60, 50) 0 _________________________________________________________________lstm_4 (LSTM) (None, 50) 20200 _________________________________________________________________dropout_4 (Dropout) (None, 50) 0 _________________________________________________________________dense_1 (Dense) (None, 2) 102 =================================================================Total params: 71,302Trainable params: 71,302Non-trainable params: 0
回答:
当密集层出现形状错误时,可能是你的标签张量不匹配。请检查y_train
的形状是否为[1200, 1],这样你就可以使用一个单位为1的密集层了。