DCGAN error: Target and input must have the same number of elements. target nelement (128) != input nelement (96)

The code for DCGAN is here

The error message is

Epoch [1/20], Step [96/469], d_loss: 0.5494, g_loss: 5.2465, D(x): 0.95, D(G(z)): 0.23
Epoch [1/20], Step [196/469], d_loss: 1.2054, g_loss: 5.1388, D(x): 0.86, D(G(z)): 0.54
Epoch [1/20], Step [296/469], d_loss: 1.2517, g_loss: 4.7196, D(x): 0.91, D(G(z)): 0.61
Epoch [1/20], Step [396/469], d_loss: 1.4117, g_loss: 0.7615, D(x): 0.39, D(G(z)): 0.06
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1594: UserWarning: Using a target size (torch.Size([128, 1, 1, 1])) that is different to the input size (torch.Size([96, 1, 1, 1])) is deprecated. Please ensure they have the same size.
  "Please ensure they have the same size.".format(target.size(), input.size()))
ValueError                                Traceback (most recent call last)
<ipython-input-32-38a5a48d3f53> in <module>()
     27         # loss for real images
     28         outputs = D(images).view(-1,1,1,1)
---> 29         d_loss_real = criterion(outputs, real_labels)
     30         real_score = outputs

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
    485     def forward(self, input, target):
--> 486         return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in binary_cross_entropy(input, target, weight, size_average, reduce, reduction)
   1595     if input.nelement() != target.nelement():
   1596         raise ValueError("Target and input must have the same number of elements. target nelement ({}) "
-> 1597                          "!= input nelement ({})".format(target.nelement(), input.nelement()))
   1599     if weight is not None:

ValueError: Target and input must have the same number of elements. target nelement (128) != input nelement (96)

It seems like it has something to do with the batch size? Since I set the batch size to 128. But I haven’t encountered any errors similar to this before.

Update: it seems that when I change the batch size to something smaller (i.e. 32) the code runs fine with no errors. Can anyone suggest a reason as to why this is happening?

Probably your number of samples is not not divisible by the batch_size without a remainder, which might yield a smaller number of samples for the last batch.
As you create real_labels using the batch_size you could have a size mismatch for the last batch:

real_labels = torch.ones(bs).view(-1,1,1,1)

You could just keep the code and get rid of the last (smaller) batch using drop_last=True in your DataLoader.
Alternatively, you could create your labels using the current number of samples in the batch instead of the global batch_size:

real_labels = torch.ones(images.size(0)).view(-1,1,1,1)