我目前正在 Google/Udacity 的 Tensorflow 課程中嘗試一個項目,使用獲取的數據集如下:_URL = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"zip_file = tf.keras.utils.get_file(origin=_URL, fname="flower_photos.tgz", extract=True)不幸的是,我遇到了以下錯誤:InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [100,5] and labels shape [500] [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at <ipython-input-43-02964d57939c>:8) ]] [Op:__inference_test_function_3591]我看了其他帖子,但似乎還是有點難以弄清楚。我最初的想法是我可能使用了錯誤的損失函數。這是遇到問題的代碼:image_gen = ImageDataGenerator(rescale = 1./255, horizontal_flip=True, zoom_range=0.5, rotation_range=45, width_shift_range=0.15, height_shift_range=0.15)train_data_gen = image_gen.flow_from_directory(batch_size=BATCH_SIZE, directory = train_dir, shuffle=True, target_size=(IMG_SHAPE,IMG_SHAPE),class_mode='binary')image_gen = ImageDataGenerator(rescale = 1./255)val_data_gen = image_gen.flow_from_directory(batch_size=BATCH_SIZE, directory = val_dir, shuffle=True, target_size=(IMG_SHAPE,IMG_SHAPE))批量大小為 100,輸入維度為 150,150 摘要如下: 模型:“sequential_4”層(類型)輸出形狀參數#conv2d_12(Conv2D)(無、148、148、16)448max_pooling2d_12(最大池化(無、74、74、16)0conv2d_13(Conv2D)(無、72、72、32)4640max_pooling2d_13(最大池化(無、36、36、32)0conv2d_14(Conv2D)(無、34、34、64)18496max_pooling2d_14(最大池化(無、17、17、64)0dropout_4(輟學)(無、17、17、64)0flatten_4(壓平)(無,18496)0密集_8(密集)(無,512)9470464密集_9(密集)(無,5)2565總參數:9,496,613 可訓練參數:9,496,613 不可訓練參數:0對可能出什么問題有什么想法嗎?
2 回答

皈依舞
TA貢獻1851條經驗 獲得超3個贊
注意生成器中的 class_mode
'int':表示標簽被編碼為整數(例如對于稀疏分類交叉熵損失)。“分類”意味著標簽被編碼為分類向量(例如,對于 categorical_crossentropy 損失)。'binary' 意味著標簽(只能有 2 個)被編碼為值為 0 或 1 的 float32 標量(例如,對于 binary_crossentropy)。無(無標簽)。
看來你需要“int”而不是“binary”來用于訓練和驗證生成器

眼眸繁星
TA貢獻1873條經驗 獲得超9個贊
在生成器中,我將 class_mode 更新為“稀疏”,并且工作正常。
train_data_gen = image_gen.flow_from_directory(train_dir, target_size = (IMG_SHAPE, IMG_SHAPE), batch_size = batch_size, class_mode = 'sparse')
添加回答
舉報
0/150
提交
取消