Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么keras说我只有一个标签而不是三个_Python_Numpy_Tensorflow_Keras - Fatal编程技术网

Python 为什么keras说我只有一个标签而不是三个

Python 为什么keras说我只有一个标签而不是三个,python,numpy,tensorflow,keras,Python,Numpy,Tensorflow,Keras,我得到一个错误,tensorflow/keras说我只有一个输出,所以我应该在稠密层中有一个(1)而不是(4),我制作了一个标签数组 #goes through files and turns images into arrays #going through each folder for categories in os.listdir('train'): category = str('train/' + folder) #going through each file i

我得到一个错误,tensorflow/keras说我只有一个输出,所以我应该在稠密层中有一个(1)而不是(4),我制作了一个标签数组

#goes through files and turns images into arrays
#going through each folder
for categories in os.listdir('train'):
    category = str('train/' + folder)
    #going through each file in folder
    for files in os.listdir(category):
        #creates file path
        filePath = category + '/' + files
        image = keras.preprocessing.image.load_img(filePath)
        imageArr = keras.preprocessing.image.img_to_array(image)
        features.append(imageArr)

# makes labels
# iterates over folders
for names in os.listdir('train'):
      label = 0
      #increment as value of current folder
      label += 1
      for files in os.listdir('train/' + names):
          #adds a number per label
          labels.append(label)

features = np.asarray(features)
labels = np.asarray(labels)


model = keras.Sequential()
model.add(Conv2D(30, (20, 20), input_shape=(600, 600, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(4, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
我得到的错误是,当我知道我有4个时,我只有一个输出,因为当我打印“标签”时,我得到了一系列从1到4的数字,是因为它们都在一个数组中吗?谢谢你的帮助

错误:

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

首先,你们有几类

案例1: 如果您有4个类别,根据模型输出-
model.add(密集(4,activation='sigmoid'))
,那么您应该使用
softmax
而不是
sigmoid
softmax
用于多标签分类,而
sigmoid
用于二进制分类。您还需要修改
model.copile()
函数:

model.add(Dense(4, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop', # or 'adam'
              metrics=['accuracy'])
案例2: 如果您的任务是二进制分类,那么您需要将模型的输出从4更改为1,代码的其他部分也可以:

model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
案例3: 如果您有3个类别:

model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop', # or 'adam'
              metrics=['accuracy'])

标签的范围为1到4,但每个图像有一个标签。你指定你的损失为二元交叉熵。这意味着你的致密层中应该有1个神经元,而不是4个。此外,您的标签在1-4范围内,它们应该在0-3范围内,因此在您的代码中 因为标签应该是

for names in os.listdir('train'):
      label = 0
      #increment as value of current folder      
      for files in os.listdir('train/' + names):
          #adds a number per label
          labels.append(label)
      label += 1

你有几门课?这能回答你的问题吗?