首页 > 解决方案 > ValueError: 目标尺寸 (torch.Size([26])) 必须与输入尺寸 (torch.Size([66])) 相同

问题描述

ValueError                                Traceback (most recent call last)

<ipython-input-28-10509ec63b58> in <module>
     11     start_time = time.time()
     12 
---> 13     train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
     14     valid_loss, valid_acc = evaluate(model, valid_iterator, criterion)
     15 

<ipython-input-25-ecea5e6d8ce8> in train(model, iterator, optimizer, criterion)
     12         predictions = model(batch.t).squeeze(1)
     13 
---> 14         loss = criterion(predictions, batch.l)
     15 
     16         acc = binary_accuracy(predictions, batch.l)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~\anaconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
    599                                                   self.weight,
    600                                                   pos_weight=self.pos_weight,
--> 601                                                   reduction=self.reduction)
    602 
    603 

~\anaconda3\lib\site-packages\torch\nn\functional.py in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight)
   2122 
   2123     if not (target.size() == input.size()):
-> 2124         raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size()))
   2125 
   2126     return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)

ValueError: Target size (torch.Size([26])) must be the same as input size (torch.Size([66]))

我正在训练CNN。使用我自己的印度尼西亚数据集进行情绪分析。这是我的代码。我用了criteria = nn.BCEWithLogitsLoss ()optimizer = optim.RMSprop,。我不明白,我需要在哪里进行更改,以纠正错误。

标签: conv-neural-network

解决方案


推荐阅读