For context, I’m trying to implement the Creative Adversarial Network (a GAN) in Pytorch. This GAN’s discriminator sends 2 signals, the normal image is real or image is fake, as well as how easily it can classify the image. Thus, there would be 2 outputs in the discriminator’s forward() method.
Unfortunately, l’m now running into input size mismatches when trying to calculate multilabel_soft_margin_loss.
I get the following error : ValueError: Target and input must have the same number of elements. target nelement (64) != input nelement (1024)
This is my model’s structure
num_disc_filters = 64
self.conv = nn.Sequential(
nn.Conv2d(channels, num_disc_filters, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(num_disc_filters, num_disc_filters * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(num_disc_filters * 2),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(num_disc_filters * 2, num_disc_filters * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(num_disc_filters * 4),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(num_disc_filters * 4, num_disc_filters * 16, 4, 2, 1, bias=False),
nn.BatchNorm2d(num_disc_filters * 16),
nn.LeakyReLU(0.2, inplace=True),
)
self.final_conv = nn.Conv2d(num_disc_filters * 16, 1, 4, 1, 0, bias=False)
self.sig = nn.Sigmoid()
self.real_or_fake = nn.Linear(num_disc_filters*16,1)
self.fc = nn.Sequential()
self.fc.add_module("linear_layer.{0}".format(num_disc_filters*16),nn.Linear(num_disc_filters*16,num_disc_filters*8))
self.fc.add_module('relu.{0}'.format(num_disc_filters*16), nn.LeakyReLU(0.2, inplace=True))
self.fc.add_module("linear_layer.{0}".format(num_disc_filters*8),nn.Linear(num_disc_filters*8,num_disc_filters))
self.fc.add_module('relu.{0}'.format(num_disc_filters), nn.LeakyReLU(0.2, inplace=True))
self.fc.add_module('softmax',nn.Softmax())
self.fc.add_module("linear_layer.{0}".format(num_disc_filters),nn.Linear(num_disc_filters,1))
self.fc.add_module('relu.{0}'.format(num_disc_filters), nn.LeakyReLU(0.2, inplace=True))
self.fc.add_module('softmax',nn.Softmax())
def forward(self, inp):
x = self.conv.forward(inp)
real = self.final_conv(x)
x = x.view(-1,x.size(1))
print(x.size(),"xs") # torch.Size([1024, 1024])
real_out = self.sig.forward(real)
real_out = real_out.view(-1,1).squeeze(1)
style = self.fc.forward(x)
print(style.size(),"style size") # torch.Size([1024, 1]) style size
return real_out,style
style
is then assigned to output_styles
, and used in the following line:
err_disc_style = criterion_style(output_styles, style_labels)
where criterion_style
is nn.MultiLabelSoftMarginLoss()
and style_labels
is a 64x1 Tensor of class labels (ints).
Why is style’s size 1024x1 instead of 64x1?
Surely self.fc
should ensure this with the line self.fc.add_module("linear_layer.{0}".format(num_disc_filters),nn.Linear(num_disc_filters,1))
?
Any help would be greatly appreciated - thanks in advance!