RuntimeError: Expected 4-dimensional input for 4-dimensional weight 6 1 5 5, but got 2-dimensional input of size [20, 76800] instead

rai24: <p>class autoencoder(nn.Module):</p>
<pre><code>def __init__(self):
    
    super(autoencoder, self).__init__()
    self.encoder = nn.Sequential(
        nn.Conv2d(1, 6, 5),  
        nn.ReLU(True),
        nn.MaxPool2d(2,2),  
        nn.Conv2d(6, 16, 5),   
        nn.ReLU(True),
        nn.Linear(16 *57*77, 20000),
        nn.Linear(20000, 14000),
        nn.Linear(14000,2000),
    )
    self.decoder = nn.Sequential(
        nn.Linear(2000,14000),
        nn.Linear(14000,20000),
        
    )

def forward(self, x):
    x = x.view(x.size(0), -1)
    x = self.encoder(x)
   # x = x.unsqueeze(1)
   # print(x.size())
    x = self.decoder(x)
   # print(x.size())
    return x
</code></pre>
<p>model = autoencoder()<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = torch.optim.Adam(model.parameters(), lr=0.005,<br>
weight_decay=1e-5)</p>
<p>for epoch in range(2):<br>
for i, data in enumerate(traindataloader,0):</p>
<pre><code>    img, labels = data
    optimizer.zero_grad()
    print(img.size())
    #img = Variable(img)
    # ===================forward=====================
    output = model(img.unsqueeze(dim=2))
    loss = criterion(output, img)
    # ===================backward====================
    
    loss.backward()
    optimizer.step()
   #===================log========================
    running_loss += loss.item()

    if i % 10 == 0:  
        # print every 2000 mini-batches
        print('[%d, %5d] loss: %.3f' %
              (epoch + 1, i + 1, running_loss / 10))
        running_loss = 0.0
</code></pre>
<p>print(‘Finished Training’)</p>
<p>Please help!</p>

ptrblck: <p><code>self.encoder</code> uses a <code>nn.Conv2d</code> as the first layer, which expects an input with the shape <code>[batch_size, channels, height, width]</code>, while you are flattening your input tensor in the first step via <code>x = x.view(x.size(0), -1)</code>.</p>
<p>If you remove the <code>view</code> op, the first layers will run, but you will run into another shape mismatch error passing the activation to the <code>nn.Linear</code> layer.<br>
Add an <code>nn.Flatten()</code> module before the first <code>nn.Linear</code> and rerun the code again.</p>

rai24: <p>Thank you so so much, I did tried that  but now I am getting below error<br>
RuntimeError: size mismatch, m1: [36480 x 154], m2: [70224 x 20000] at /Users/distiller/project/conda/conda-bld/pytorch_1579022061893/work/aten/src/TH/generic/THTensorMath.cpp:136</p>
<p>Code:</p>
<p>class autoencoder(nn.Module):</p>
<pre><code>def __init__(self):
    
    super(autoencoder, self).__init__()
    self.encoder = nn.Sequential(
        nn.Conv2d(1, 6,stride = 1,kernel_size=5),  
        nn.ReLU(True),
        nn.MaxPool2d(2,2),  
        nn.Conv2d(6, 16,stride = 1,kernel_size=5),
        nn.ReLU(True),
        nn.Linear(16 *57*77, 20000),
        nn.Linear(20000, 14000),
        nn.Linear(14000,2000),
    )
    self.decoder = nn.Sequential(
        nn.Flatten(),
        nn.Linear(2000,14000),
        nn.Linear(14000,20000),
        
    )

def forward(self, x):
   
    x = self.encoder(x)
   # x = x.unsqueeze(1)
   # print(x.size())
    x = self.decoder(x)
   # print(x.size())
    return x
</code></pre>
<p>model = autoencoder()<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = torch.optim.Adam(model.parameters(), lr=0.005,<br>
weight_decay=1e-5)</p>
<p>for epoch in range(2):<br>
for i, data in enumerate(traindataloader,0):</p>
<pre><code>    img, labels = data
    optimizer.zero_grad()
    print(img.size())
    img = Variable(img)
    # ===================forward=====================
    output = model(img)
    loss = criterion(output, img)
    # ===================backward====================
    
    loss.backward()
    optimizer.step()
   #===================log========================
    running_loss += loss.item()

    if i % 10 == 0:  
        # print every 2000 mini-batches
        print('[%d, %5d] loss: %.3f' %
              (epoch + 1, i + 1, running_loss / 10))
        running_loss = 0.0
</code></pre>
<p>print(‘Finished Training’)</p>

ptrblck: <p>You have to add <code>nn.Flatten()</code> before the first linear layer in your encoder.</p>

rai24: <p>It did that it is still showing me the below error:<br>
RuntimeError: size mismatch, m1: [20 x 280896], m2: [70224 x 20000] at /Users/distiller/project/conda/conda-bld/pytorch_1579022061893/work/aten/src/TH/generic/THTensorMath.cpp:136</p>
<p>Do, I have to make changes in the values  added in the Linear layer</p>

ptrblck: <p>It seems so.<br>
Based on the input shape, you would have to adapt the number of input features of this linear layer.</p>

rai24: <p>I calculated it using the formula and choose the arbitrary value for out_features. Can you please tell me what would be the right input_feature of Linear</p>

ptrblck: <p>What input shape are you using?<br>
You could calculate it based on the convolution parameters (kernel size, stride, padding, dilation).</p>

