Type conversion issue while porting from older pytorch to new version

I have used the dcgan tutorial with older versions of pytorch and python2.7. However, Now I get this error with the latest revision and python3

Starting Training Loop...
Traceback (most recent call last):
  File "main.py", line 245, in <module>
    errD_real = criterion(output, label)
  File "/home/mahmood/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/mahmood/.local/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 530, in forward
    return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
  File "/home/mahmood/.local/lib/python3.7/site-packages/torch/nn/functional.py", line 2527, in binary_cross_entropy
    input, target, weight, reduction_enum)
RuntimeError: Found dtype Long but expected Float

Part of the code is

        # Format batch
        real_cpu = data[0].to(device)
        b_size = real_cpu.size(0)
        label = torch.full((b_size,), real_label, device=device)
        # Forward pass real batch through D
        output = netD(real_cpu).view(-1)
        # Calculate loss on all-real batch
        errD_real = criterion(output, label)
        # Calculate gradients for D in backward pass

So, I would like to know how can I fix that to be compatible with the latest pytorch?

I guess your label tensor might be a LongTensor while a FloatTensor is expected.
If that’s the case, use label = torch.full(..., dtype=torch.float32) and rerun the code.