PIXELBANKv9.1.0
Menu

Custom Initialization with apply()

Problem Statement

Use model.apply() to recursively initialize all layers in a model.

Background

model.apply(fn) calls fn on every submodule. You can use this to apply different initialization strategies based on layer type.

Your Task

The starter code creates a 3-Linear-layer Sequential model. Define an initialization function that sets all Linear layer weights to 0.5 and biases to 0.1, then apply it to the entire model using model.apply().

The verification code is pre-filled.

Output Format

Returns a dictionary with "all_weights_same", "all_bias_same", "num_linear_layers", and "sample_weight".

Example:

Input:
None
Output:
{'all_weights_same': True, 'all_bias_same': True, 'num_linear_layers': 3, 'sample_weight': 0.5}
Reasoning:
  • The torch.manual_seed(42) ensures reproducibility of the results, but in this case, it doesn't affect the output because the initialization function sets weights and biases to constant values.
  • The custom_init_test() function creates a model with three nn.Linear layers and two nn.ReLU layers, then defines an init function that sets weights to all 0.50.5 and bias to all 0.10.1 for nn.Linear layers.
  • The init function is applied to the model using model.apply(), resulting in all nn.Linear layers having weights of 0.50.5 and biases of 0.10.1.
  • The function then returns a dictionary with the desired information, including the fact that all weight values are 0.50.5 and all bias values are 0.10.1, the count of nn.Linear layers (which is 33), and the first weight value rounded to one decimal place (which is 0.50.5).

Constraints:

  • Use model.apply()
  • Only initialize nn.Linear layers
  • Weights = 0.5, bias = 0.1
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.
Custom Initialization with apply() - Medium | PixelBank