【问题标题】:Struggling to setup a basic LSTM based on numpy array input努力设置基于 numpy 数组输入的基本 LSTM
【发布时间】:2019-10-23 17:11:45
【问题描述】:

我正在尝试设置一个 LSTM,以便为它提供我的 numpy 数组特征和标签。

这是我的第一次尝试:

nb_features =len(seq_cols)
print("initial shape:", X_train.shape)
print("nb features", nb_features)
# X_train = X_train.reshape(X_train.shape + (1,))

print("Seq length ", seq_length)
print('New shape ', X_train.shape)

model = Sequential()

model.add(LSTM(
         input_shape=(nb_features, 1),
         units=100,
         return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(units=1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, batch_size=200, verbose=1,
          callbacks = [EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto')])

这给了我输出

initial shape: (175850, 4)
nb features 4
Seq length  50
New shape  (175850, 4)

    ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-99-50959413cb62> in <module>()
      1 model.fit(X_train, y_train, epochs=10, batch_size=200, verbose=1,
----> 2           callbacks = [EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto')])

2 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    126                         ': expected ' + names[i] + ' to have ' +
    127                         str(len(shape)) + ' dimensions, but got array '
--> 128                         'with shape ' + str(data_shape))
    129                 if not check_batch_axis:
    130                     data_shape = data_shape[1:]

ValueError: Error when checking input: expected lstm_28_input to have 3 dimensions, but got array with shape (175850, 4)

所以我试图通过取消注释第 4 行来重塑

nb_features =len(seq_cols)
print("initial shape:", X_train.shape)
print("nb features", nb_features)
X_train = X_train.reshape(X_train.shape + (1,))

print("Seq length ", seq_length)
print('New shape ', X_train.shape)

model = Sequential()

model.add(LSTM(
         input_shape=(nb_features, 1),
         units=100,
         return_sequences=True))
model.add(Dropout(0.2))

model.add(Dense(units=1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, batch_size=200, verbose=1,
          callbacks = [EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto')])

这现在给了我尺寸错误的错误。

initial shape: (175850, 4)
nb features 4
Seq length  50
New shape  (175850, 4, 1)

ValueError: Error when checking input: expected lstm_28_input to have 3 dimensions, but got array with shape (175850, 4)

我想我只是想知道如何进行随机更改。 谁能告诉我我在拼图中缺少哪些部分?我是该领域的初学者,错误对我没有多大帮助。

P.S: X_train 是一个 numpy 数组

【问题讨论】:

    标签: python-3.x numpy keras deep-learning lstm


    【解决方案1】:

    Keras input_shape 忽略了第一个维度,因为它表示训练示例的数量,m。这是因为 Keras 能够处理任意数量的训练示例,它只关心实际的输入维度。

    例如,input_shape=(nb_features, 1)=(4,1) 表示期望输入为 (None, 4, 1),其中 none 是训练示例的数量。您还可以在编译后但在适合之前键入 model.summary() 来查看这一点。

    这是 3 维,因此“预期 lstm_28_input 有 3 维”错误。你正在喂它 (175850, 4) 这是一个二维数组。

    【讨论】:

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