def calculate_gradient_penalty(self, real_sens, fake_sens):
        eta = torch.FloatTensor(opt.BATCH_SIZE, opt.MAX_SEQ_LEN).uniform_(0,1)

        if self.cuda:
            eta = eta.cuda()
        # CUDALongTensor--->LongTensor--->FloatTensor--->CUDAFloatTensor
        new_real_sens = real_sens.cpu().type(torch.FloatTensor).cuda()
        new_fake_sens = fake_sens.cpu().type(torch.FloatTensor).cuda()
        interpolated = eta * new_real_sens + ((1 - eta) * new_fake_sens) # CUDAFloatTensor
        # calculate probability of interpolated examples
        prob_interpolated = self.D(interpolated.cpu().type(torch.LongTensor).cuda()) # batch_size, 1 (LongTensor)
        # define it to calculate gradient
        interpolated = Variable(interpolated, requires_grad = True)
        # calculate gradients of probabilities with respect to examples
        gradients = torch.autograd.grad(outputs=prob_interpolated, inputs=interpolated,
                                        grad_outputs=torch.ones(prob_interpolated.size()).cuda(),
                                        create_graph=True, retain_graph=True)
        grad_penalty = ((gradients.norm(2, dim=1)-1) ** 2).mean() * self.lambda_term
        return grad_penalty


When I write the calculate_gradient_penalty function of WGAN-GP, but I got the following problem:
“RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.”
I search the similar problem that has exists in other man’s work, the cause is that they use  ‘.detach()’  or  ‘.data’  or ‘.numpy()’  somewhere in the chain. After investigation, I don’t have this problem. So I want to know what the causes of problem I am facing?