Error in "summary(model, input_size = (100,64,64), device = 'cpu')"

Hi all,

I have the following model:

class ModelExample(torch.nn.Module):

def __init__(self):
    super(ModelExample, self).__init__()
    
    self.linear = torch.nn.Linear(100, 1024*4*4)
    
    self.conv1 = nn.Sequential(
                               nn.ConvTranspose2d(in_channels=1024, out_channels=128, kernel_size=4, stride=2, padding=1, bias=False)
                               ,nn.BatchNorm2d(128)
                               ,nn.ReLU(inplace=True)
                              )
                              
    self.conv2 = nn.Sequential(
                               nn.ConvTranspose2d(in_channels=128, out_channels=3, kernel_size=4, stride=2, padding=1, bias=False)
                              )
    self.out = torch.nn.Tanh()

def forward(self, x):
    
    x = self.linear(x)
    x = x.view(x.shape[0], 1024, 4, 4)
    
    x = self.conv1(x)
    x = self.conv2(x)

    x = self.out(x)

    return x

I tried to make the following code work:

model = ModelExample()
summary(model, input_size = (100,64,64), device = ‘cpu’)

But it gives me the following error

RuntimeError: size mismatch, m1: [12800 x 64], m2: [100 x 16384] at …\aten\src\TH/generic/THTensorMath.cpp:41

Can someone please tell me where the error is and how to solve it?
Thanks for your help.

The input that you have passed to summary is a 4D tensor of size ([batch_size, 100, 64, 64]), so before passing that directly to the linear layer, you have to reshape to a 2D Tensor. just add:

x = x.view(-1, 100) //100 = input_channels to the Linear layer

before x = self.linear(x) in the forward() and you are good to go.

@Vrushank98

Thanks a lot for your answer.