bentrevett/pytorch-sentiment-analysis

Using a target size (torch.Size([64, 1])) that is different to the input size (torch.Size([304800, 1])) is deprecated. Please ensure they have the same size

KeerthiKrishna97 opened this issue · 7 comments

Hi, I am new to pytorch. There are 762 images in my dataset, each of size (1,160,160). I am trying to build a GAN and the code for Generator and Discriminator class is as follows:

batch_size = 64
sample_size = 64
nz = 128

class Generator(nn.Module):
def init(self, nz):
super(Generator, self).init()
self.nz = nz
self.main = nn.Sequential(
nn.Flatten(),
nn.Linear(self.nz, 256),
nn.LeakyReLU(0.2),

        nn.Linear(256, 512),
        nn.LeakyReLU(0.2),

        nn.Linear(512, 1024),
        nn.LeakyReLU(0.2),

        nn.Linear(1024, batch_size),
        nn.Tanh(),
    )

def forward(self, x):
    return self.main(x).view(-1, 1, 8, 8)

class Discriminator(nn.Module):
def init(self):
super(Discriminator, self).init()
self.n_input = batch_size
self.main = nn.Sequential(
nn.Flatten(),
nn.Linear(self.n_input, 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 = x.view(-1, batch_size)
    return self.main(x)

generator = Generator(nz).to(device)
discriminator = Discriminator().to(device)

But when I try to train it, it gives the following error:
Using a target size (torch.Size([64, 1])) that is different to the input size (torch.Size([304800, 1])) is deprecated. Please ensure they have the same size.
I don't understand where the 304800 came from. Could anyone help me solve the issue?

Your error is in the line nn.Linear(1024, batch_size).

I believe this should be nn.Linear(1024, 8*8) as the generator is trying to output 8x8 images.

Also, the line x = x.view(-1, batch_size) is an error. It should be x = x.view(batch_size, -1).

Your error is in the line nn.Linear(1024, batch_size).

I believe this should be nn.Linear(1024, 8*8) as the generator is trying to output 8x8 images.

Hello. Here 8*8 = 64 is the batch_size

Your error is in the line nn.Linear(1024, batch_size).
I believe this should be nn.Linear(1024, 8*8) as the generator is trying to output 8x8 images.

Hello. Here 8*8 = 64 is the batch_size

Right, but nn.Linear(1024, batch_size) means it takes in a [batch size, 1024] tensor and outputs a [batch size, batch size] tensor, when it should be a [batch size, channels * height * width] tensor and then the last dimension should be reshaped.

Okay. I have done that and now the error is : mat1 and mat2 shapes cannot be multiplied (64x304800 and 64x1024)

Which line is giving you that error? Tensor shape mismatching is a common bug in deep learning models, and the best way to solve it by making sure the shapes output by each layer is what you expect by printing out the tensor shapes out by each layer and seeing if it matches with what you expect it to be.

Your error is in the line nn.Linear(1024, batch_size).

I believe this should be nn.Linear(1024, 8*8) as the generator is trying to output 8x8 images.

I believe the error is here. The output of nn.Linear of Generator shouldn't be batch size. As you said, it should be channels × height× width. For my images, it is 1 × 160× 160, which is quite large to be an output.