Hi,


Hooks can allow you to get gradients wrt to intermediate results in the forward, not just the weights/biases.


For backward hooks, you should only use the Tensor version right now (the nn.Module version is not working properly at the moment).
To you a module hook for the output of cnn1 for example, you would modify your forward method as follow:


# Defined somewhere else
def my_hook(grad):
    # Do what you want with the grad


# In your model definition
    def forward(self, x):
        x = self.cnn1(x)
        x.register_hook(my_hook)
        x = self.cnn2(x)
        out = self.cnn_out(x)

        return out


As in the example above.