ValueError:logits和标签必须具有相同的形状

时间:2020-10-19 21:46:19

标签: keras neural-network classification

  • 我在 Keras 中有一个多层感知器网络,其中有两个隐藏层。

  • 在尝试训练网络时,我在fit_generator中遇到错误:

错误:

ValueError:登录件和标签的形状必须相同((无,2)与(无,1))

我的代码是:

import numpy as np
import keras
from keras import layers
from keras import Sequential
# Define Window size (color images)
img_window = (32,32,3)

# Flatten the Window shape
input_shape = np.prod(img_window)
print(input_shape)

# Define MLP with two hidden layers(neurons)
simpleMLP = Sequential(
    [layers.Input(shape=img_window),
     layers.Flatten(), # Flattens the input, conv2D to 1 vector , which does not affect the batch size.
     layers.Dense(input_shape//2 ,activation="relu"),
     layers.Dense(input_shape//2 ,activation="relu"),
     layers.Dense(2,activation="sigmoid"),
     ]
)
# After model is "built" call its summary() menthod to display its contents
simpleMLP.summary()

# Initialization
# Size of the batches of data, adjust it depends on RAM
batch_size = 128
epochs = 5
# Compile MLP model with 3 arguments: loss function, optimizer, and metrics function to judge model performance
simpleMLP.compile(loss="binary_crossentropy",optimizer="adam",metrics=["binary_accuracy"])  #BCE

# Create ImagedataGenerator to splite training, validation dataset
from keras.preprocessing.image import ImageDataGenerator

train_dir = '/content/train'
train_datagen = ImageDataGenerator(
    rescale=1./255, # rescaling factor
    shear_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True,
    fill_mode='nearest')

valid_dir = '/content/valid'
valid_datagen =ImageDataGenerator(
    rescale=1./255,
    shear_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True,
    fill_mode='nearest')


train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=img_window[:2],
    batch_size=batch_size,
    class_mode='binary',
    color_mode='rgb'
    )

validation_generator = valid_datagen.flow_from_directory(
    valid_dir,
    target_size=img_window[:2],
    batch_size=batch_size,
    class_mode='binary',
    color_mode='rgb')

# Train the MLP model
simpleMLP.fit_generator((
    train_generator, 
    steps_per_epoch= 8271 // batch_size,
    epochs=5,
    validation_data=validation_generator,
    validation_steps= 2072 // batch_size)

能否请您告诉我如何解决此问题?预先感谢。

1 个答案:

答案 0 :(得分:1)

您的问题很简单,您有形状为(N, 1)且损失定义为binary_crossentropy的标签。这意味着您应该在最后一层中有一个输出节点。但是您有一个输出两个类的模型。

simpleMLP = Sequential(
    [...
     layers.Dense(2,activation="sigmoid"),
     ]
)

只需将其更改为

simpleMLP = Sequential(
    [...
     layers.Dense(1,activation="sigmoid"),
     ]
)