How can I get the gradient of each pixels in GANs?


I have DCGAN model which generate 64x64 image for MNIST.

I would like to calculate d x_{i,j} / d theta.

So, try to calculate each pixels’ gradient.

What I tried was,

for image channel, width, height:
#Hooking only for Conv2d layers
                for name, param in netG.main.named_parameters():
                    if ('0' in name and '10' not in name or '3' in name or '6' in name or '9' in name or '12' in name):
                        param.register_hook(lambda grad: grad_list.append(grad.view(-1)))

#Try to calculate gradient
                fake[0,channel,j,k].backward(retain_graph = True)


This code made an error. CUDA out of memory.

How can I get this?