<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">The MSELoss criteria has a strange behavior when used with two tensors of different sizes -- I would expect this to throw an error. Instead it does... something else.</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<p dir="auto">Install stable, non-CUDA torch, i.e.: <a href="https://download.pytorch.org/whl/cpu/torch-1.0.0-cp37-cp37m-linux_x86_64.whl" rel="nofollow">https://download.pytorch.org/whl/cpu/torch-1.0.0-cp37-cp37m-linux_x86_64.whl</a></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import torch
&gt;&gt;&gt; import torch.nn as nn
&gt;&gt;&gt; a = torch.Tensor([1, 2, 3])
&gt;&gt;&gt; b = torch.Tensor([[1],[2],[3]])
&gt;&gt;&gt; nn.MSELoss()(a, b)
tensor(1.3333)"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">torch</span>.<span class="pl-s1">nn</span> <span class="pl-k">as</span> <span class="pl-s1">nn</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>([[<span class="pl-c1">1</span>],[<span class="pl-c1">2</span>],[<span class="pl-c1">3</span>]])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">nn</span>.<span class="pl-v">MSELoss</span>()(<span class="pl-s1">a</span>, <span class="pl-s1">b</span>)
<span class="pl-en">tensor</span>(<span class="pl-c1">1.3333</span>)</pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">I would've appreciated an error in this case. Perhaps there is some reason why someone might wish to call MSELoss with different sized arrays... but I think if the <em>number of dimensions</em> between the two vectors differs, it's likely the user's mistake.</p>
<p dir="auto">I realize this is a "rookie mistake," but I promise it was <em>not</em> a fun way to spend 2 hours. Perhaps a friendly warning message could save someone the headache in the future?</p>
<h2 dir="auto">Environment</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python3 collect_env.py 
Collecting environment information...
PyTorch version: 1.0.0
Is debug build: No
CUDA used to build PyTorch: Could not collect

OS: Arch Linux
GCC version: (GCC) 8.2.1 20181127
CMake version: version 3.13.3

Python version: 3.7
Is CUDA available: No
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: Quadro M1000M
Nvidia driver version: 415.27
cuDNN version: Could not collect

Versions of relevant libraries:
[pip] Could not collect
[conda] Could not collect"><pre class="notranslate"><code class="notranslate">$ python3 collect_env.py 
Collecting environment information...
PyTorch version: 1.0.0
Is debug build: No
CUDA used to build PyTorch: Could not collect

OS: Arch Linux
GCC version: (GCC) 8.2.1 20181127
CMake version: version 3.13.3

Python version: 3.7
Is CUDA available: No
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: Quadro M1000M
Nvidia driver version: 415.27
cuDNN version: Could not collect

Versions of relevant libraries:
[pip] Could not collect
[conda] Could not collect
</code></pre></div>
<h2 dir="auto">Additional context</h2>
<p dir="auto">It looks like this may have been looked at previously in issue <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="364300006" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/12129" data-hovercard-type="issue" data-hovercard-url="/pytorch/pytorch/issues/12129/hovercard" href="https://github.com/pytorch/pytorch/issues/12129">#12129</a> or PR <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="294889195" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/5085" data-hovercard-type="pull_request" data-hovercard-url="/pytorch/pytorch/pull/5085/hovercard" href="https://github.com/pytorch/pytorch/pull/5085">#5085</a> ?</p>
<h2 dir="auto">Issue description</h2>
<p dir="auto">When computing the loss between <code class="notranslate">predicted</code> and <code class="notranslate">target</code> with shapes such as <code class="notranslate">target.shape = [100]</code> and <code class="notranslate">predicted.shape = [100, 1]</code>, an error should be thrown.  Indeed, my colleagues tell me this was the case in version 0.4, but lost in version 1.0.</p>
<h2 dir="auto">Code example</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# Version 1.0
# Set up straightforward linear regression problem.
import torch
import torch.nn as nn
sample_size = 100
x = torch.randn((sample_size,2))
eps = torch.randn(sample_size )
y = 10 + 9*x[:,0] - 2*x[:, 1] +  eps
#y = y.reshape(sample_size, 1)
model = nn.Linear(2, 1)

optimizer = torch.optim.SGD(model.parameters(), lr=.1)
loss_func = nn.MSELoss()
for epoch in range(30):
   optimizer.zero_grad()
   yhat = model(x)
   loss_val = loss_func(yhat, y)
   loss_val.backward()
   optimizer.step()
   print(loss_val.item())

print(model.weight.data)
print(model.bias.data)
"><pre class="notranslate"><code class="notranslate"># Version 1.0
# Set up straightforward linear regression problem.
import torch
import torch.nn as nn
sample_size = 100
x = torch.randn((sample_size,2))
eps = torch.randn(sample_size )
y = 10 + 9*x[:,0] - 2*x[:, 1] +  eps
#y = y.reshape(sample_size, 1)
model = nn.Linear(2, 1)

optimizer = torch.optim.SGD(model.parameters(), lr=.1)
loss_func = nn.MSELoss()
for epoch in range(30):
   optimizer.zero_grad()
   yhat = model(x)
   loss_val = loss_func(yhat, y)
   loss_val.backward()
   optimizer.step()
   print(loss_val.item())

print(model.weight.data)
print(model.bias.data)

</code></pre></div>
<p dir="auto">If you leave y.reshape commented out, the correct estimates of this straightforward linear regression problem are not found and the loss fails to decrease into a region near the global optimum of this convex problem.  Yet, no error is raised.</p>
<p dir="auto">If you uncomment y.reshape, the correct estimates are found and the loss decreases in the anticipated way.</p>
<h2 dir="auto">System Info</h2>
<p dir="auto">Collecting environment information...<br>
PyTorch version: 1.0.0<br>
Is debug build: No<br>
CUDA used to build PyTorch: None</p>
<p dir="auto">OS: Ubuntu 16.04.5 LTS<br>
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609<br>
CMake version: Could not collect</p>
<p dir="auto">Python version: 3.5<br>
Is CUDA available: No<br>
CUDA runtime version: No CUDA<br>
GPU models and configuration: No CUDA<br>
Nvidia driver version: No CUDA<br>
cuDNN version: No CUDA</p>
<p dir="auto">Versions of relevant libraries:<br>
[pip] Could not collect<br>
[conda] blas                      1.0                         mkl<br>
[conda] mkl                       2017.0.3                      0<br>
[conda] torch                     1.0.0                     <br>
[conda] torchvision               0.2.1                     </p>