@ptrblck
class Autoencoder(nn.Module):
def __init__(self):
    super(Autoencoder, self).__init__()
    self.encoder = nn.Sequential(
       # nn.Dropout2d(dropout_p= 0.5),
        nn.Conv2d(1, 16,kernel_size=3,stride =1, padding= 1,bias= False),  
        nn.ReLU(True),
        nn.MaxPool2d( 2, 2),  
        nn.Conv2d(16,32,kernel_size=3,stride =1, padding=1),  
        nn.ReLU(True),
        #nn.MaxPool2d(2, 2),  
        nn.Conv2d(32,64,kernel_size=3,stride =1, padding= 1),  
        nn.ReLU(True),
       # nn.Linear(64 * 3 *3, 120)
        #nn.MaxPool2d(2, 2),  
        
    )
    self.decoder = nn.Sequential(
        nn.MaxUnpool2d(2, 2),
        nn.ConvTranspose2d(64, 32, kernel_size=3), 
        nn.ReLU(True),
        nn.MaxUnpool2d(2, 2),
        nn.ConvTranspose2d(32, 16, kernel_size=3),  
        nn.ReLU(True),
        nn.MaxUnpool2d(2, 2),
        nn.ConvTranspose2d(16, 8, kernel_size=3),
        
        
        #nn.Tanh()
    )

def forward(self, x):
    x = self.encoder(x)
    encoded_x = x
    x = self.decoder(x)
    print(x.shape)
    x = x.view(-1, 64 * 3 * 3)
    x = F.log_softmax(self.fc2(x), dim=0)
    return x, encoded_x

num_epochs = 5
learning_rate = 1e-5
model= Autoencoder()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
for data in loader_train_set:
    
    img, label = data
    print(img.size())
    img = torch.tensor(img)
    labels = labels.view(-1)
    img = img[:, 0:1]
    encoded_output = model.encoder(img)
    
    print(encoded_output.size())
    output_of_net = torch.rand(20, 2, 240, 320)
    label = torch.LongTensor(20,240, 320)

    output_of_net_linearized = output_of_net.permute(0, 2, 3, 1).reshape(-1, output_of_net.size(1))
    label_linearized = label.view(-1)
    loss = criterion(output_of_net_linearized, label_linearized)
    
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()



print('epoch [{}/{}], loss:{:.4f}'.format(epoch+1, num_epochs, loss.data()))

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
I tried making few changes, but now I encountered the above mentioned error.