ValueError:此损失期望目标与输出具有相同的形状 [英] ValueError : This loss expects targets to have the same shape as the output

查看:70
本文介绍了ValueError:此损失期望目标与输出具有相同的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to classify the Cats and Dogs using Convolutional Neural Network. Here X contains the featuers and Y the labels. The image dataset is taken from Microsoft site. I dont understand why the shape error is coming.

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)

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

Now I get the following error.

---------------------------------------------------------------------------
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.

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

It would be great if someone helps in this matter.

推荐答案

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

The short answer is: your network returns just one number when your labels are images. If you would classify if on the image is a cat or a dog your labels should be a numbers. For example 1 if on the image is a cat and 0. The features should be images represented as 3 dimensional tensors. In your code

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

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

X and Y have the same objects - you don't have labels and features (you have probably two times features).

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

The second problem could be in this line:

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

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

As you mentioned you have in X - labels, and i Y features. The method fit takes as first argument features and as the second argument labels.

请注意表示法.通常,在深度学习问题中, X 表示功能,而 Y 表示标签.

Please be careful about the notation. Typically in deeplearning problems X denotes features and Y denotes labels.

这篇关于ValueError:此损失期望目标与输出具有相同的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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