how could i detect which tensors are still connected to the computation graph?
So i have printed after each process the time.time() and i have found that:

In the first epoch it takes 1 second to do the discriminator backpropagation and zero for generator backpropagation.
After 28 epoch it takes 3 second to do the discriminator backpropagation and 4 second for generator backpropagation.
Actually i use loss.item() while storing in tensorboard does this make a difference?

 for epoch in range(opt.niter):
    print ("epoch||||||||||||||||", time.time())
    for i, batch in enumerate(dataloader,0):
        data_time = AverageMeter('Data', ':6.3f')
        print("data_time",data_time)
        print ("iteration============", time.time())
        for iter in range(3):
            print ("discriminator_iteration", time.time())
            f = open(work_dir+"training_curve.csv", "a")
            autograd.detect_anomaly()       
            ############################
            # (1) Update D network
            ############################
            ### PART ---1--- training the discriminator on real data ####
            optimizerD.zero_grad()
            x = Variable(batch.type(Tensor))
            noise = Variable(Tensor(batch.size(0), nz, 1, 1, 1).normal_(0, 1))
            x_tilde = Variable(netG(noise), requires_grad=True)
            epsilon = Variable(Tensor(batch.size(0), 1, 1, 1, 1).normal_(0, 1))
            x_hat = epsilon*x + (1 - epsilon)*x_tilde
            x_hat = torch.autograd.Variable(x_hat, requires_grad=True)
            dw_x = netD(x_hat)
        
            grad_x = torch.autograd.grad(outputs=dw_x, inputs=x_hat,
                                        grad_outputs=Variable(Tensor(dw_x.size()).fill_(1.0), requires_grad=False),
                                        create_graph=True, retain_graph=True, only_inputs=True)

            grad_x = grad_x[0].view(batch.size(0), -1)
            grad_x = grad_x.norm(p=2, dim=1)
            d_loss = torch.mean(netD(x_tilde)) - torch.mean(netD(x)) + LAMBDA*torch.mean((grad_x - 1)**2) 
            gradient_penal=LAMBDA*torch.mean((grad_x - 1)**2)
            wgan_loss=torch.mean(netD(x_tilde)) - torch.mean(netD(x))
            print ("before back discriminator|||||||||||||||", time.time())
            d_loss.backward()
            print ("after back discriminator==============", time.time())
            for p in netD.parameters():
                p.register_hook(lambda grad: torch.clamp(grad, -clip_value, clip_value))
            print ("after gradient clipping|||||||||||", time.time())
            for p in list(filter(lambda p: p.grad is not None, netD.parameters())):
                print("Discriminator",p.grad.data.norm(2).item())
            optimizerD.step()
        ###########################
        # (2) Update G network
        ###########################
        optimizerG.zero_grad()
        print ("before generator iter||||||||||||", time.time())
        noise = Variable(Tensor(batch.size(0), nz, 1, 1,1).normal_(0, 1))
        imgs_fake = netG(noise)
        g_loss = -torch.mean(netD(imgs_fake)) ##minimiser
        print ("before back generator==========", time.time())
        g_loss.backward()
        print ("after back generator||||||||||||", time.time())
        for p in netG.parameters():
            p.register_hook(lambda grad: torch.clamp(grad, -clip_value, clip_value))
        for p in list(filter(lambda p: p.grad is not None, netG.parameters())):
            print("Generator",p.grad.data.norm(2).item())
        print ("befor weight update generateur===========", time.time())
        optimizerG.step()
       ## TensorBoard
        writer.add_scalar('G_loss', g_loss.item(), i + epoch * len(dataloader))
        writer.add_scalar('D_loss', d_loss.item(), i + epoch * len(dataloader))
        writer.add_scalar('gradient_penal', gradient_penal.item(), i + epoch * len(dataloader))
        writer.add_scalar('wgan_loss', wgan_loss.item(), i + epoch * len(dataloader))
        writer.add_scalar('fake_loss', fake_loss.item(), i + epoch * len(dataloader))
        writer.add_scalar('real_loss', real_loss.item(), i + epoch * len(dataloader))
        f.write('[%d/%d][%d/%d] Loss_D: %.4f Gradient_p: %.4f WGAN_loss: %.4f Loss_G: %.4f'
            % (epoch, opt.niter, i, len(dataloader),
                d_loss.data, gradient_penal.data, wgan_loss.data, g_loss.data))
        f.write('\n')
        f.close()