【问题标题】:ValueError: Shapes (None, 1) and (None, 10) are incompatible - When training CNN with Keras using image_dataset_from_directoryValueError: Shapes (None, 1) and (None, 10) is incompatible - 使用 image_dataset_from_directory 使用 Keras 训练 CNN 时
【发布时间】:2022-10-22 23:23:52
【问题描述】:

ValueError: Shapes... incompatible 在使用张量时是一个非常普遍的错误,但在我的情况下,它是在使用image_dataset_from_directory 方法使用 Keras 训练 CNN 时。

【问题讨论】:

  • 数据集中的图像是什么形状,你为 CNN 的 InputLayer 指定了什么形状?
  • @Mark Lavin我正在使用 tf.keras.utils.image_dataset_from_directory 的 image_size 参数来调整图像的大小, image_size=(150, 150) 和模型定义 keras.Input(shape=(150, 150, 3)

标签: keras conv-neural-network


【解决方案1】:

解决方案是在编译语句中将 'categorical_crossentropy' 替换为 'sparse_categorical_crossentropy'。

为什么?根据 image_dataset_from_directory 文档,“int”是默认的标签编码。因此,默认情况下,“image_dataset_from_directory”将为数据集创建一组标签作为整数,从 1 到数据集中的类数。

在这种情况下,应该使用“sparse_categorical_crossentropy”损失而不是“categorical_crossentropy”来编译模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 2020-11-24
    • 2021-08-18
    • 2021-07-07
    • 2022-11-18
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多