rai24: <p>torch.Size([20, 1, 240, 320])</p>

ptrblck: <p>The first linear layer in your <code>encoder</code> should be <code>nn.Linear(16*114*154, 2)</code>.</p>

rai24: <p>isn’t 2 the small number, how we choose this</p>

rai24: <aside class="quote no-group quote-modified" data-username="ptrblck" data-post="10" data-topic="72190">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>16<em>114</em>154</p>
</blockquote>
</aside>
<p>and don’t we have reduce the size to half because of maxpooling</p>

ptrblck: <p>The <code>2</code> was just for debugging purposes to not flood my machine, so please reset it to your value.</p>
<aside class="quote no-group" data-username="rai24" data-post="12" data-topic="72190">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/bcef8e/48.png" class="avatar"> rai24:</div>
<blockquote>
<p>and don’t we have reduce the size to half because of maxpooling</p>
</blockquote>
</aside>
<p>I did. Do you see any errors using this shape?</p>

rai24: <p>Thank you so much.I I did the changes, as advised by you. I just came across with one error after that which is RuntimeError: 1D target tensor expected, multi-target not supported<br>
SO for that I updated the line of code as below:<br>
loss = criterion(output, labels.squeeze())<br>
The code is on Training since an half an hour,<br>
can you please provide me solution for decreasing  the training time  and “1D target tensor expected, multi-target not supported” error.<br>
I greatly appreciate your help</p>

ptrblck: <p>The shape error is thrown, as for a multi-class classification <code>nn.CrossEntropyLoss</code> expects the target to be a <code>LongTensor</code> in the shape <code>[batch_size]</code> containing the class indices.<br>
I assume you passed <code>labels</code> as a <code>[batch_size, 1]</code> tensor, which will raise this error.<br>
Using <code>labels.squeeze(1)</code> should work.</p>
<aside class="quote no-group" data-username="rai24" data-post="14" data-topic="72190">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/bcef8e/48.png" class="avatar"> rai24:</div>
<blockquote>
<p>can you please provide me solution for decreasing the training time</p>
</blockquote>
</aside>
<p>The training time depends highly on your model, machine, libs etc.<br>
I would recommend to use the latest PyTorch version as well as updated libraries (CUDA, cudnn etc.).<br>
Also, make sure to use multiple workers in your <code>DataLoader</code> (via <code>num_workers</code>).</p>

rai24: <p>I will add that num_workers, however, I am training the below model, it does not through any error but it even does not output anything, don’t know the reason . Please help.<br>
class autoencoder(nn.Module):</p>
<pre><code>def __init__(self):
    
    super(autoencoder, self).__init__()
    self.encoder = nn.Sequential(
        nn.Conv2d(1, 6,stride = 1,kernel_size=5),  
        nn.ReLU(True),
        nn.MaxPool2d(2,2),  
        nn.Conv2d(6, 16,stride = 1,kernel_size=5),
        nn.Flatten(),
        nn.Linear(16*114*154, 20000),
        nn.Linear(20000, 50000),

    )
    self.decoder = nn.Sequential(
        nn.Linear(50000,20000),
        nn.Linear(20000,16*114*154),
       
        
    )

def forward(self, x):
   
    x = self.encoder(x)
    x = self.decoder(x)
    return x
</code></pre>
<p>def train(model, num_epochs=5, batch_size=10, learning_rate=1e-3):<br>
torch.manual_seed(42)<br>
criterion = nn.CrossEntropyLoss() # mean square error loss<br>
optimizer = torch.optim.Adam(model.parameters(),<br>
lr=learning_rate,<br>
weight_decay=1e-5) # &lt;–</p>
<pre><code>outputs = []
for epoch in range(num_epochs):
    for data in traindataloader:
        img, labels = data
        optimizer.zero_grad()
        print(img.size())
        img = Variable(img)
        print(img.size())
        labels = Variable(labels)
        print(labels.size())
    # ===================forward=====================
        output = model(img)
        print(output.size())
        train_loss = criterion(output, labels.squeeze())
    # ===================backward====================
    
        train_loss.backward()
        optimizer.step()

    print('Epoch:{}, Loss:{:.4f}'.format(epoch+1, float(train_loss)))
    outputs.append((epoch, img, output),)
return outputs
</code></pre>
<p>model = autoencoder()<br>
max_epochs = 5<br>
outputs = train(model, num_epochs=max_epochs)</p>

ptrblck: <p>What do you mean by it doesn’t output anything?<br>
Are you now seeing any print statements at all?</p>

rai24: <p>jus the print statement not the “Epoch”,</p>
<p>print(‘Epoch:{}, Loss:{:.4f}’.format(epoch+1, float(train_loss))), nothing gets print for this line.</p>

ptrblck: <p>It sounds like your code might be hanging.<br>
If you are using multiple workers on a Windows machine, you would have to use the if-clause guard as described <a href="https://pytorch.org/docs/stable/notes/windows.html#usage-multiprocessing">here</a>.</p>
<p>Let me know, if that helps or if this is unrelated to your issue.</p>

ptrblck: <p>It seems the original code has changes a couple of times while the other issues were debugged.<br>
Based on the error message, I assume your model outputs a spatial activation in the shape <code>[batch_size, nb_classes, height, width]</code>?<br>
If that’s the case, your target should have the shape <code>[batch_size, height, width]</code> and contain class indices in the range <code>[0, nb_classes-1]</code> for <code>nn.CrossEntropyLoss</code>.</p>