我正在尝试使用 keras train_on_batch 函数训练 nn。我有 39 个特征,并希望一个批处理包含 32 个样本。所以对于每次训练迭代,我都有一个包含 32 个 numpy 数组的列表。
所以这是我的代码(这里每个 batch_x 都是 32 个 numpy 数组的列表,每个数组包含 39 个特征):
input_shape = (39,)
model = Sequential()
model.add(Dense(39, input_shape=input_shape)) # show you is only first layer
...
for batch_x, batch_y in train_gen:
model.train_on_batch(batch_x, batch_y)
但是突然报错了:
Exception: Error when checking model input: the list of Numpy arrays
that you are passing to your model is not the size the model expected.
Expected to see 1 arrays but instead got the following list of 32 arrays:
我不太确定哪里出了问题。
附言我还尝试了不同的 input_shape,例如 (32, 39)、(39, 32) 等。
请您参考如下方法:
您不需要 32 个大小为 39 的数组,您需要一个大小为 (32, 39) 的数组。
因此您必须将 input_shape 更改为 (None, 39),None 允许您动态更改 batch_size,并将 batch_x 更改为形状为 (32, 39) 的 numpy 数组。
