Feature Extraction with Hooks
Problem Statement
Extract features from multiple layers simultaneously using hooks.
Background
In many applications (e.g., FPN, style transfer), you need features from multiple layers. Hooks let you capture all of them in one forward pass.
Your Task
The starter code creates a 5-layer model with named modules. Register hooks on fc1, fc2, and fc3 to capture their outputs in a single forward pass. Use a pattern that correctly captures the layer name in the hook closure (a common gotcha with closures in loops).
Output Format
Returns a dictionary with "fc1_shape", "fc2_shape", "fc3_shape", and "num_features_captured".
Example:
None
{'fc1_shape': [1, 8], 'fc2_shape': [1, 4], 'fc3_shape': [1, 2], 'num_features_captured': 3}- The function
feature_extraction_test()starts by seeding the random number generator withtorch.manual_seed(42)to ensure reproducibility. - A model is created with the specified layers:
"fc1","relu1","fc2","relu2", and"fc3", and forward hooks are registered on"fc1","fc2", and"fc3"to capture their outputs. - The input
[[1.0, 2.0, 3.0, 4.0]]is passed through the model, and the outputs of the hooked layers are captured:"fc1"output shape is [1,8] because the input has 1 sample and"fc1"has 8 output neurons"fc2"output shape is [1,4] because the input to"fc2"has 1 sample and"fc2"has 4 output neurons"fc3"output shape is [1,2] because the input to"fc3"has 1 sample and"fc3"has 2 output neurons
- The function returns a dictionary with the shapes of the captured outputs and the number of layers captured, which is 3.
Constraints:
- Register hooks on 3 separate layers
- Capture outputs in a single forward pass
- Remove all hooks after
Background Knowledge
Introduction to PyTorch and Hooks
PyTorch is a popular deep learning framework that provides a dynamic computation graph and automatic differentiation. One of its key features is the ability to register hooks on modules, which are functions that can be executed at specific points during the forward or backward pass. In this problem, we will use forward hooks to capture the outputs of specific layers.
Understanding PyTorch Modules and Layers
In PyTorch, a module is a basic building block of a neural network, such as a linear layer (nn.Linear) or an activation function (nn.ReLU). A layer is a specific instance of a module, which can be composed together to form a larger network. In this problem, we will create a model with multiple named layers and register hooks on specific layers to capture their outputs.
Feature Extraction and Hooks
Feature extraction is the process of extracting relevant features from input data, which can be useful for various applications such as image classification, object detection, or style transfer. Hooks provide a convenient way to extract features from multiple layers simultaneously, without having to modify the underlying model architecture. By registering forward hooks on specific layers, we can capture their outputs and use them for further processing or analysis.
Algorithm/Approach
The general approach to solving this problem involves creating a PyTorch model with named layers, registering forward hooks on specific layers, and running input data through the model to capture the outputs. The key concepts involved are:
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.