InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [1,10] and labels shape [40]

InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [1,10] and labels shape [40]

我正在使用 Keras-Tuner 自动识别我的 CNN 的最佳参数。我正在使用 Celeb_a 数据集。

你可以找到我正在使用的代码here 当我尝试 运行 时,出现以下错误。

InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [1,10] and labels shape [40] [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at C:\Users\admin-sepr\anaconda3\envs\tf\lib\site-packages\kerastuner\engine\tuner.py:141) ]] [Op:__inference_train_function_953]

我在其他地方看到这可能是因为我在编译代码时使用了 loss="categorical_crossentropy",,他们建议尝试使用 loss="sparse_categorical_crossentropy",

执行此操作时出现以下错误。

InvalidArgumentError: logits and labels must be broadcastable: logits_size=[64,380192] labels_size=[64,40] [[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at C:\Users\admin-sepr\anaconda3\envs\tf\lib\site-packages\kerastuner\engine\tuner.py:141) ]] [Op:__inference_train_function_6830]

两个错误的函数调用堆栈如下。

Function call stack:
train_function

我的函数 train_function 如下(上面提供了完整代码):

train_generator = train_datagen.flow_from_dataframe(
                                        dataframe=train_split,
                                        directory=celeba.images_folder,
                                        x_col='image_id',
                                        y_col=celeba.features_name,
                                        target_size=TARGET_SIZE,
                                        batch_size=64,
                                        class_mode='raw',
                                        dtype=tf.float32)

我尝试了 here 中推荐的方法,但没有成功。

您从 Celeb_A 数据集加载的数据似乎与您尝试训练的模型不兼容。从扫描代码来看,您似乎需要更改输出密集层中的节点数。这个数字(你目前有 10 个节点)应该与你期望预测的标签数量相匹配。从错误来看,您似乎预测了 40 个标签,但模型认为只有 10 个。

我最好的猜测是将损失保持为分类交叉熵,并将输出密集层更改为 Dense(40) 而不是 Dense(10)。