Hi,
I am quite confuse about your code, and not sure to understand what it is trying to do.

register_backward_hook() allows you to specify a function that will be called just after the backward pass of a nn.Module has been done. It will provide you with the gradients used as parameter for the backward (the derivative of your loss wrt the output of your Module) and the gradients returned by the backward (the derivative of your loss wrt the input of your Module).

network.parameters() will return all the nn.Parameters contained in the nn.Module (and its children). so [x.grad.data for x in network.parameters()] will create a list of all the gradients for each parameter in your model, namely the derivative of the loss wrt the learnable parameters of your Module.

I guess my confusion is more of a fundamental understanding of gradients. Are the gradients obtained from [x.grad.data for x in network.parameters()] the same gradients used to update the parameters in gradient descent e.g. theta = theta - lr*gradient, where gradient is the derivative of the cost function w.r.t. the parameters in the network? Sounds like they’re something different - this point seems subtle to me so please correct any misunderstands I have - and that the gradients obtained from register_backwards_pass(hook) are exactly the gradients from theta = theta - lr*gradient, right?

Also say for whatever reason, if I want to manually implement dropout, which one should I use to control gradient updates, register_backwards_pass(hook) or [x.grad.data for x in network.parameters()]?
Like if I want to directly replace in_grad from register_backward_hook(hook), can I simply do something like:

Consider a layer that has an input i, and output o and some weights w. And suppose you have a loss function L.
If you want to minimize your loss by gradient descent, you want to obtain dL/dw and then update w as w = w - lr * dL/dw.
This dL/dw is computed during the backward pass and is stored in the Variable's .grad field.
That means that [x.grad.data for x in network.parameters()] will give you the list of all the dL/dw associated to each w in your network. If you want to do gradient descent by hand you can do:

for w in network.parameters():
w.data -= lr * w.grad.data

If you use a hook, you will get dL/di and dL/do. These are just intermediary computations. They are used to compute the dL/dw during the backward pass using the chain rule as we write for our layer: dL/dw = dL/do * do/dw where dL/do is given by the chain rule applied to the next layer, and do/dw is written by hand for each layer.
The gradients passed to these hook does not correspond to any weight that you are trying to learn.

Does that make it clearer?
You can also take a look at the examples to see how the optimiser should used in practice.

As mentioned in the above comment, register_backward_hook gives you the gradient with respect to the input and output. If you want the gradients for the specific parameters (weights and bias), then you need to use the register_hook method.

Again, register_forward_hook and register_backward_hook are only available for modules while register_hook is available only for parameters