Autoencoder on CIFAR wrong dimensions

username: mdimitrov97
cooked: <p>Hello everyone,<br>
I am fairly new to deep learning and neural networks and I am currently following the 60 Minute Blitz tutorial<br>
<a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py</a><br>
I want to recreate this by extending the CNN to use an autoencoder<br>
The following is my autoencoder</p>
<pre><code class="lang-auto">class Autoencoder(nn.Module):
    def __init__(self):
        super(Autoencoder,self).__init__()
        
        self.encoder = nn.Sequential(
            nn.Conv2d(3, 6, kernel_size=5),
            nn.ReLU(True),
            nn.Conv2d(6,16,kernel_size=5),
            nn.ReLU(True),
            nn.Sigmoid()
        )
        
        self.decoder = nn.Sequential(             
            nn.ConvTranspose2d(16,6,kernel_size=5),
            nn.ReLU(True),
            nn.ConvTranspose2d(6,3,kernel_size=5),
            nn.ReLU(True),
            nn.Sigmoid(),
        )
    
    def forward(self,x):
        x = self.encoder(x)
        x = self.decoder(x)
        return x
</code></pre>
<p>However, the part which is computing the accuracy throws the following error<br>
The size of tensor a (32) must match the size of tensor b (16) at non-singleton dimension 2</p>
<p>Any help will be greatly appreciated, thanks in advance</p>

username: albanD
cooked: <p>Hi, could you give the full stack trace of where this error happens please?</p>

username: mdimitrov97
cooked: <pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-214-52ea346a67f2&gt; in &lt;module&gt;
      7         _, predicted_auto = torch.max(outputs_auto.data, 1)
      8         total += labels.size(0)
----&gt; 9         correct += (predicted_auto == labels).sum().item()
     10 
     11 print('Accuracy of the network on the 10000 test images: %d %%' % (

RuntimeError: The size of tensor a (32) must match the size of tensor b (16) at non-singleton dimension 2
</code></pre>
<p>This is the full code that computes the accuracy of the model</p>
<pre><code class="lang-auto">correct = 0
total = 0
with torch.no_grad():
    for data in testloader:
        images, labels = data[0].to(device), data[1].to(device)
        outputs_auto = autoencoder(images.to(device))
        _, predicted_auto = torch.max(outputs_auto.data, 1)
        total += labels.size(0)
        correct += (predicted_auto == labels).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' % (
    100 * correct / total))
</code></pre>

username: albanD
cooked: <p>Given the line, I would guess that <code>predicted_auto</code> and <code>labels</code> don’t have the same size: one is of size 32 and the other of size 16.<br>
You can add some printing in your code to make sure it’s the case.</p>

username: mdimitrov97
cooked: <p>Yeah, I printed their sizes and they are indeed different, but I don’t really understand why they are different since in the tutorial example they are the same size</p>

username: albanD
cooked: <p>Which one is the one you were expecting? the one from the network or the one from the labels?</p>

username: mdimitrov97
cooked: <p>I was expecting that the predictions from the autoencoder will be the same size as the predictions from the CNN in the example but I’m not sure why they’re different since the autoencoder expands on the CNN</p>

username: albanD
cooked: <p>Can you print the exact sizes of <code>predicted_auto</code> and <code>labels</code> please?<br>
Also it’s possible that you’re missing some padding in the decoder?</p>

username: mdimitrov97
cooked: <p><code>predicted_auto</code> size - torch.Size([16, 32, 32])<br>
<code>labels</code> size - torch.Size([16])</p>

username: albanD
cooked: <p>So your auto-encoder outputs something which is the same size as the input. Which is expected for an auto-encoder.<br>
But why is your label just of size 16? Do you actually want to do classification?</p>

username: mdimitrov97
cooked: <p>Yes, I want to do classification<br>
I am not even sure if it even makes sense to do classification with this particular autoencoder that I have<br>
Sorry, I am very very new to this</p>

username: albanD
cooked: <p>Hi,</p>
<p>Autoencoder is used to reconstruct something that is the same size as your input.<br>
If you just want to do classification, you should have a network that only outputs one value per class. You can see the mnist example for such a model if you want.</p>