RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x3072 and 1024x2048)

username: Renata_Figueredo_Lin
cooked: <p><strong>Guys, I’m getting this error while trying to run a Vanilla Gan on the SVHN dataset using PyTorch, could someone help me?</strong></p>
<p><span class="hashtag">#dataset</span><br>
train_dataset = torchvision.datasets.SVHN(‘./data’,<br>
transform=torchvision.transforms.ToTensor(),<br>
download=True)</p>
<p><span class="hashtag">#define</span> generator<br>
class Generator(nn.Module):<br>
def <strong>init</strong>(self, latent_dims):<br>
super(Generator, self).<strong>init</strong>()<br>
self.latent_dims = latent_dims<br>
<a href="http://self.net" rel="noopener nofollow ugc">self.net</a> = nn.Sequential(<br>
nn.Linear(self.latent_dims, 256),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(256, 512),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(512, 32*32),<br>
nn.Tanh(),<br>
)<br>
def forward(self, x):<br>
return <a href="http://self.net" rel="noopener nofollow ugc">self.net</a>(x).view(-1, 1, 32, 32)</p>
<p><span class="hashtag">#define</span> discriminator<br>
class Discriminator(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Discriminator, self).<strong>init</strong>()<br>
self.n_input = 32*32</p>
<pre><code>    self.net = nn.Sequential(
        nn.Linear(self.n_input, 2048),
        nn.LeakyReLU(0.2),
        nn.Dropout(0.3),
        nn.Linear(2048, 1024),
        nn.LeakyReLU(0.2),
        nn.Dropout(0.3),
        nn.Linear(1024, 512),
        nn.LeakyReLU(0.2),
        nn.Dropout(0.3),
        nn.Linear(512, 256),
        nn.LeakyReLU(0.2),
        nn.Dropout(0.3),
        nn.Linear(256, 1),
        nn.Sigmoid(),
    )
def forward(self, x):
    x = torch.flatten(x, start_dim=1)
    return self.net(x)
</code></pre>
<p><span class="hashtag">#define</span> GAN<br>
class VanillaGAN:<br>
def <strong>init</strong>(self, latent_dims, criterion):<br>
self.latent_dims = latent_dims<br>
self.generator = Generator(latent_dims)<br>
self.discriminator = Discriminator()<br>
self.criterion = criterion<br>
self.optimizer_g = optim.Adam(self.generator.parameters(), lr=3e-4)<br>
self.optimizer_d = optim.Adam(self.discriminator.parameters(), lr=3e-4)</p>
<pre><code>def to(self, device):
    self.generator.to(device)
    self.discriminator.to(device)

def train_discriminator(self, real_data, fake_data):
    batch_size = real_data.size(0)
    real_label = torch.ones(batch_size, 1).to(device) # real labels (1s)
    fake_label = torch.zeros(batch_size, 1).to(device) # fake labels (0s)

    self.optimizer_d.zero_grad()

    output_real = self.discriminator(real_data)
    loss_real = criterion(output_real, real_label)

    output_fake = self.discriminator(fake_data)
    loss_fake = criterion(output_fake, fake_label)

    loss_real.backward()
    loss_fake.backward()
    self.optimizer_d.step()

    return (loss_real + loss_fake) * 0.5


def train_generator(self, fake_data):
    batch_size = fake_data.size(0)
    real_label = torch.ones(batch_size, 1).to(device) # real labels (1s)

    self.optimizer_g.zero_grad()

    output = self.discriminator(fake_data)
    loss = self.criterion(output, real_label)

    loss.backward()
    self.optimizer_g.step()

    return loss
</code></pre>
<p><span class="hashtag">#function</span> to create the noise vector<br>
def create_noise(sample_size, latent_dims):<br>
return torch.randn(sample_size, latent_dims).to(device)</p>
<p>def train(model, train_loader, epochs=20):<br>
log_dict = {“train_generator_loss”: [],<br>
“train_discriminator_loss”: [],<br>
“reconstructed_images”:[]}</p>
<pre><code>for epoch in tqdm.tqdm_notebook(range(epochs)):
    loss_g = 0.0
    loss_d = 0.0
    model.discriminator.train()
    model.generator.train()
    for data in tqdm.tqdm_notebook(train_loader):
        image, _ = data
        image = image.to(device)
        batch_size = len(image)
        
        # train the discriminator network
        random_noise = create_noise(batch_size, model.latent_dims)
        fake_data = model.generator(random_noise).detach() # detach() prevents from training generator
        real_data = image            
        loss_d += model.train_discriminator(real_data, fake_data)

        # train the generator network
        random_noise = create_noise(batch_size, model.latent_dims)
        fake_data = model.generator(random_noise)
        loss_g += model.train_generator(fake_data)

    log_dict["train_generator_loss"].append(loss_g.item()/len(train_loader))
    log_dict["train_discriminator_loss"].append(loss_d.item()/len(train_loader))        
        
    log_dict["reconstructed_images"].append(fake_data.to('cpu').detach()[:5])
log_dict["original_images"] = image.to('cpu').detach()[:5]

return model, log_dict
</code></pre>
<p>criterion = nn.BCELoss()<br>
latent_dims = 128</p>
<p>train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True)</p>
<p>model = VanillaGAN(latent_dims, criterion)<br>
model.to(device)<br>
model, log_dict = train(model, train_loader, epochs=300)</p>
<p><strong>Im getting this error:</strong></p>
<p>/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:6: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0<br>
Please use <code>tqdm.notebook.tqdm</code> instead of <code>tqdm.tqdm_notebook</code></p>
<p>0%<br>
0/300 [00:00&lt;?, ?it/s]<br>
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:11: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0<br>
Please use <code>tqdm.notebook.tqdm</code> instead of <code>tqdm.tqdm_notebook</code></p>
<h1>
<a name="this-is-added-back-by-interactiveshellappinit_path-1" class="anchor" href="#this-is-added-back-by-interactiveshellappinit_path-1"></a>This is added back by InteractiveShellApp.init_path()</h1>
<h2>
<a name="h-0-0573-0000-its-2" class="anchor" href="#h-0-0573-0000-its-2"></a>0%<br>
0/573 [00:00&lt;?, ?it/s]</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
6 model = VanillaGAN(latent_dims, criterion)<br>
7 model.to(device)<br>
----&gt; 8 model, log_dict = train(model, train_loader, epochs=300)</p>
<p>7 frames<br>
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/linear.py in forward(self, input)<br>
112<br>
113     def forward(self, input: Tensor) → Tensor:<br>
 → 114         return F.linear(input, self.weight, self.bias)<br>
115<br>
116     def extra_repr(self) → str:</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x3072 and 1024x2048)</p>

username: ptrblck
cooked: <p>Based on the error message and the posted code snippet it seems the shape mismatch is raised by the <code>Discriminator</code>. Print the shape of all inputs used in the <code>Discriminator</code> and make sure they have the expected 1024 input features.</p>