PIXELBANKv9.1.0
Menu

Apply Kaiming Normal Initialization

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.1281, 'weight_std': 0.4966, 'expected_std': 0.5, 'std_close': True}
Reasoning:
  • The function kaiming_normal_test() starts by seeding the random number generator with torch.manual_seed(42) to ensure reproducibility.
  • It then creates a linear layer nn.Linear(8, 4), which has a weight matrix of shape [4, 8], and applies Kaiming normal initialization with nn.init.kaiming_normal_ and default parameters for ReLU networks: std=2/fan_instd = \sqrt{2} / \sqrt{fan\_in}, where fan_in=8fan\_in = 8.
  • The theoretical standard deviation is calculated as 2/8=2/23=2/22=1/2=2/2=0.5\sqrt{2} / \sqrt{8} = \sqrt{2} / \sqrt{2^3} = \sqrt{2} / 2\sqrt{2} = 1/\sqrt{2} = \sqrt{2}/2 = 0.5, which is the expected standard deviation.
  • The actual standard deviation of the initialized weights is calculated and compared to the expected standard deviation, with the result being that the actual standard deviation (0.49660.4966) is within 0.20.2 of the expected standard deviation (0.50.5), so "std_close" is True.

Constraints:

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

Test Results

0/0
Run code to see test results.
Apply Kaiming Normal Initialization - Easy | PixelBank