0

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

您可以在此处找到我正在使用的代码 当我尝试运行它时,我收到以下错误。

InvalidArgumentError:logits 和标签必须具有相同的第一维,得到 logits 形状 [1,10] 和标签形状 [40] [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits(定义在 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 和标签必须是可广播的:logits_size=[64,380192] labels_size=[64,40] [[node categorical_crossentropy/softmax_cross_entropy_with_logits(定义在 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)

我在这里尝试了推荐的方法,但没有成功。

4

1 回答 1

2

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

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

于 2021-04-14T19:30:11.317 回答