【问题标题】:Logits and Labels must have the same shape : TensorflowLogits 和 Labels 必须具有相同的形状:Tensorflow
【发布时间】:2021-09-16 01:30:40
【问题描述】:

我正在尝试使用 CNN 网络对猫与狗进行分类,但是尽管检查了两次,但我无法找到错误的来源。根据我的说法,损失函数和形状是有序的,但我仍然无法找到错误的根源

!unzip cats_and_dogs.zip
PATH = 'cats_and_dogs'
train_dir = os.path.join(PATH, 'train')
train_image_generator = ImageDataGenerator(rescale=1./255)

train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
                                                 directory=train_dir,
                                                 target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                 class_mode='binary')
augmented_images = [train_data_gen[0][0][0] for i in range(5)]
plotImages(augmented_images)

model = Sequential()
model.add(Conv2D(25,kernel_size=3,input_shape=(IMG_HEIGHT, IMG_WIDTH, 3),activation="relu"))
model.add(MaxPooling2D())
model.add(Conv2D(25,kernel_size=3,activation="relu"))
model.add(MaxPooling2D())
model.add(Conv2D(25,kernel_size=3,activation="relu"))
model.add(MaxPooling2D())
model.add(Conv2D(25,kernel_size=3,activation="relu"))
model.add(Dense(64,activation="relu"))
model.add(Dense(1,activation="sigmoid"))
model.summary()

model.compile(optimizer="adam",metrics=['accuracy'],loss='binary_crossentropy')
history = model.fit_generator(train_data_gen)

我正在努力解决的错误是

ValueError: logits and labels must have the same shape ((None, 15, 15, 1) vs (None, 1))

【问题讨论】:

    标签: tensorflow keras computer-vision shapes valueerror


    【解决方案1】:

    在将张量流向密集层之前,我忘记将其展平

    【讨论】:

    • 我认为如果它解决了您的问题,您应该将此答案标记为已接受
    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2019-01-12
    • 2018-07-28
    • 2022-01-11
    • 1970-01-01
    • 2017-09-12
    相关资源
    最近更新 更多