gpt4 book ai didi

python - 用于训练的 Keras 特征形状

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:57 26 4
gpt4 key购买 nike

我正在尝试使用 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 数组。

关于python - 用于训练的 Keras 特征形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39566123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com