v9.1.1
Menu

Apply Kaiming Normal Initialization

easyFREEWeight InitializationPyTorch Advanced

Problem Statement

Apply Kaiming (He) normal initialization, designed for ReLU networks.

Background

Kaiming initialization samples from N(0, std) where std = gain / sqrt(fan_in) with gain=sqrt(2) for ReLU. This prevents the variance from shrinking in deep ReLU networks.

Your Task

The starter code creates an nn.Linear(8, 4) layer. Apply Kaiming normal initialization designed for ReLU networks to the layer's weights.

The rest (computing statistics and comparing to theoretical std) is pre-filled.

Output Format

Returns a dictionary with "weight_shape", "weight_mean", "weight_std", "expected_std", and "std_close".

Example

Input
None
Output
{'weight_shape': [4, 8], 'weight_mean': 0.0267, 'weight_std': 0.5526, 'expected_std': 0.5, 'std_close': True}
Reasoning
  • There are no arguments: the function fixes the random seed, builds a linear layer mapping 8 input features to 4 outputs, and reports statistics of its weight matrix. That matrix has one row per output and one column per input, so weight_shape is [4, 8] and each output neuron sums over fan_in=8fan\_in = 8 inputs.
  • For ReLU the gain is 2\sqrt{2}, so the target spread is σ=28=2/8=0.25=0.5\sigma = \frac{\sqrt{2}}{\sqrt{8}} = \sqrt{2/8} = \sqrt{0.25} = 0.5, giving expected_std =0.5= 0.5. Every weight should come from N(0,0.52)\mathcal{N}(0, 0.5^2).
  • The 4×8=324 \times 8 = 32 drawn weights have sample mean 0.02670.0267 (near the target 00) and sample standard deviation 0.55260.5526. With only 32 draws the estimate drifts a little from 0.50.5.
  • The gap is ∣0.5526−0.5∣=0.0526<0.2|0.5526 - 0.5| = 0.0526 < 0.2, so std_close is True.
  • Notice that σ\sigma depends only on the input width: a layer with 32 inputs would target 2/32=0.25\sqrt{2/32} = 0.25. Wider inputs get smaller weights, so the variance of each pre-activation stays about the same.

Constraints

  • Use nn.init.kaiming_normal_
  • mode='fan_in', nonlinearity='relu'
  • Compare actual vs theoretical std

Learn this in

More from PyTorch Advanced

solution.py

Test results

Sign in to run your code against the test cases.