gpt4 book ai didi

python - CNN 模型分类错误 : logits and labels must be broadcastable: logits_size=[32, 10] labels_size=[32,13]

转载 作者:行者123 更新时间:2023-12-04 09:10:54 26 4
gpt4 key购买 nike

在这里,我试图在图像分类上运行 CNN 模型。
这是批量大小和 13 个标签

Image batch shape:  (32, 32, 32, 3)
Label batch shape: (32, 13)
['Watch_Back' 'Watch_Chargers' 'Watch_Earpods' 'Watch_Front'
'Watch_Lifestyle' 'Watch_Others' 'Watch_Packages' 'Watch_Side'
'Watch_Text' 'Watch_Tilted' 'Watch_With_Accessories'
'Watch_With_Ear_Pods' 'Watch_With_People']
以下是cnn的模型
model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=(32,32,3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(500, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(250, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
从代码的以下部分,错误来了:
steps_per_epoch = np.ceil(train_generator.samples/train_generator.batch_size)
val_steps_per_epoch = np.ceil(valid_generator.samples/valid_generator.batch_size)
hist = model.fit(
train_generator,
epochs=10,
verbose=1,
steps_per_epoch=steps_per_epoch,
validation_data=valid_generator,
validation_steps=val_steps_per_epoch).history
以下是错误
Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-64-b89d5efc8aaf> in <module>()
7 steps_per_epoch=steps_per_epoch,
8 validation_data=valid_generator,
----> 9 validation_steps=val_steps_per_epoch).history

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:

InvalidArgumentError: logits and labels must be broadcastable: logits_size=[32,10] labels_size=[32,13]
[[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at <ipython-input-64-b89d5efc8aaf>:9) ]] [Op:__inference_train_function_6504]

Function call stack:
train_function
如何解决这个分类错误

最佳答案

错误是由这一行引起的:

model.add(Dense(10, activation='softmax'))
最后一层包含与数据集中的类别一样多的神经元,这一点很重要。我猜你有 13 个类别,所以它应该是 13 个。你也可以使用
model.add(Dense(len(train_generator.classes), activation='softmax'))

关于python - CNN 模型分类错误 : logits and labels must be broadcastable: logits_size=[32, 10] labels_size=[32,13],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63354021/

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