Some wrong about nn.MSELoss

Hello,everyone.

out = net(input)
Target = Variable(t.arange(0,10))
Criterion = nn.MSELoss()
Loss = Criterion(out,Target)
Print(Loss)

when i run it ,which give me a mistake.

And i know how to correct it ,just replace Target = Variable(t.arange(0,10)) with Target = Variable(t.arange(0,10)).float()

why float?

Thank you!!!

Hello Clover!

There are two things going on:

First, many pytorch tensor functions that act on two (or more)
tensors require the tensors to be of the same type. So MSELoss()
doesn’t want you to mix tensors of differing types.

Second, many pytorch tensor functions require arguments of
specific types. So MSELoss() won’t work on LongTensors, even
if both arguments are LongTensors.

This is illustrated by the following pytorch session:

>>> import torch
>>> print (torch.__version__)
0.3.0b0+591e73e
>>>
>>> xx = torch.autograd.Variable (torch.FloatTensor ([[1, 2], [3, 4]]))
>>> yy = torch.autograd.Variable (torch.FloatTensor ([[5, 6], [7, 8]]))
>>>
>>> torch.nn.MSELoss()(xx, yy)
Variable containing:
 16
[torch.FloatTensor of size 1]

>>>
>>> uu = torch.autograd.Variable (torch.DoubleTensor ([[1, 2], [3, 4]]))
>>> vv = torch.autograd.Variable (torch.DoubleTensor ([[5, 6], [7, 8]]))
>>>
>>> torch.nn.MSELoss()(uu, vv)
Variable containing:
 16
[torch.DoubleTensor of size 1]

>>>
>>> torch.nn.MSELoss()(xx, vv)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\<torch_install>\torch\nn\modules\module.py", line 325, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\<torch_install>\torch\nn\modules\loss.py", line 329, in forward
    return F.mse_loss(input, target, size_average=self.size_average, reduce=self.reduce)
RuntimeError: Expected object of type Variable[torch.FloatTensor] but found type Variable[torch.DoubleTensor] for argument #1 'target'
>>>
>>> ii = torch.autograd.Variable (torch.LongTensor ([[1, 2], [3, 4]]))
>>> jj = torch.autograd.Variable (torch.LongTensor ([[5, 6], [7, 8]]))
>>>
>>> torch.nn.MSELoss()(xx, jj.float())
Variable containing:
 16
[torch.FloatTensor of size 1]

>>>
>>> torch.nn.MSELoss()(xx, jj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\<torch_install>\torch\nn\modules\module.py", line 325, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\<torch_install>\torch\nn\modules\loss.py", line 329, in forward
    return F.mse_loss(input, target, size_average=self.size_average, reduce=self.reduce)
RuntimeError: Expected object of type Variable[torch.FloatTensor] but found type Variable[torch.LongTensor] for argument #1 'target'
>>>
>>> torch.nn.MSELoss()(ii, jj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\<torch_install>\torch\nn\modules\module.py", line 325, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\<torch_install>\torch\nn\modules\loss.py", line 329, in forward
    return F.mse_loss(input, target, size_average=self.size_average, reduce=self.reduce)
RuntimeError: mse_loss_forward is not implemented for type torch.LongTensor
>>>

(In the future, could you please not post screenshots of text?
Please just post the text itself so that it will be searchable and
can be copy-pasted.)

Best.

K. Frank

1 Like

My problem has been solved. Thank you. And I’ll listen to your opinion.
Best
cclover

I’m sorry to have caused you trouble