Hi,
You can use autograd for this.
gradients = torch.autograd.grad(outputs=activations, inputs=input,
                          grad_outputs=torch.ones(activations.size()).to(device),
                          create_graph=True, retain_graph=True, only_inputs=True)[0]

Related question here. I’d like to compute the gradient of activations wrt inputs for several layers inside the network. So far, I’ve built several intermediate models to compute the gradients of the network output wrt input. I’m sure it can done more efficiently using hooks, but I don’t know how.
Any help ?
Thanks.