Loading...
Use a forward hook to capture intermediate activations from a hidden layer.
module.register_forward_hook(hook_fn) lets you inspect outputs of any layer without modifying the model. The hook function receives (module, input, output).
The starter code creates a 3-layer model and an activations dict. Use a forward hook to capture the output of the first layer during a forward pass. Store the captured activation in the provided dict and remember to clean up the hook afterward.
Returns a dictionary with "activation_shape", "activation_values", and "final_output".
None
{'activation_shape': [1, 4], 'activation_values': [-0.1526, 0.2206, 1.674, -3.426], 'final_output': [-0.3288, 0.7.085]}nn.Sequential(nn.Linear(3, 4), nn.ReLU(), nn.Linear(4, 2)), and a forward hook is registered on the first Linear layer to capture its output.[[1.0, 2.0, 3.0]] is passed through the model, and the forward hook captures the output of the first Linear layer, which is a linear transformation of the input: y=x⋅W+b, where x is the input, W is the weight matrix, and b is the bias.[-0.3288, 0.7085].