首页 > 解决方案 > Python机器学习LSTM:无法重塑LSTM的输入

问题描述

我正在努力使 LSTM 工作。我在 Stack Overflow 上发现了一个问题:

来自数据帧的神经网络 LSTM 输入形状

这似乎对我有帮助,但实际上我遇到了另一个问题:无法重塑数据,但我会按照“指令”执行所有步骤。

我有 48 行 × 22 列的数据集,其中第一列是日期。标签列与该数据集分离。所以我有 21 个预测特征。

但是当我这样做时

# Extract your training data
X_train_init = np.asarray(DF.padded_input_vectors)
print(X_train_init.shape)
# Use hstack to and reshape to make the inputs a 3d vector
X_train = np.hstack(X_train_init).reshape(len(DF),max_sequence_length,21)
y_train = np.hstack(np.asarray(train_target)).reshape(len(DF),1)

我得到:

(48,)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-ea37bd9226df> in <module>
      3 print(X_train_init.shape)
      4 # Use hstack to and reshape to make the inputs a 3d vector
----> 5 X_train = np.hstack(X_train_init).reshape(len(DF),max_sequence_length,21)
      6 y_train = np.hstack(np.asarray(train_target)).reshape(len(DF),1)

ValueError: cannot reshape array of size 48 into shape (48,48,21)

标签: pythontensorflowlstm

解决方案


的形状(48,)是大小为 48 的单个数组。对于 的形状(48, 48, 21),您需要 48,384 个元素,因此您不能将 48 个元素的大小调整为该形状。也许您需要以某种方式添加元素?无论如何,这就是重塑失败的原因。

编辑:检查您的导入,这似乎是问题所在。


推荐阅读