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
None
{'all_weights_same': True, 'all_bias_same': True, 'num_linear_layers': 3, 'sample_weight': 0.5}- 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 threenn.Linearlayers and twonn.ReLUlayers, then defines an init function that sets weights to all and bias to all fornn.Linearlayers. - The init function is applied to the model using
model.apply(), resulting in allnn.Linearlayers having weights of and biases of . - The function then returns a dictionary with the desired information, including the fact that all weight values are and all bias values are , the count of
nn.Linearlayers (which is ), and the first weight value rounded to one decimal place (which is ).
Constraints
- Use model.apply()
- Only initialize nn.Linear layers
- Weights = 0.5, bias = 0.1