RuntimeError: Expected 4-dimensional input for 4-dimensional weight [8, 1, 3, 3], but got 2-dimensional input of size [1, 784] instead

Xudong_Li: <p>I saw these kind of topics a lot in forum but generally batch dimension is missing in their tensors. But in my case channel dimension is missing and I don’t know how to just change my model to meet its requirements. Here is my code</p>
<pre><code class="lang-auto">class DAE(nn.Module):
    def __init__(self):
        super(DAE, self).__init__()

        self.conv1 = nn.Sequential(
            nn.Conv2d(1,8,3,1,1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2)
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(8, 64, 3, 1, 1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2)        )
        self.up = nn.UpsamplingBilinear2d(scale_factor=2)
        self.down = nn.MaxPool2d(kernel_size=2 , stride=2)
        self.deconv1 = nn.Sequential(
            nn.Conv2d(64, 8, 3, 1, 1),
            nn.ReLU(),
            nn.UpsamplingBilinear2d(scale_factor=2)
        )
        self.deconv2 = nn.Sequential(
            nn.Conv2d(8, 1, 3, 1, 1),
            nn.ReLU(),
            nn.UpsamplingBilinear2d(scale_factor=2)
        )
        self.linear1 = nn.Linear(64*7*7 , 64 )
        self.linear2 = nn.Linear( 64 ,64*7*7 )

    def forward(self , x ):
        x = self.conv2(self.conv1(x))
        # x = self.res5(self.res4(self.res3(self.res2(self.res1(x)))))
        x = x.view(x.shape[0] ,64*7*7  )
        x = self.linear2(self.linear1(x))
        x = x.view(x.shape[0] , 64 ,7 ,7)
        x = self.deconv2(self.deconv1(x))
        
        return  x
</code></pre>
<pre><code class="lang-auto">f, axes= plt.subplots(6, 3, figsize = (5, 10))
axes[0,0].set_title("Original Image")
axes[0,1].set_title("Noisy Image")
axes[0,2].set_title("Cleaned Image")

for idx, (noisy, clean, label) in enumerate(test_dataloader):
    if idx &gt; 5:
        break
    
    # denoising with DAE
    noisy = noisy.view(noisy.size(0),-1).type(torch.FloatTensor)
    noisy = noisy.to(device)
    output = model(noisy)
    
    # fix size
    output = output.view(1, 28, 28)
    output = output.permute(1, 2, 0).squeeze(2)
    output = output.detach().cpu().numpy()
    
    noisy = noisy.view(1, 28, 28)
    noisy = noisy.permute(1, 2, 0).squeeze(2)
    noisy = noisy.detach().cpu().numpy()

    clean = clean.view(1, 28, 28)
    clean = clean.permute(1, 2, 0).squeeze(2)
    clean = clean.detach().cpu().numpy()
    
    # plot
    axes[idx, 0].imshow(clean, cmap="gray")
    axes[idx, 1].imshow(noisy, cmap="gray")
    axes[idx, 2].imshow(output, cmap="gray")
    
    axes[idx, 0].set(xticks=[], yticks=[])
    axes[idx, 1].set(xticks=[], yticks=[])
    axes[idx, 2].set(xticks=[], yticks=[])
</code></pre>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
13     noisy = noisy.view(noisy.size(0),-1).type(torch.FloatTensor)<br>
14     noisy = noisy.to(device)<br>
—&gt; 15     output = model(noisy)<br>
16<br>
17     # fix size</p>
<p>6 frames<br>
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
720             result = self._slow_forward(*input, **kwargs)<br>
721         else:<br>
–&gt; 722             result = self.forward(*input, **kwargs)<br>
723         for hook in itertools.chain(<br>
724                 _global_forward_hooks.values(),</p>
<p> in forward(self, x)<br>
32<br>
33     def forward(self , x ):<br>
—&gt; 34         x = self.conv2(self.conv1(x))<br>
35         # x = self.res5(self.res4(self.res3(self.res2(self.res1(x)))))<br>
36         x = x.view(x.shape[0] ,64<em>7</em>7  )</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
720             result = self._slow_forward(*input, **kwargs)<br>
721         else:<br>
–&gt; 722             result = self.forward(*input, **kwargs)<br>
723         for hook in itertools.chain(<br>
724                 _global_forward_hooks.values(),</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py in forward(self, input)<br>
115     def forward(self, input):<br>
116         for module in self:<br>
–&gt; 117             input = module(input)<br>
118         return input<br>
119</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
720             result = self._slow_forward(*input, **kwargs)<br>
721         else:<br>
–&gt; 722             result = self.forward(*input, **kwargs)<br>
723         for hook in itertools.chain(<br>
724                 _global_forward_hooks.values(),</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in forward(self, input)<br>
417<br>
418     def forward(self, input: Tensor) -&gt; Tensor:<br>
–&gt; 419         return self._conv_forward(input, self.weight)<br>
420<br>
421 class Conv3d(_ConvNd):</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)<br>
414                             _pair(0), self.dilation, self.groups)<br>
415         return F.conv2d(input, weight, self.bias, self.stride,<br>
–&gt; 416                         self.padding, self.dilation, self.groups)<br>
417<br>
418     def forward(self, input: Tensor) -&gt; Tensor:</p>
<p>RuntimeError: Expected 4-dimensional input for 4-dimensional weight [8, 1, 3, 3], but got 2-dimensional input of size [1, 784] instead</p>

ptrblck: <p>You are passing a 2-dimensional input tensor to the model, while the first conv layer expects a 4-dimensional tensor in the shape <code>[batch_size, channels, height, width]</code>.<br>
This line of code creates the flattened tensor:</p>
<pre><code class="lang-python">noisy = noisy.view(noisy.size(0),-1).type(torch.FloatTensor)
noisy = noisy.to(device)
output = model(noisy)
</code></pre>
<p>After the <code>view</code> operation <code>noisy</code> will have the shape <code>[batch_size, nb_features]</code> and you would need to reshape it to the expected 4 dimensions.</p>