ValueError:此损失期望目标与输出具有相同的形状

时间:2019-11-13 07:31:34

标签: python tensorflow keras deep-learning conv-neural-network

我试图使用卷积神经网络对猫和狗进行分类。 X包含特征,Y包含标签。图像数据集来自Microsoft网站。我不明白为什么会出现形状错误。

Image_Size=65
import pickle
import numpy as np
X=np.asarray(pickle.load(open("X.pickle","rb")))
Y=np.asarray(pickle.load(open("X.pickle","rb")))
import tensorflow as tf
from tensorflow.keras.layers import Dense, Activation, Flatten,Conv2D, MaxPooling2D
from tensorflow.keras.models import Sequential

X=X/255.0
model=Sequential()

model.add(Conv2D(64,(3,3), input_shape=X.shape[1:]))

model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))


model.add(Conv2D(64,(3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(1))

model.add(Activation('sigmoid'))

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

model.fit(X,Y,batch_size=32,epochs=3,validation_split=0.3)

现在,我收到以下错误消息。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-67-1eaf182a2702> in <module>
 23 
 24 model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
 ---> 25 model.fit(X,Y,batch_size=32,epochs=3,validation_split=0.3)
 26 model.summary()

 C:\Anaconda\lib\site-packages\tensorflow_core\python\keras\engine\training.py in fit(self, x, y, 
batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, 
sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, 
workers, use_multiprocessing, **kwargs)
726         max_queue_size=max_queue_size,
727         workers=workers, 
--> 728         use_multiprocessing=use_multiprocessing)
729 
730   def evaluate(self,

C:\Anaconda\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in fit(self, model, 
x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, 
class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, 
**kwargs)
222           validation_data=validation_data,
223           validation_steps=validation_steps,
--> 224           distribution_strategy=strategy)
225 
226       total_samples = _get_total_number_of_samples(training_data_adapter)

C:\Anaconda\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in 
_process_training_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, 
steps_per_epoch, validation_split, validation_data, validation_steps, shuffle, 
distribution_strategy, max_queue_size, workers, use_multiprocessing)
514         batch_size=batch_size,
515         check_steps=False,
--> 516         steps=steps_per_epoch)
517     (x, y, sample_weights,
518      val_x, val_y,

C:\Anaconda\lib\site-packages\tensorflow_core\python\keras\engine\training.py in 
 _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, 
 steps_name, 
 steps, validation_split, shuffle, extract_tensors_from_dataset)
 2536           # Additional checks to avoid users mistakenly using improper loss fns.
 2537           training_utils.check_loss_and_target_compatibility(
 -> 2538               y, self._feed_loss_fns, feed_output_shapes)
 2539 
 2540       # If sample weight mode has not been set and weights are None for all the

C:\Anaconda\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py in 
check_loss_and_target_compatibility(targets, loss_fns, output_shapes)
741           raise ValueError('A target array with shape ' + str(y.shape) +
742                            ' was passed for an output of shape ' + str(shape) +
--> 743                            ' while using as loss `' + loss_name + '`. '
744                            'This loss expects targets to have the same shape '
745                            'as the output.')

ValueError: A target array with shape (24946, 65, 65, 1) was passed for an output of shape (None, 1) 
while using as loss `binary_crossentropy`. This loss expects targets to have the same shape as the 
output.

如果有人在这件事上提供帮助,那就太好了。

1 个答案:

答案 0 :(得分:1)

简单的答案是:当标签为图像时,您的网络仅返回一个数字。 如果您要分类图像上的猫还是狗,则标签应为数字。例如,如果图像上的猫是1,则0。特征应该是表示为3维张量的图像。 在您的代码中

X=np.asarray(pickle.load(open("X.pickle","rb")))
Y=np.asarray(pickle.load(open("X.pickle","rb")))

XY具有相同的对象-您没有标签和特征(您可能具有两倍的特征)。

第二个问题可能在这一行:

model.fit(X,Y,batch_size=32,epochs=3,validation_split=0.3)

正如您提到的,您在X-标签和i Y功能中。方法fit 将作为第一个参数特征并作为第二个参数标签。

请注意表示法。通常,在深度学习问题中,X表示要素,Y表示标签。