Search code examples
pytorchgenerative-adversarial-network

mat1 and mat2 shapes cannot be multiplied (1x110 and 100x256)


I am trying to build a GAN which accepts a label to generate new image.

class Generator(nn.Module):
       def __init__(self):
           super(Generator, self).__init__()
           self.fc1 = nn.Linear(100, 256)
           self.fc2 = nn.Linear(256, 512)
           self.fc3 = nn.Linear(512, 1024)
           self.fc4 = nn.Linear(1024, 784)

       def forward(self, x):
           x = F.relu(self.fc1(x))
           x = F.relu(self.fc2(x))
           x = F.relu(self.fc3(x))
           x = torch.tanh(self.fc4(x))
           return x
   

# set label
   label = 3

   # create one-hot encoded vector
   one_hot = torch.zeros(1, 10)
   one_hot[0][label] = 1

   # set noise vector
   noise = torch.randn(1, 100)

   # concatenate label and noise
   noise_with_label = torch.cat([one_hot, noise], dim=1)

   # pass through generator
   generated_image = generator(noise_with_label)

But its throwing :

    112 
    113     def forward(self, input: Tensor) -> Tensor:
--> 114         return F.linear(input, self.weight, self.bias)
    115 
    116     def extra_repr(self) -> str:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x110 and 100x256)

I am using MNIST dataset.

I tried to resolve it, but couldn’t find a way to fix it.


Solution

  • Original comment by @jodag helped to fix the issue:

    The input you're providing to your model has shape [1,110] because you're concatenating noise (shape [1,100]) and one_hot (shape [1,10]). But the first layer expects inputs of shape [..., 100]. One way to get rid of the error would be to change the first layer of the model to self.fc1 = nn.Linear(110, 256). An alternative would be to define noise as noise = torch.randn(1, 90)