PIXELBANKv8.2.1
Menu

Register Forward Hook to Capture Activations

EasyHooks

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:

Input:
None
Output:
{'activation_shape': [1, 4], 'activation_values': [-0.1526, 0.2206, 1.674, -3.426], 'final_output': [-0.3288, 0.7.085]}
Reasoning:
  • The model is created as 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.
  • The input [[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=xW+by = x \cdot W + b, where xx is the input, WW is the weight matrix, and bb is the bias.
  • The captured output is then passed through the ReLU activation function, but since the hook captures the output before the ReLU function, the captured values are not yet activated.
  • The final output of the model is obtained by passing the output of the ReLU function through the second Linear layer, resulting in the values [-0.3288, 0.7085].

Constraints:

  • Use register_forward_hook
  • Hook captures output of first Linear layer
  • Do not modify the model architecture
Editor

Test Results

0/0
Run code to see test results.