Register Forward Hook to Capture Activations
Problem Statement
Use a forward hook to capture intermediate activations from a hidden layer.
Background
module.register_forward_hook(hook_fn) lets you inspect outputs of any layer without modifying the model. The hook function receives (module, input, output).
Your Task
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.
Output Format
Returns a dictionary with "activation_shape", "activation_values", and "final_output".
Example
None
{'activation_shape': [1, 4], 'activation_values': [1.4206, 0.7051, 2.2023, 0.8228], 'final_output': [-0.2042, -0.7735]}- Input: none. The seed fixes every weight of the network, and the forward pass uses the single row , a batch of one sample.
- First layer: , a result. Its first entry is ; the other three give , , . That row is the captured activation, shape
[1, 4]. - Rest of the network: ReLU zeroes negatives, but all four entries are positive, so they pass unchanged. The second layer maps them to .
- Output: the shape, the four rounded activations, and the two final values.
- Notice that the activation is taken straight out of the first layer, before ReLU: had any been negative, the captured value would still be negative even though the next layer sees .
Constraints
- Use register_forward_hook
- Hook captures output of first Linear layer
- Do not modify the model architecture