How to solve this problem in loss.py file while using HingeEmbeddingLoss

I’m using HingeEmbeddingLoss() but got this error.
Please help me. I don’t find any thing related to this error on the forum.

File "traning_horlicks.py", line 194, in <module>
    num_epochs=25)
  File "traning_horlicks.py", line 92, in train_model
    loss = criterion(outputs, labels)
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/loss.py", line 228, in forward
    self.size_average)(input, target)
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/_functions/loss.py", line 105, in forward
    buffer[torch.eq(target, -1.)] = 0
TypeError: torch.eq received an invalid combination of arguments - got (torch.cuda.LongTensor, float), but expected one of:
 * (torch.cuda.LongTensor tensor, int value)
      didn't match because some of the arguments have invalid types: (torch.cuda.LongTensor, float)
 * (torch.cuda.LongTensor tensor, torch.cuda.LongTensor other)
      didn't match because some of the arguments have invalid types: (torch.cuda.LongTensor, float)
 * (torch.cuda.LongTensor tensor, int value)
      didn't match because some of the arguments have invalid types: (torch.cuda.LongTensor, float)
 * (torch.cuda.LongTensor tensor, torch.cuda.LongTensor other)
      didn't match because some of the arguments have invalid types: (torch.cuda.LongTensor, float)

Thanks

The error message says whats wrong:
You provide two variables with the types “(torch.cuda.LongTensor, float)” to the function, but it wants e.g. “(torch.cuda.LongTensor tensor, int value)” or one of the other types.
So the problem is your the type of your second argument. Change it appropriate.

1 Like

I’m feeding the output of the neural network and the labels to the EmbeddingLoss(), both of them are torch.cuda.LongTensor.

If you look at the error, it shows the error is at line 105 of file loss.py

buffer[torch.eq(target, -1.)] = 0

torch.eq received an invalid combination of arguments - got (torch.cuda.LongTensor, float), but expected one of:

  • (torch.cuda.LongTensor tensor, int value)

the type of my target is torch.cuda.LongTensor which is I’m feeding to the function but the problem is in the next argument of the function.

I don’t think this is an error from my side. I’m not feeding any wrong input, the problem is in the code of loss.py file. Please correct me if I’m wrong

Thanks

I found the answer on the forum, that by changing target variable to FloatTensor will solve this issue.