【问题标题】:input_shape in LSTMLSTM 中的 input_shape
【发布时间】:2020-01-22 06:11:27
【问题描述】:

我有以下代码sn-p:

    X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
    X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))


    model = Sequential()
    model.add(LSTM(200, activation='relu', input_shape=(X_train.shape[0], 1, X_train.shape[2]), return_sequences=False))
    model.compile(optimizer='adam', loss='mean_squared_error')
    model.fit(X_train, y_train, epochs=100, batch_size=32)
    y_pred = model.predict(X_test)

但是,我收到以下错误:

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4

X_train 和 X_test 的原始形状:

X_train: 1483, 13
X_test: 360, 13

在重塑之后它们变成:

X_train: 1483, 1, 13
X_test: 360, 1, 13

我知道这可能是重复的,但网上的答案似乎都不适合我。

【问题讨论】:

    标签: keras neural-network time-series lstm


    【解决方案1】:

    input_shape=(X_train.shape[0], 1, X_train.shape[2]) 是错误的。

    LSTM 应具有 2D 输入形状(这意味着 3D 内部张量)。
    l - 输入形状必须包含(sequence_length, features_per_step)
    - 这意味着内部形状将是(free_batch_size, sequence_length, features_per_step)

    那么您的数据必须是 3D 的,可以,但 input_shape 应该是 2D。
    现在,sequence_length 对于循环层的工作是绝对必要的,如果你有sequence_length = 1,它就完全没用了,除非你要使用stateful=True,它涉及到更复杂的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-09
      • 2020-12-10
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 2016-07-04
      相关资源
      最近更新 更多