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

I am confused now having this error:

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

As some days ago this code run with no error at all. How is it possible?

The error happens in the following lines when I try to train my GAN

       # Train the Generator
        model.zero_grad()
        errG = criterion(fake, hr_img)

        out_Dis_G_1 = Dis(fake)
        print(out_Dis_G_1.shape)
        labels1 = torch.full((bsize, 1), 1, dtype=torch.float, device='cuda')
        **e_g = loss_clf(out_Dis_G_1, labels1)**

Although, when I have the similar lines above but about D model, there is no problem

labels0 = torch.full((bsize,1), 0, dtype=torch.float, device='cuda')

errDis_fake = loss_clf(out_Dis_G, labels0)

What are the shapes of out_Dis_G_1 and labels1? Also, what line is the error pointing to?

out_Dis_G_1 = torch.Size([5])
labels1 = torch.Size([5, 1])

eroor is here: e_g = loss_clf(out_Dis_G_1, labels1), loss_clf is nn.BCELoss()

And what happens if you make them the same shape using squeeze or unsqueeze ?

Yeah, I think this trick works !
Thank you a lot :slight_smile: