【问题标题】:Python machine learning LSTM: cannot reshape input for LSTMPython机器学习LSTM:无法重塑LSTM的输入
【发布时间】:2020-02-21 01:58:44
【问题描述】:

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

Neural Network LSTM input shape from dataframe

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

我有 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)

【问题讨论】:

    标签: python tensorflow lstm


    【解决方案1】:

    LSTM 模型需要 [samples时间步数特征]形式的 3D 输入

    因此 X_train 应该有 3D 格式的数据。

    火车数据应该能够正确地重新塑造成这种 3D 格式。

    这可以通过一个简单的例子来理解。 例如。我们总共有 10 名学生,每个学生都有 4 个不同科目(特征/列)的 5 周在线学习互动记录。

    这意味着数据框由 10*5 = 50 行和 4 列(主题)组成。 现在,这些数据可以很容易地重塑为 3D 格式

    X_train_shaped= np.reshape(X_train.values,(10,5,4))
    

    但是,有问题的代码无法将 48 的单个向量重塑为 (48,48,21),因为数据对于 3D 表示不准确。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-30
        • 2018-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-05
        • 2019-09-07
        • 1970-01-01
        相关资源
        最近更新 更多