Torch throws a RuntimeError: element 0 of tensors does not require grad... but can't find where computational graph is severed
I am getting the above error:
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
I looked this up and it looks like the computational graph is not connected for some reason.  However, I cannot find the location where the graph is severed.
My code is a reproduction of the arjovsky WGAN: https://github.com/martinarjovsky/WassersteinGAN
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import torch
import torch.nn as nn

from __future__ import print_function
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable
import os
import json


class MLP_G(nn.Module):
    def __init__(self, isize, nz, ngf, ngpu):
        super(MLP_G, self).__init__()
        self.ngpu = ngpu

        main = nn.Sequential(
            # Z goes into a linear of size: ngf
            nn.Linear(nz, ngf),
            nn.ReLU(True),
            nn.Linear(ngf, ngf),
            nn.ReLU(True),
            nn.Linear(ngf, ngf),
            nn.ReLU(True),
            nn.Linear(ngf, isize),
        )
        self.main = main
        self.isize = isize
        self.nz = nz

    def forward(self, input):
        input = input.view(input.size(0), input.size(1))
        if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu &gt; 1:
            output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
        else:
            output = self.main(input)
        return output.view(output.size(0), self.isize)
class MLP_D(nn.Module):
    def __init__(self, isize, nz, ndf, ngpu):
        super(MLP_D, self).__init__()
        self.ngpu = ngpu

        main = nn.Sequential(
            # Z goes into a linear of size: ndf
            nn.Linear(isize, ndf),
            nn.ReLU(True),
            nn.Linear(ndf, ndf),
            nn.ReLU(True),
            nn.Linear(ndf, ndf),
            nn.ReLU(True),
            nn.Linear(ndf, 1),
        )
        self.main = main
        self.isize = isize
        self.nz = nz

    def forward(self, input):
        input = input.view(input.size(0),input.size(1))
        if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu &gt; 1:
            output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
        else:
            output = self.main(input)
        output = output.mean(0)
        return output.view(1)

netG = None #path to saved generator
netD = None #discriminator path
batchSize = 1000 #size of batch (which is size of data)
cuda = False
lrD = lrG = .00005
beta1 = .5
niter = 25
experiment = '/content/drive/MyDrive/savefolder'
clamp_upper = .01
clamp_lower = -clamp_upper



manualSeed = random.randint(1, 10000) # fix seed
print(&quot;Random Seed: &quot;, manualSeed)
random.seed(manualSeed)
torch.manual_seed(manualSeed)

cudnn.benchmark = True


dataset = torch.tensor(np.stack([x,y, instrument], axis = 1)).float().reshape(-1,3)

ngpu = 1
nz = 4 #three latents and the instrument
ngf = 128
ndf = 128

# custom weights initialization called on netG and netD
def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        m.weight.data.normal_(1.0, 0.02)
        m.bias.data.fill_(0)


netG = MLP_G(2, nz, ngf, ngpu)



netG.apply(weights_init)

print(netG)

netD = MLP_D(3, nz, ndf, ngpu)

print(netD)

input = torch.FloatTensor(batchSize, 2)
noise = torch.FloatTensor(batchSize, nz-1)
fixed_noise = torch.FloatTensor(batchSize, nz-1).normal_(0, 1)
one = torch.FloatTensor([1])
mone = one * -1

# setup optimizer

optimizerD = optim.Adam(netD.parameters(), lr=lrD, betas=(beta1, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=lrG, betas=(beta1, 0.999))

real_cpu = data = dataset


gen_iterations = 0
for epoch in range(niter):
    #data_iter = iter(dataloader)
    ############################
    # (1) Update D network
    ###########################
    for p in netD.parameters(): # reset requires_grad
        p.requires_grad = True # they are set to False below in netG update

    # train the discriminator Diters times
    if gen_iterations &lt; 25 or gen_iterations % 500 == 0:
        Diters = 100
    else:
        Diters = 5
    j = 0
    while j &lt; Diters:
        j += 1

        # clamp parameters to a cube
        for p in netD.parameters():
            p.data.clamp_(clamp_lower, clamp_upper)

        

        # train with real
        netD.zero_grad()

        if cuda:
            real_cpu = real_cpu.cuda()
        input.resize_as_(real_cpu).copy_(real_cpu)
        inputv = Variable(input, requires_grad=False)

        errD_real = netD(inputv)
        errD_real.backward(one)#Error Occurs here

        # train with fake
        noise.resize_(batchSize, nz-1).normal_(0, 1)
        noisev = torch.cat([Variable(noise, requires_grad=False), dataset[:,2].reshape(-1,1)], 1)# totally freeze netG
        fake = torch.cat([Variable(netG(noisev).data), dataset[:,2].view(-1,1)], 1)
        inputv = fake
        errD_fake = netD(inputv)
        errD_fake.backward(mone)
        errD = errD_real - errD_fake
        optimizerD.step()

        ############################
        # (2) Update G network
        ###########################
        for p in netD.parameters():
            p.requires_grad = False # to avoid computation
        netG.zero_grad()
        # in case our last batch was the tail batch of the dataloader,
        # make sure we feed a full batch of noise
        noise.resize_(batchSize, nz-1).normal_(0, 1)
        noisev = torch.cat([Variable(noise), dataset[:,2].view(-1,1)], 1)
        fake = torch.cat([netG(noisev), dataset[:,2].view(-1,1)], 1)
        errG = netD(fake)
        errG.backward(one)
        optimizerG.step()
        gen_iterations += 1
        
        i = 0

        print('[%d/%d][%d] Loss_D: %f Loss_G: %f Loss_D_real: %f Loss_D_fake %f'
            % (epoch, niter, gen_iterations,
            errD.data[0], errG.data[0], errD_real.data[0], errD_fake.data[0]))
        # if gen_iterations % 500 == 0:
        #     real_cpu = real_cpu.mul(0.5).add(0.5)
        #     vutils.save_image(real_cpu, '{0}/real_samples.png'.format(opt.experiment))
        #     fake = netG(Variable(fixed_noise, volatile=True))
        #     fake.data = fake.data.mul(0.5).add(0.5)
        #     vutils.save_image(fake.data, '{0}/fake_samples_{1}.png'.format(opt.experiment, gen_iterations))

    # do checkpointing
    torch.save(netG.state_dict(), '{0}/netG_epoch_{1}.pth'.format(experiment, epoch))
    torch.save(netD.state_dict(), '{0}/netD_epoch_{1}.pth'.format(experiment, epoch))




Error occurs on the line: errD_real.backward(one).  The error might be something regarding zeroing out the computational graph as the code runs for one iteration then throws an error.  Thanks for your help.

You most certainly need to add require_grad=True on one. You could define it as:
one = torch.tensor([1], dtype=torch.float16, requires_grad=True)

