【发布时间】:2019-05-28 19:18:21
【问题描述】:
我正在尝试使用 TensorFlow 后端在玩具数据上训练带有 keras 的 LSTM,但出现此错误:
ValueError: 检查目标时出错:预期 dense_39 有 2 个维度,但得到的数组形状为 (996, 1, 1)
调用model.fit时立即发生错误;似乎什么都没有运行。在我看来,Keras 正在检查尺寸,但忽略了这样一个事实,即它应该在每批输入中获取我的目标的批次。该错误显示了我的目标数组的完整维度,这对我来说意味着 Keras 从来没有将它分成多个批次,至少在检查维度时是这样。对于我的生活,我无法弄清楚为什么会这样或其他任何可能有帮助的东西。
我的网络定义在 cmets 中具有预期的层输出形状:
batch_shape = (8, 5, 1)
x_in = Input(batch_shape=batch_shape, name='input') # (8, 5, 1)
seq1 = LSTM(8, return_sequences=True, stateful=True)(x_in) # (8, 5, 8)
dense1 = TimeDistributed(Dense(8))(seq1) # (8, 5, 8)
seq2 = LSTM(8, return_sequences=False, stateful=True)(dense1) # (8, 8)
dense2 = Dense(8)(seq2) # (8, 8)
out = Dense(1)(dense2) # (8, 1)
model = Model(inputs=x_in, outputs=out)
optimizer = Nadam()
model.compile(optimizer=optimizer, loss='mean_squared_error')
model.summary()
模型摘要,形状符合预期:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input (InputLayer) (8, 5, 1) 0
_________________________________________________________________
lstm_28 (LSTM) (8, 5, 8) 320
_________________________________________________________________
time_distributed_18 (TimeDis (8, 5, 8) 72
_________________________________________________________________
lstm_29 (LSTM) (8, 8) 544
_________________________________________________________________
dense_38 (Dense) (8, 8) 72
_________________________________________________________________
dense_39 (Dense) (8, 1) 9
=================================================================
Total params: 1,017
Trainable params: 1,017
Non-trainable params: 0
_________________________________________________________________
我的玩具数据,其中目标只是从 100 到 0 的一条线,输入只是一个零数组。我想提前一步预测,所以我使用下面定义的rolling_window() 方法创建输入和目标的滚动窗口:
target = np.linspace(100, 0, num=1000)
target_rolling = rolling_window(target[4:], 1)[:, :, None]
target_rolling.shape # (996, 1, 1) <-- this seems to be the array that's causing the error
x_train = np.zeros((1000,))
x_train_rolling = rolling_window(x_train, 5)[:, :, None]
x_train_rolling.shape # (996, 5, 1)
rolling_window() 方法:
def rolling_window(arr, window):
shape = arr.shape[:-1] + (arr.shape[-1] - window + 1, window)
strides = arr.strides + (arr.strides[-1],)
return np.lib.stride_tricks.as_strided(arr, shape=shape, strides=strides)
还有我的训练循环:
reset_state = LambdaCallback(on_epoch_end=lambda _, _: model.reset_states())
callbacks = [reset_state]
history = model.fit(x_train_rolling, y_train_rolling,
batch_size=8,
epochs=100,
validation_split=0.,
callbacks=callbacks)
我试过了:
- 无状态 LSTM,但我确实需要有状态的最终应用程序。同样的错误。
-
return_sequence=True在第二个 LSTM 中,之后是Flatten层。同样的错误。 -
return_sequence=True没有Flatten层。这会产生不同的错误,因为它期望目标与输出具有相同的形状,此时是(batch_size, 5, 1)而不是(batch_size, 1, 1)。 - 一次在整个序列上运行相同的架构(批量大小为 1),没有滚动窗口。这行得通,但只是学会逼近我的目标的平均值,对我的目的没有用。
请注意,这些问题似乎都没有直接回答我的问题,尽管我真的对几个问题抱有希望:
- Error when checking target: expected time_distributed_5 to have 3 dimensions, but got array with shape (14724, 1)
- LSTM and CNN: ValueError: Error when checking target: expected time_distributed_1 to have 3 dimensions, but got array with shape (400, 256)
- ValueError: Error when checking target: expected lstm_27 to have 2 dimensions, but got array with shape (1, 11, 1)
- expected dense_218_input to have 2 dimensions, but got array with shape (512, 28, 28, 1)
- expected dense_1 to have 2 dimensions, but got array with shape (308, 1, 6)
【问题讨论】:
-
target_rolling = target_rolling.reshape(-1,1)
-
在 target_rolling.shape # (996, 1, 1)
-
看来已经做到了!谢谢@Tom!
标签: python tensorflow keras lstm