关于pytorch中损失函数中输入输出不匹配问题

File "C:\Users\Rain\AppData\Local\Programs\Python\Anaconda.3.5.1\envs\python35\python35\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\Rain\AppData\Local\Programs\Python\Anaconda.3.5.1\envs\python35\python35\lib\site-packages\torch\nn\modules\loss.py", line 500, in forward
    reduce=self.reduce)

  File "C:\Users\Rain\AppData\Local\Programs\Python\Anaconda.3.5.1\envs\python35\python35\lib\site-packages\torch\nn\functional.py", line 1514, in binary_cross_entropy_with_logits

    raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size()))

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

原因

input 和 target 尺寸不匹配

解决办法

将target转为onehot

例如:

one_hot = torch.nn.functional.one_hot(masks, num_classes=args.num_classes)

 

你可能感兴趣的:(pytorch,loss,size,mismatch,onehot)