I’m using HingeLoss as a loss function but got this error.
inconsistent tensor size at d:\downloads\pytorch-master-1\torch\lib\th\generic/THTensorMath.c:134
The documentation says: Measures the loss given an input x which is a 2D mini-batch tensor and a labels y, a 1D tensor containing values (1 or -1).
I’m feeding a 2D mini-batch tensor
Outputs of model Variable containing:
0.0325 0.2188
0.0325 0.2188
0.0325 0.2188
0.0325 0.2188
[torch.FloatTensor of size 4x2]
torch.Size([4, 2])
1D target variable
Lables of the class Variable containing:
-1
-1
-1
-1
[torch.FloatTensor of size 4]
torch.Size([4])
This is the full error
Epoch 0/9
----------
LR is set to 0.001
Outputs of model Variable containing:
0.0325 0.2188
0.0325 0.2188
0.0325 0.2188
0.0325 0.2188
[torch.FloatTensor of size 4x2]
torch.Size([4, 2])
Lables of the class Variable containing:
-1
-1
-1
-1
[torch.FloatTensor of size 4]
torch.Size([4])
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-14-c1d26d6a84db> in <module>()
----> 1 model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler, num_epochs=10)
<ipython-input-13-84abd5130424> in train_model(model, criterion, optimizer, lr_scheduler, num_epochs)
61 preds[preds == 1] = 1
62
---> 63 loss = criterion(outputs, labels)
64 # Typecasting labels
65 labels = labels.type(torch.LongTensor)
C:\Users\Prakritidev Verma\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
204
205 def __call__(self, *input, **kwargs):
--> 206 result = self.forward(*input, **kwargs)
207 for hook in self._forward_hooks.values():
208 hook_result = hook(self, input, result)
C:\Users\Prakritidev Verma\Anaconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
226 def forward(self, input, target):
227 return self._backend.HingeEmbeddingLoss(self.margin,
--> 228 self.size_average)(input, target)
229
230
C:\Users\Prakritidev Verma\Anaconda3\lib\site-packages\torch\nn\_functions\loss.py in forward(self, input, target)
103 buffer = input.new()
104 buffer.resize_as_(input).copy_(input)
--> 105 buffer[torch.eq(target, -1.)] = 0
106 output = buffer.sum()
107
RuntimeError: inconsistent tensor size at d:\downloads\pytorch-master-1\torch\lib\th\generic/THTensorMath.c:134
What am I doing wrong? Please let me know.
Thanks