ValueError:logits 和标签必须具有相同的形状 ((None, 4) vs (None, 1)) [英] ValueError: logits and labels must have the same shape ((None, 4) vs (None, 1))

查看:129
本文介绍了ValueError:logits 和标签必须具有相同的形状 ((None, 4) vs (None, 1))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试制作一个卷积神经网络来对狗和猫进行分类.我在标题中提到了错误.

I try to make a convolutional neural network in order to classify dogs and cat. I got error mentioned in the title.

从我的搜索来看,有人说错误属于不同版本的tensorflow和keras库,也有人说是语法错误.我会把我的代码放在这里,告诉我哪里出错了.

From my search, some said that error belongs to different versions of tensorflow and keras libraries, others said that it is a syntax error. I will leave my code here, tell me where i made mistakes.

#IMPORTING LIBRARIES
import tensorflow as tf
import pandas as pd
import keras
from keras.preprocessing.image import ImageDataGenerator



#IMAGE DATA PREPROCESSING

#preprocessing the training set
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
training_set = train_datagen.flow_from_directory(
        directory = r"C:UsersCucuDownloads	raining_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')

#preprocessing the test set
test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
        directory = r"C:UsersCucuDownloads	est_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')


 
#BULDING THE CNN
#
#
#initialising the cnn
cnn = tf.keras.models.Sequential()


#convolution
cnn.add(tf.keras.Input(shape=(64, 64, 3)))
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu' ))


#pooling
cnn.add(tf.keras.layers.MaxPool2D( pool_size = 2 , strides = 2))


#adding a SECOND CONVOLUTIONAL LAYER
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu'))
cnn.add(tf.keras.layers.MaxPool2D( pool_size = 2 , strides = 2))


#flattening
cnn.add(tf.keras.layers.Flatten())


#full connection
cnn.add(tf.keras.layers.Dense(units = 128 , activation = 'relu'))


#adding the output layer
cnn.add(tf.keras.layers.Dense(units = 4 , activation = 'sigmoid'))




#TRAINING THE CNN
#
#
#compiling the cnn
cnn.compile(optimizer = 'adam' , loss = 'binary_crossentropy' , metrics = ['accuracy'] )


#training the cnn on the training_set & test_set
cnn.fit(x = training_set , validation_data = test_set , epochs = 25)



#MAKING A SINGLE PREDICTION
import numpy as np
test_image = image.load_img('dataset/single_predictioncat_or_dog_1.jpg' , test_size = (64 , 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image , axis = 0)
result = cnn.predic(test_image)
training_set.class_indices

错误是:

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

推荐答案

改变最后一个dense layer.不是 4 而是 1.

Change the last dense layer. Instead of 4 make it 1.

#adding the output layer
cnn.add(tf.keras.layers.Dense(units = 1 , activation = 'sigmoid'))

这可能是因为您的标签是每个输入的单个值.然而,您定义的最后一层有 4 个输出.损失函数无法比较两种不同的形状.

This might be happening because your label is a single value for each input. Whereas, your final layer defined has 4 outputs. The loss function is not able compare two different shapes.

如果您仍想在最终的密集层中添加多个单元,请将所有输出设为一个热向量,并在密集层中添加与数据集中标签一样多的单元.

If you still want to add more than one unit in the final dense layer, make all your outputs a one hot vector and add as many units in the dense layer as there are labels in your data set.

这篇关于ValueError:logits 和标签必须具有相同的形状 ((None, 4) vs (None, 1))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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