ValueError:形状不匹配:标签的形状(接收到(15,))应等于对数的形状,但最后一个尺寸(接收到(5,3))除外 [英] ValueError: Shape mismatch: The shape of labels (received (15,)) should equal the shape of logits except for the last dimension (received (5, 3))

查看:58
本文介绍了ValueError:形状不匹配:标签的形状(接收到(15,))应等于对数的形状,但最后一个尺寸(接收到(5,3))除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试拟合模型时出现此错误:

I am getting this error when trying to fit a model:

ValueError:形状不匹配:标签的形状(已收到(15,))除最后一个尺寸外,应等于对数的形状(收到(5,3)).

ValueError: Shape mismatch: The shape of labels (received (15,)) should equal the shape of logits except for the last dimension (received (5, 3)).

产生错误的代码:

history = model.fit_generator(
  train_generator,
  epochs=10,
  validation_data=validation_generator)

这是train_generator,验证生成器与此类似:

This is the train_generator, validation generator is similar:

train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(IMG_WIDTH, IMG_HEIGHT),
        batch_size=5)

我尝试得到形状:

for data_batch, labels_batch in train_generator:
    print('data batch shape:', data_batch.shape)
    print('labels batch shape:', labels_batch.shape)
    break

数据批处理形状:(5,192,192,3)标签批处理形状:(5,3)

data batch shape: (5, 192, 192, 3) labels batch shape: (5, 3)

当我更改批次大小时,错误中标签的形状也会相应更改(例如,批次大小3会导致标签形状(9,)出现错误,例如,我有3个类).但是我担心的是这是由train_generator完成的,我能做些什么来解决这个问题?而且,当我从train_generator打印形状时,这似乎是正确的.

When I change the batch size, the shape of labels in the error changes accordingly (batch size of 3 gives an error with label shape (9,) for example, I have 3 classes). But my concern is that it is being done by the train_generator, is there anything I can do to fix this? Moreover, when I print the shapes from the train_generator, it seems right.

这里是模型,以防万一:

Here is the model, in case it is helpful:

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
                        input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
for i in range(2):
  model.add(layers.Conv2D(128, (3, 3), activation='relu'))
  model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Flatten())
model.add(layers.Dense(3, activation='softmax'))


model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])

谢谢!

编辑-完整代码:

该目录包含两个文件夹-训练和验证,每个文件夹都有三个带有相应类图像的子文件夹.

The directory contains two folders - train and validation and each of them has three subfolders with the images of the corresponding classes.

try:
 %tensorflow_version 2.x # enable TF 2.x in Colab
except Exception:
  pass

from tensorflow.keras import datasets, layers, models

IMG_WIDTH = 192
IMG_HEIGHT = 192
train_dir = 'train'
validation_dir = 'validation'

from google.colab import drive
drive.mount('/content/drive')

import os
os.chdir("drive/My Drive/colab")

from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(IMG_WIDTH, IMG_HEIGHT),
        batch_size=5)

validation_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = validation_datagen.flow_from_directory(
        validation_dir,
        target_size=(IMG_WIDTH, IMG_HEIGHT),
        batch_size=5)

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
                        input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
for i in range(2):
  model.add(layers.Conv2D(128, (3, 3), activation='relu'))
  model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Flatten())
model.add(layers.Dense(3, activation='softmax'))

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit_generator(
      train_generator,
      epochs=10,
      validation_data=validation_generator)

谢谢!

推荐答案

sparse_categorical_crossentropy categorical_crossentropy 之间的区别在于您的目标是否为一键编码.

The difference between sparse_categorical_crossentropy and categorical_crossentropy is whether your targets are one-hot encoded.

标签批的形状为(5,3),这意味着它已被一次性编码.因此,您应该使用 categorical_crossentropy 损失函数.

The shape of label batch is (5,3), which means it has been one-hot encoded. So you should use categorical_crossentropy loss function.

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

这篇关于ValueError:形状不匹配:标签的形状(接收到(15,))应等于对数的形状,但最后一个尺寸(接收到(5,3))除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