UserWarning: different input and target sizes

I’m trying to implement DCGAN on MNIST

When I start training the model, I’m getting the following warning:

/Users/maxtian/anaconda3/lib/python3.6/site-packages/torch/nn/ UserWarning: Using a target size (torch.Size([128])) that is different to the input size (torch.Size([128, 1, 1, 1])) is deprecated. Please ensure they have the same size.
  "Please ensure they have the same size.".format(target.size(), input.size()))

How can I fix this, I’m thinking it might be due to the torch.ones/torch.zeros

That’s your answer. You need to change the target size (i.e., the label size) to be 128,1,1,1

So for example,

target = target.view(-1,1,1,1)


Adding to the answer by @Ranahanocka, if you get a ‘not contiguous error’ use target.contiguous() before that line.


Thanks your answer. Can I ask you if we can fix the label size ,and change the input size (torch.Size([128, 1, 1, 1])) to be [128]
for example,
input = input.reshape(-1)
to make the torch.Size([128, 1, 1, 1]) to be [128]
and so solve the question “UserWarning: different input and target sizes”


It’s very useful, thank you all!

I think its more appropriate to change the label size here, rather than the input size.

1 Like