ValueError: expected 4D input (got 3D input) (Different)

Ayush_Singhal: <p>I was working with some Pytorch Model when I got this error. At first glance I thought that this issue might have raised because the model Expect <code>(Batch x the_3d_size)</code> for Image. But then I thought that Pytorch have the functionality to directly pass <code>both batched and non-batched data</code> into the <code>inherited classes</code> of <code>nn.Module</code>. Like this</p>
<pre><code class="lang-auto">layer = nn.Linear(4 , 5)

non_batched_input = torch.rand(4)

layer(non_batched_input)

Output = tensor([-0.1435,  0.4662, -0.6626, -0.3902,  0.4086], grad_fn=&lt;ViewBackward0&gt;)

batched_input = torch.rand(5 , 4)

layer(batched_input)

Output = tensor([[-0.5876,  0.2451, -0.7482, -0.1869,  0.0120],
        [-0.2541,  0.2187, -0.5797, -0.1493,  0.4184],
        [-0.3964,  0.1388, -0.5467, -0.0359,  0.2836],
        [-0.2042,  0.5370, -0.6630, -0.4109,  0.6399],
        [-0.2992,  0.1950, -0.6337, -0.1641,  0.0830]],
       grad_fn=&lt;AddmmBackward0&gt;)
</code></pre>
<p>I also visited the same Issue on the forums. But that was related to batch. My question is why is Pytorch at this point not able to recognize the batched and the non batched-data…?</p>
<p>This is specifically not an Issue as it has been already solved <a href="https://discuss.pytorch.org/t/valueerror-expected-4d-input-got-3d-input/150585">here</a> with the add on of Single LOC</p>
<pre><code class="lang-auto">valid_result = model(image_valid.unsqueeze(0))
</code></pre>

ptrblck: <aside class="quote no-group" data-username="Ayush_Singhal" data-post="1" data-topic="196445">
<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/ayush_singhal/48/62393_2.png" class="avatar"> Ayush_Singhal:</div>
<blockquote>
<p>My question is why is Pytorch at this point not able to recognize the batched and the non batched-data…?</p>
</blockquote>
</aside>
<p>I don’t understand this question as it seems the outputs are expected.<br>
For the unbatched input you will receive an unbatched output and a batched input will create an output batch. Could you describe what the issue is?</p>

Ayush_Singhal: <p>According to this <a href="https://discuss.pytorch.org/t/valueerror-expected-4d-input-got-3d-input/150585">link</a>, we need to give the input to the layer in the format of <code>(Batch x the_size)</code>. But what I have worked with Pytorch, it is always able to recognize the difference between <code>Batched-Input</code> and <code>Non-Batched Input</code>(also as you said).</p>
<ul>
<li>So why it this point we need to define an extra dimension for batch before passing to the layer.</li>
<li>If it can already create the distinct output for <code>Batched(4D Input)</code> and <code>Non-Batched(3D Input)</code>, then why it is throwing a <code>Value Error</code>.</li>
<li>If it can process both the <code>4D Input</code> and <code>3D Input</code> ,<code>why it expecting 4D Input</code>.</li>
</ul>
<p>The specific Layer I was getting Issue with was <code>Batch Norm 2d</code></p>
<p>Sorry for replying Late : )</p>