Some wrong about nn.MSELoss

username: cclover
cooked: <p>Hello,everyone.</p>
<pre><code class="lang-auto">out = net(input)
Target = Variable(t.arange(0,10))
Criterion = nn.MSELoss()
Loss = Criterion(out,Target)
Print(Loss)
</code></pre>
<p>when i run it ,which give me a mistake.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/5/b508720a340895155de8f55a6a3de0e44c71126a.png" data-download-href="https://discuss.pytorch.org/uploads/default/b508720a340895155de8f55a6a3de0e44c71126a" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/5/b508720a340895155de8f55a6a3de0e44c71126a.png" alt="image" data-base62-sha1="pPuw1msBWULSHgDBNMTIFTsUMSS" width="690" height="345" data-dominant-color="F6F8F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1130×566 13.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>And i know how to correct it ，just replace Target = Variable(t.arange(0,10)) with Target = Variable(t.arange(0,10)).float()</p>
<p>why float?</p>
<p>Thank you!!!</p>

username: KFrank
cooked: <p>Hello Clover!</p>
<aside class="quote no-group quote-modified" data-username="cclover" data-post="1" data-topic="62641" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cclover/48/18341_2.png" class="avatar"> cclover:</div>
<blockquote>
<pre><code class="lang-auto">out = net(input)
Target = Variable(t.arange(0,10))
Criterion = nn.MSELoss()
Loss = Criterion(out,Target)
Print(Loss)
</code></pre>
<p>when i run it ,which give me a mistake.<br>
…<br>
And i know how to correct it ，just replace Target = Variable(t.arange(0,10)) with Target = Variable(t.arange(0,10)).float()</p>
<p>why float?</p>
</blockquote>
</aside>
<p>There are two things going on:</p>
<p>First, many pytorch tensor functions that act on two (or more)<br>
tensors require the tensors to be of the same type.  So <code>MSELoss()</code><br>
doesn’t want you to mix tensors of differing types.</p>
<p>Second, many pytorch tensor functions require arguments of<br>
specific types.  So <code>MSELoss()</code> won’t work on <code>LongTensor</code>s, even<br>
if both arguments are <code>LongTensor</code>s.</p>
<p>This is illustrated by the following pytorch session:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; print (torch.__version__)
0.3.0b0+591e73e
&gt;&gt;&gt;
&gt;&gt;&gt; xx = torch.autograd.Variable (torch.FloatTensor ([[1, 2], [3, 4]]))
&gt;&gt;&gt; yy = torch.autograd.Variable (torch.FloatTensor ([[5, 6], [7, 8]]))
&gt;&gt;&gt;
&gt;&gt;&gt; torch.nn.MSELoss()(xx, yy)
Variable containing:
 16
[torch.FloatTensor of size 1]

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

&gt;&gt;&gt;
&gt;&gt;&gt; torch.nn.MSELoss()(xx, vv)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\&lt;torch_install&gt;\torch\nn\modules\module.py", line 325, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\&lt;torch_install&gt;\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'
&gt;&gt;&gt;
&gt;&gt;&gt; ii = torch.autograd.Variable (torch.LongTensor ([[1, 2], [3, 4]]))
&gt;&gt;&gt; jj = torch.autograd.Variable (torch.LongTensor ([[5, 6], [7, 8]]))
&gt;&gt;&gt;
&gt;&gt;&gt; torch.nn.MSELoss()(xx, jj.float())
Variable containing:
 16
[torch.FloatTensor of size 1]

&gt;&gt;&gt;
&gt;&gt;&gt; torch.nn.MSELoss()(xx, jj)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\&lt;torch_install&gt;\torch\nn\modules\module.py", line 325, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\&lt;torch_install&gt;\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'
&gt;&gt;&gt;
&gt;&gt;&gt; torch.nn.MSELoss()(ii, jj)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\&lt;torch_install&gt;\torch\nn\modules\module.py", line 325, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\&lt;torch_install&gt;\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
&gt;&gt;&gt;
</code></pre>
<p>(In the future, could you please not post screenshots of text?<br>
Please just post the text itself so that it will be searchable and<br>
can be copy-pasted.)</p>
<p>Best.</p>
<p>K. Frank</p>

username: cclover
cooked: <p>My problem has been solved. Thank you. And I’ll listen to your opinion.<br>
Best<br>
cclover</p>

username: cclover
cooked: <p>I’m sorry to have caused you trouble</p>