The code for DCGAN is here

  
      github.com
  
  
    maxmatical/pytorch-projects/blob/master/DCGAN_with_GPU_Google_Colab.ipynb 15
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "DCGAN with GPU - Google Colab.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",


  This file has been truncated. show original

  
  
    
    
  
  


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
     31 

/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)
    484 
    485     def forward(self, input, target):
--> 486         return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
    487 
    488 

/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()))
   1598 
   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?