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

class autoencoder(nn.Module):

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

model = autoencoder()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.005,
weight_decay=1e-5)

for epoch in range(2):
for i, data in enumerate(traindataloader,0):

    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

print(‘Finished Training’)

Please help!

self.encoder uses a nn.Conv2d as the first layer, which expects an input with the shape [batch_size, channels, height, width], while you are flattening your input tensor in the first step via x = x.view(x.size(0), -1).

If you remove the view op, the first layers will run, but you will run into another shape mismatch error passing the activation to the nn.Linear layer.
Add an nn.Flatten() module before the first nn.Linear and rerun the code again.

Thank you so so much, I did tried that but now I am getting below error
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

Code:

class autoencoder(nn.Module):

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

model = autoencoder()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.005,
weight_decay=1e-5)

for epoch in range(2):
for i, data in enumerate(traindataloader,0):

    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

print(‘Finished Training’)

You have to add nn.Flatten() before the first linear layer in your encoder.

It did that it is still showing me the below error:
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

Do, I have to make changes in the values added in the Linear layer

It seems so.
Based on the input shape, you would have to adapt the number of input features of this linear layer.

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

What input shape are you using?
You could calculate it based on the convolution parameters (kernel size, stride, padding, dilation).

torch.Size([20, 1, 240, 320])

The first linear layer in your encoder should be nn.Linear(16*114*154, 2).

isn’t 2 the small number, how we choose this

and don’t we have reduce the size to half because of maxpooling

The 2 was just for debugging purposes to not flood my machine, so please reset it to your value.

I did. Do you see any errors using this shape?

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
SO for that I updated the line of code as below:
loss = criterion(output, labels.squeeze())
The code is on Training since an half an hour,
can you please provide me solution for decreasing the training time and “1D target tensor expected, multi-target not supported” error.
I greatly appreciate your help

The shape error is thrown, as for a multi-class classification nn.CrossEntropyLoss expects the target to be a LongTensor in the shape [batch_size] containing the class indices.
I assume you passed labels as a [batch_size, 1] tensor, which will raise this error.
Using labels.squeeze(1) should work.

The training time depends highly on your model, machine, libs etc.
I would recommend to use the latest PyTorch version as well as updated libraries (CUDA, cudnn etc.).
Also, make sure to use multiple workers in your DataLoader (via num_workers).

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.
class autoencoder(nn.Module):

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

def train(model, num_epochs=5, batch_size=10, learning_rate=1e-3):
torch.manual_seed(42)
criterion = nn.CrossEntropyLoss() # mean square error loss
optimizer = torch.optim.Adam(model.parameters(),
lr=learning_rate,
weight_decay=1e-5) # <–

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

model = autoencoder()
max_epochs = 5
outputs = train(model, num_epochs=max_epochs)

What do you mean by it doesn’t output anything?
Are you now seeing any print statements at all?

jus the print statement not the “Epoch”,

print(‘Epoch:{}, Loss:{:.4f}’.format(epoch+1, float(train_loss))), nothing gets print for this line.

It sounds like your code might be hanging.
If you are using multiple workers on a Windows machine, you would have to use the if-clause guard as described here.

Let me know, if that helps or if this is unrelated to your issue.

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