Same code crashes in a different pytorch version

Pablo_Garcia_Sant: <p>Hi, when my tensor:</p>
<pre><code class="lang-auto">tensor([[[-0.2132, -0.0098, -0.2187,  0.2484, -0.2478, -0.1970],
         [-0.1974, -0.0097, -0.1878,  0.2076, -0.2087, -0.1970],
         [-0.2494, -0.0098, -0.2379,  0.2802, -0.2788, -0.2645],
         [-0.2179, -0.0098, -0.2074,  0.2394, -0.2392, -0.2645]]])

</code></pre>
<p>Passes through the following layer:</p>
<pre><code>    self.conv1 = nn.Conv2d(in_channels=1, out_channels=24, kernel_size=2, padding=1)
</code></pre>
<p>With versions: Python: 3.8 , pytorch: 1.11.0 , It executes without problem</p>
<p>But with the versions, Python: 3.7.13, pytorch 1.10.0 it gets the error:</p>
<pre><code class="lang-auto">RuntimeError: Expected 4-dimensional input for 4-dimensional weight [24, 1, 2, 2], but got 3-dimensional input of size [1, 4, 6] instead
</code></pre>
<p>It is hard for me to understand because both the layer and the tensor are the same in both cases. (I needed that code to be executable in the older version)</p>
<p>EDIT: I’m checking the documentation of the Conv2d layer for both versions, and everythings seems the same.</p>

KFrank: <p>Hi Pablo!</p>
<aside class="quote no-group" data-username="Pablo_Garcia_Sant" data-post="1" data-topic="158419" 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/pablo_garcia_sant/48/51588_2.png" class="avatar"> Pablo_Garcia_Sant:</div>
<blockquote>
<p>With versions: Python: 3.8 , pytorch: 1.11.0 , It executes without problem</p>
<p>But with the versions, Python: 3.7.13, pytorch 1.10.0 it gets the error:</p>
<pre><code class="lang-auto">RuntimeError: Expected 4-dimensional input for 4-dimensional weight [24, 1, 2, 2], but got 3-dimensional input of size [1, 4, 6] instead
</code></pre>
</blockquote>
</aside>
<p>Pytorch version 1.11 made a flexibility improvement to <code>Conv2d</code>  so that it<br>
no longer requires a batch dimension.  Quoting from <code>Conv2d</code>’s <a href="https://pytorch.org/docs/1.10/generated/torch.nn.Conv2d.html" rel="noopener nofollow ugc">1.10</a> and<br>
<a href="https://pytorch.org/docs/1.11/generated/torch.nn.Conv2d.html" rel="noopener nofollow ugc">1.11</a> documentation, respectively:</p>
<blockquote>
<p>Shape:</p>
<pre><code>   Input: (N,Cin,Hin,Win)(N, C_{in}, H_{in}, W_{in})(N,Cin​,Hin​,Win​)
</code></pre>
</blockquote>
<blockquote>
<p>Shape:</p>
<pre><code>   Input: (N,Cin,Hin,Win)(N, C_{in}, H_{in}, W_{in})(N,Cin​,Hin​,Win​) or (Cin,Hin,Win)(C_{in}, H_{in}, W_{in})(Cin​,Hin​,Win​)
</code></pre>
</blockquote>
<p>If it were me, I would try to modify your current code to work naturally with<br>
a batch dimension.  If that’s impractical, you could use <code>unsqueeze()</code> /<br>
<code>squeeze()</code> to temporarily add a batch dimension to your tensor:</p>
<pre data-code-wrap="python"><code class="lang-python">conv_output = self.conv1 (conv_input.unsqueeze (0)).squeeze (0)
</code></pre>
<p>Assuming that <code>conv_input</code> lacks a batch dimension, this will work in<br>
version 1.10 while continuing to work in version 1.11.</p>
<p>Best.</p>
<p>K. Frank</p>

Pablo_Garcia_Sant: <p>Thanks mate! That should be it, I can’t try it right now, I just didn’t notice that flexibility difference</p>