ValueError: 目标尺寸 (torch.Size([16])) 必须与输入尺寸相同 (torch.Size([16, 1])) [英] ValueError: Target size (torch.Size([16])) must be the same as input size (torch.Size([16, 1]))

查看:31
本文介绍了ValueError: 目标尺寸 (torch.Size([16])) 必须与输入尺寸相同 (torch.Size([16, 1]))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ValueError                                Traceback (most recent call last)
<ipython-input-30-33821ccddf5f> in <module>
     23         output = model(data)
     24         # calculate the batch loss
---> 25         loss = criterion(output, target)
     26         # backward pass: compute gradient of the loss with respect to model parameters
     27         loss.backward()

C:UsersmnaufAnaconda3envsfederated_learninglibsite-packages	orch
nmodulesmodule.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

C:UsersmnaufAnaconda3envsfederated_learninglibsite-packages	orch
nmodulesloss.py in forward(self, input, target)
    593                                                   self.weight,
    594                                                   pos_weight=self.pos_weight,
--> 595                                                   reduction=self.reduction)
    596 
    597 

C:UsersmnaufAnaconda3envsfederated_learninglibsite-packages	orch
nfunctional.py in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight)
   2073 
   2074     if not (target.size() == input.size()):
-> 2075         raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size()))
   2076 
   2077     return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)

ValueError: Target size (torch.Size([16])) must be the same as input size (torch.Size([16, 1]))

我正在训练一个 CNN.处理马匹与人类数据集.这是我的代码.我正在使用 criterion = nn.BCEWithLogitsLoss()optimizer = optim.RMSprop(model.parameters(), lr=0.01).我的最后一层是 self.fc2 = nn.Linear(512, 1).输出最后一个神经元,会为马输出 1,为人输出 0,对吗?还是应该选择 2 个神经元作为输出?

I am training a CNN. Working on the Horses vs humans dataset. This is my code. I am using criterion = nn.BCEWithLogitsLoss() and optimizer = optim.RMSprop(model.parameters(), lr=0.01). My final layer is self.fc2 = nn.Linear(512, 1). Out last neuron, will output 1 for horse and 0 for human, right? or should I choose 2 neurons for output?

16 是批量大小.由于错误说 ValueError: Target size (torch.Size([16])) must be the same as input size (torch.Size([16, 1])).我不明白,我需要在哪里进行更改以纠正错误.

16 is the batch size. Since the error says ValueError: Target size (torch.Size([16])) must be the same as input size (torch.Size([16, 1])). I don't understand, where do I need to make change, to rectify the error.

推荐答案

target = target.unsqueeze(1),在将目标传递给标准之前,将目标张量大小从 [16][16,1].这样做解决了问题.此外,我还需要在将它传递给标准之前执行 target = target.float(),因为我们的输出是浮动的.此外,代码中还有另一个错误.我在最后一层使用了 sigmoid 激活函数,但我不应该使用,因为我使用的标准已经内置了 sigmoid.

target = target.unsqueeze(1), before passing target to criterion, changed the target tensor size from [16] to [16,1]. Doing it solved the issue. Furthermore, I also needed to do target = target.float() before passing it to criterion, because our outputs are in float. Besides, there was another error in the code. I was using sigmoid activation function in the last layer, but I shouldn’t because the criterion I am using already comes with sigmoid builtin.

这篇关于ValueError: 目标尺寸 (torch.Size([16])) 必须与输入尺寸相同 (torch.Size([16, 1]))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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