Hi,
You can get the gradient for a given tensor by doing x.register_hook(hook_fn). You hook_fn will be called with the gradient of x when it is computed. You can then save it wherever you want.

how to freeze the weights and not an input_to_each layer?

I am not sure what you mean by that. Could you describe in more details what you’re trying to accomplish?