Hi,
I have this model architecture:
import torch
import torch.nn as nn
nz = 100 # Size of the latent vector
class netG(nn.Module):
def init(self, nz, ngf, nc):
super(netG, self).init()
self.main = nn.Sequential(
# Input: (nz, 1, 1)
nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0, bias=False),
nn.LayerNorm([ngf * 8, 4, 4]),
nn.ReLU(True),
# Output: (ngf * 8, 4, 4)
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.LayerNorm([ngf * 4, 8, 8]),
nn.ReLU(True),
# Output: (ngf * 4, 8, 8)
nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.LayerNorm([ngf * 2, 16, 16]),
nn.ReLU(True),
# Output: (ngf * 2, 16, 16)
nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False),
nn.LayerNorm([ngf, 32, 32]),
nn.ReLU(True),
# Output: (ngf, 32, 32)
nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
# Output: (nc, 64, 64)
)
def forward(self, input):
print(f"Input shape: {input.shape}")
x = self.main(input)
print(f"Output shape: {x.shape}")
return x
class netD(nn.Module):
def init(self, ndf, nc, nb_label):
super(netD, self).init()
self.main = nn.Sequential(
# Input: (nc, 64, 64)
nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout2d(0.25),
# Output: (ndf, 32, 32)
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.GroupNorm(num_groups=4, num_channels=ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout2d(0.25),
# Output: (ndf * 2, 16, 16)
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.GroupNorm(num_groups=8, num_channels=ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout2d(0.25),
# Output: (ndf * 4, 8, 8)
nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
nn.GroupNorm(num_groups=16, num_channels=ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout2d(0.25),
# Output: (ndf * 8, 4, 4)
nn.Conv2d(ndf * 8, ndf, 4, 1, 0, bias=False),
# Output: (ndf, 1, 1)
nn.Flatten(),
nn.Linear(ndf, 1),
nn.Sigmoid(),
nn.Linear(ndf, nb_label),
nn.Softmax(dim=1)
)
def forward(self, input):
print(f"Input shape: {input.shape}")
x = self.main(input)
output = x[:, :1]
aux_output = x[:, 1:]
print(f"Output shape: {output.shape}")
print(f"Aux output shape: {aux_output.shape}")
return output, aux_output
def weights_init(m):
classname = m.class.name
if classname.find(‘Conv’) != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find(‘BatchNorm’) != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
elif classname.find(‘GroupNorm’) != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
elif classname.find(‘Linear’) != -1:
m.weight.data.normal_(0.0, 0.02)
m.bias.data.fill_(0)
and I am getting this error:
Traceback (most recent call last):
File “/root/cifar10_experiments/serveradv.py”, line 181, in
server.run()
File “/root/cifar10_experiments/serveradv.py”, line 122, in run
client_weights_d, client_weights_g, _, _ = client.client_training()
File “/root/cifar10_experiments/clientadv.py”, line 111, in client_training
perturbed_data = fgsm_attack(self.discriminator, img, real_labels, 0.01, self.s_criterion, attack_output_idx=0)
File “/root/cifar10_experiments/adversarial_utils.py”, line 22, in fgsm_attack
outputs = model(data)
File “/root/cifar10_experiments/venv/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1532, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File “/root/cifar10_experiments/venv/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1541, in _call_impl
return forward_call(*args, **kwargs)
File “/root/cifar10_experiments/model_GAN3.py”, line 82, in forward
x = self.main(input)
File “/root/cifar10_experiments/venv/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1532, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File “/root/cifar10_experiments/venv/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1541, in _call_impl
return forward_call(*args, **kwargs)
File “/root/cifar10_experiments/venv/lib/python3.10/site-packages/torch/nn/modules/container.py”, line 217, in forward
input = module(input)
File “/root/cifar10_experiments/venv/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1532, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File “/root/cifar10_experiments/venv/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1541, in _call_impl
return forward_call(*args, **kwargs)
File “/root/cifar10_experiments/venv/lib/python3.10/site-packages/torch/nn/modules/linear.py”, line 116, in forward
return F.linear(input, self.weight, self.bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (16x1 and 64x10)