【发布时间】:2021-06-16 12:28:13
【问题描述】:
我目前正在研究 LSTM 模型,以根据其他数据预测股票的收盘价。这是我第一次使用 RNN。我正在使用张量流。
当我尝试通过 X 训练数据(这是模型的训练数据)预测价格时,问题就出现了。与 y 训练数据相比,我得到不同的维度。
我正在使用 7 个时间步长为 100 的特征来预测收盘价。
这些是我输入数据的形状:
x_train = (3697, 100, 7)
y_train = (3697, 1)
x_test = (1584, 100, 7)
y_test = (1584, 1)
输入数据的形状对我来说似乎是正确的。我将 (100, 7) 的输入形状传递给模型
然后我运行:
predicted_stock_price_train = model.predict(x_train)
predicted_stock_price_train.shape
我得到的输出是 (3697, 100, 1)。我期待 (3697, 1) 这是 y_train 的尺寸。
结果,在执行 inverse_transform 时,我得到了错误:
ValueError: Found array with dim 3. Estimator expected <= 2.
因为 fit_transform 是在 y_train 上传递的。
我不明白我做错了什么。
编辑: 这是 model.summary() 的输出
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_15 (LSTM) (None, 100, 50) 11600
_________________________________________________________________
dropout_6 (Dropout) (None, 100, 50) 0
_________________________________________________________________
lstm_16 (LSTM) (None, 100, 50) 20200
_________________________________________________________________
dropout_7 (Dropout) (None, 100, 50) 0
_________________________________________________________________
dense_6 (Dense) (None, 100, 1) 51
=================================================================
Total params: 31,851
Trainable params: 31,851
Non-trainable params: 0
_________________________________________________________________
【问题讨论】:
-
最好将
model.summary()的输出附加到您的问题中。 -
添加 flatten 层后,您应该会看到模型摘要中的最后一个条目是 (None, 1)
-
@Kaveh 嘿,我按照你的要求做了,并添加了一个 model.add(Flatten())。我得到 ValueError: Input 0 of layer lstm_31 is in compatible with the layer: expected ndim=3, found ndim=2.收到的完整形状:(无,700)。
标签: python tensorflow deep-learning lstm