ValueError: Using a target size (torch.Size([16, 1])) that is different to the input size (torch.Size([3, 512, 512])) is deprecated. Please ensure they have the same size.
This error is usually raised in nn.BCEWithLogitsLoss
is a shape mismatch between the target and model output is detected.
The current error also indicates that the number of elements in these tensors is different so you would have to check how the loss should be calculated given these different tensors.
Thank you it has worked now am having
RuntimeError: mat1 and mat2 shapes cannot be multiplied (2x786432 and 100x8192) when i run my training module
This error is most likely raised in a linear layer so you would need to make sure the activation tensor is flattened to the desired shape and then adapt the in_features
of the linear layer if needed. Based on the error message you might need to change the number of input features from 100
to 786432
.
##########################
MODEL
##########################
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.conv = nn.Sequential(
#
# input size color_channels x image_height x image_width
#
nn.Conv2d(3, 32,
kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(32),
nn.LeakyReLU(inplace=True),
#
# size: num_feat_maps_dis x 32 x 32
#
nn.Conv2d(32, 64,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(64),
nn.LeakyReLU(inplace=True),
#
# size: num_feat_maps_dis*2 x 16 x 16
#
nn.Conv2d(64, 128,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(inplace=True),
nn.Conv2d(128, 512,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(inplace=True),
#
# size: num_feat_maps_dis*4 x 8 x 8
#
)
self.fc = nn.Sequential(
# reshape input, 128 -> 1
nn.Linear(512, 1),
nn.Sigmoid(),
#
# size: num_feat_maps_dis*8 x 4 x 4
#
#nn.Conv2d(4096, 1,
# kernel_size=4, stride=1, padding=0),
# size: 1 x 1 x 1
nn.Flatten(),
)
def forward(self, x):
y = self.conv(x)
y = y.view(y.size(0), -1)
fc = self.fc
y = self.fc(y)
return y
class Generator(nn.Module):
def __init__(self, input_size=524288):
super(Generator, self).__init__()
self.fc = nn.Sequential(
nn.Linear(input_size, 4*4*32),
nn.ReLU(),
)
self.conv = nn.Sequential(
nn.ConvTranspose2d(input_size, 512,
kernel_size=4, stride=1, padding=0,
bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(inplace=True),
#
# size if latent_dim=100: num_feat_maps_gen*8 x 4 x 4
#
nn.ConvTranspose2d(512, 128,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(inplace=True),
#
# size if latent_dim=100: num_feat_maps_gen*4 x 8 x 8
#
nn.ConvTranspose2d(128, 64,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(64),
nn.LeakyReLU(inplace=True),
#
# size if latent_dim=100: num_feat_maps_gen*2 x 16 x 16
#
nn.ConvTranspose2d(64, 32,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(32),
nn.LeakyReLU(inplace=True),
#
# size if latent_dim=100: num_feat_maps_gen x 32 x 32
#
nn.ConvTranspose2d(32,3,
kernel_size=4, stride=2, padding=1,
bias=False),
#
# size: color_channels x 64 x 64
#
nn.Tanh(),
)
def forward(self, x):
x = x.view(x.size(0), -1)
y = self.fc(x)
y = y.view(y.size(0),32,4,4 )
y = self.conv(y)
return y
When i run that i run out memory and it crashes and still get that error below
RuntimeError: mat1 and mat2 shapes cannot be multiplied (16x524288 and 512x1)
@ptrblck when you say flatten to the desired shape and then adapt the in_features
of the linear layer if needed,could you elaborate for me from my shared code i dont understand what you meant,thank you
I don’t think your current code fixes the original issue, as you’ve changed the in_channels
of the transposed conv layer to 524288
, which would create a huge weight
parameter of 16GB.
Could you post a minimal, executable code snippet which reproduces the error before you’ve tried to fix it, please?
okay let me change it back to 512*512 and share
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(3, 512,
kernel_size=4, stride=2, padding=1),
#nn.BatchNorm2d(32),
nn.LeakyReLU(inplace=True),
nn.Conv2d(512, 1024,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(1024),
nn.LeakyReLU(inplace=True),
nn.Conv2d(1024, 2048,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(2048),
nn.LeakyReLU(inplace=True),
nn.Conv2d(2048, 4096,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(4096),
nn.LeakyReLU(inplace=True),
nn.Conv2d(4096, 1,
kernel_size=4, stride=1, padding=0),
)
self.fc = nn.Sequential(
nn.Linear(512, 1),
)
def forward(self, x):
y = self.conv(x)
y = y.view(y.size(0), -1)
fc = self.fc
y = self.fc(y)
return y
class Generator(nn.Module):
def __init__(self, input_size=100):
super(Generator, self).__init__()
self.fc = nn.Sequential(
nn.Linear(input_size, 4*4*512),
nn.ReLU(),
)
self.conv = nn.Sequential(
nn.ConvTranspose2d(input_size, 4096,
kernel_size=4, stride=1, padding=0,
bias=False),
nn.BatchNorm2d(4096),
nn.LeakyReLU(inplace=True),
nn.ConvTranspose2d(4096, 2048,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(2048),
nn.LeakyReLU(inplace=True),
nn.ConvTranspose2d(2048, 1024,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(1024),
nn.LeakyReLU(inplace=True),
nn.ConvTranspose2d(1024, 512,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(inplace=True),
#
#
nn.ConvTranspose2d(512,3,
kernel_size=4, stride=2, padding=1,
bias=False),
nn.Tanh(),
)
def forward(self, x):
x = x.view(x.size(0), -1)
y = self.fc(x)
y = y.view(y.size(0),512,4,4 )
y = self.conv(y)
return y
the is the code i had before i tried to fix the issue now when i run it again i run out of memory
Your code works fine if you fix the shape mismatch in Discriminator.fc
to have in_features=16
:
g = Generator(input_size=512)
d = Discriminator()
x = torch.randn(2, 512)
out = g(x)
out = d(out)
print(out.shape)
# torch.Size([2, 1])