here’s the code:
x_real.requires_grad_()
out = nets.discriminator(x_real, y_org)
loss_reg = r1_reg(out, x_real)
def r1_reg(d_out, x_in):
# zero-centered gradient penalty for real images
batch_size = x_in.size(0)
out =d_ out.sum().requires_grad_()
grad_dout = torch.autograd.grad(
outputs=out, inputs=x_in,
create_graph=True, retain_graph=True, only_inputs=True
)[0]
grad_dout2 = grad_dout.pow(2)
assert(grad_dout2.size() == x_in.size())
reg = 0.5 * grad_dout2.view(batch_size, -1).sum(1).mean(0)
return reg
But the error shows:
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.