首页 > 解决方案 > flow_from_data_frame 在 keras 的多输出分类中给出了错误的标签形状

问题描述

我目前正在尝试在 keras 中训练一个多输出分类网络。.csv 有 4 个类别,它们都是来自 {0, 1, 2, 3, 4} 的类。我的网络有一个 softmax 密集层作为其输出层,但尝试训练给我以下错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError:  logits and labels must have the same first dimension, got logits shape [32,4] and labels shape [128]
     [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at /engagement_detector/cnn/train.py:58) ]] [Op:__inference_train_function_1752]

由于我的批量大小在训练和生成器中都是 32,所以我原以为两者都是形状 [32, 4]。这是我用于生成器的代码:

    datagen = datagen.flow_from_dataframe(
    dataframe=df,
    directory=subdir,
    x_col="ClipID",
    y_col=list(df.columns.drop("ClipID")),
    subset=subset.lower(),
    batch_size=32,
    seed=42,
    shuffle=True,
    class_mode="raw",
    target_size=(32, 32))

我做错了什么还是有人可以帮助我调整生成器输出的大小?我尝试添加一个重塑图层,但无法让它工作。

标签: pythonpandaskeraspreprocessor

解决方案


推荐阅读