I was trying out autoencoder with linear layers and parameter sharing between encoder and decoders’ convolution. When I ran the code only with convolutions it just works fine but when I introduce linear layers, after the decoder’s linear layers I get tuple index out of range while trying to run convolution transpose. The shape of index is 16x16x5x5 (bath size is 16) and so is the of output of second convolution in encoder. Thanks in advance for going through this. Below is the definition of my model and the output.
class Model(nn.Module):
    
    def __init__(self):
        
        super(Model,self).__init__()
            
        self.conv1 = nn.Conv2d(1, 16, kernel_size=5, stride=1)  
        self.conv2 = nn.Conv2d(16, 16, kernel_size=3, stride=1)
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2,return_indices=True)
        
        self.linear1 = nn.Linear(400,128)
        self.linear2 = nn.Linear(128,32)
        self.linear3 = nn.Linear(32,3)
        
        
    def encoder(self,inp):
        
        out = self.conv1(inp)
        out = F.relu(out)
        out, self.inx1 = self.pool(out)
        
        out = self.conv2(out)
        out = F.relu(out)
        out, self.inx2 = self.pool(out)           
        
        out = out.view(-1,400)
        
 
        
        out = F.relu(self.linear1(out))
   

        out = F.relu(self.linear2(out))
      
        out = self.linear3(out)
       
        return out
        
               
    def decoder(self,inp):
    
        self.copy_conv1 = to.tensor(self.conv1.weight, requires_grad=False)
        self.copy_conv2 = to.tensor(self.conv2.weight, requires_grad=False)
     
        self.copy_linear1 = to.tensor(self.linear1.weight, requires_grad=False)
        self.copy_linear2 = to.tensor(self.linear2.weight, requires_grad=False)
        self.copy_linear3 = to.tensor(self.linear3.weight, requires_grad=False)
        
      
        out = F.relu(F.linear(inp,self.copy_linear3.t()))
      
        out = F.relu(F.linear(out,self.copy_linear2.t()))
    
        out = F.relu(F.linear(out,self.copy_linear1.t()))
   
        out = out.view(-1,16,5,5)
   
        
        out = F.max_unpool2d(inp, self.inx2, kernel_size=2, stride=2)
        out = F.conv_transpose2d(out, self.copy_conv2 )
        out = F.relu(out)

                
        out = F.max_unpool2d(out, self.inx1, kernel_size=2, stride=2)
        out = F.conv_transpose2d(out, self.copy_conv1 )

        
        out = F.tanh(out)
        
        return out
        
        
    
    def forward(self,inp):
        
        out = self.encoder(inp)
        out = self.decoder(out)
        
        return out

IndexError                               Traceback (most recent call last)
<ipython-input-22-8d410874ac4e> in <module>()
      5         img, lbl = data
      6 
----> 7         out = model(img)
      8         loss = Criterion(out,img)
      9 

/usr/local/anaconda35/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

<ipython-input-20-e7fe5ca0862a> in forward(self, inp)
     79 
     80         out = self.encoder(inp)
---> 81         out = self.decoder(out)
     82 
     83         return out

<ipython-input-20-e7fe5ca0862a> in decoder(self, inp)
     61         print(self.inx2.shape)
     62 
---> 63         out = F.max_unpool2d(inp, self.inx2, kernel_size=2, stride=2)
     64         out = F.conv_transpose2d(out, self.copy_conv2 )
     65         out = F.relu(out)

/usr/local/anaconda35/lib/python3.6/site-packages/torch/nn/functional.py in max_unpool2d(input, indices, kernel_size, stride, padding, output_size)
    461     padding = _pair(padding)
    462     output_size = _unpool_output_size(input, kernel_size, stride, padding,
--> 463                                       output_size)
    464     return torch._C._nn.max_unpool2d(input, indices, output_size)
    465 

/usr/local/anaconda35/lib/python3.6/site-packages/torch/nn/functional.py in _unpool_output_size(input, kernel_size, stride, padding, output_size)
    414     for d in range(len(kernel_size)):
    415         default_size.append((input_size[d + 2] - 1) * stride[d] +
--> 416                             kernel_size[d] - 2 * padding[d])
    417     if output_size is None:
    418         return default_size

IndexError: tuple index out of range