ValueError:形状不匹配:标签的形状(接收到(15,))应等于对数的形状,但最后一个维度(接收到(5,3))

时间:2019-10-15 15:50:26

标签: python tensorflow keras

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

  

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

产生错误的代码:

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

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

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)

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

这里是模型,以防万一:

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'])

谢谢!

编辑-完整代码

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

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)

谢谢!

2 个答案:

答案 0 :(得分:1)

for (int i = 0; i < N; i++) { maxError = fmax(maxError, fabs(d_y[i]-3.0f)); ^^^^^^ } sparse_categorical_crossentropy之间的区别在于您的目标是否为一键编码。

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

categorical_crossentropy

答案 1 :(得分:0)

我认为这与您的损失功能有关 只需尝试使用“ categorical_crossentropy”而不是“ sparse ...”