v9.1.1
Menu

Apply Xavier Uniform Initialization

easyFREEWeight InitializationPyTorch Advanced

Problem Statement

Apply Xavier (Glorot) uniform initialization to a linear layer and verify its properties.

Background

Xavier initialization sets weights from a uniform distribution U(-a, a) where a = gain * sqrt(6 / (fan_in + fan_out)). It's designed for layers with Sigmoid/Tanh activations.

Your Task

The starter code creates an nn.Linear(6, 4) layer. Apply Xavier uniform initialization to the layer's weights.

The rest (computing statistics) is pre-filled.

Output Format

Returns a dictionary with "weight_shape", "weight_mean", "weight_std", "weight_min", and "weight_max".

Example

Input
None
Output
{'weight_shape': [4, 6], 'weight_mean': 0.0607, 'weight_std': 0.4581, 'weight_min': -0.7668, 'weight_max': 0.6995}
Reasoning
  • torch.manual_seed(42) makes the draw reproducible, and nn.Linear(6, 4) has a weight matrix of shape (4,6)(4, 6): fan_out =4= 4 outputs, fan_in =6= 6 inputs.
  • nn.init.xavier_uniform_ (default gain 11) redraws all 2424 weights from U(−a,a)U(-a, a) with a=66+4=0.6≈0.7746a = \sqrt{\frac{6}{6 + 4}} = \sqrt{0.6} \approx 0.7746.
  • Every weight therefore lies in [−0.7746,0.7746][-0.7746, 0.7746]: the drawn extremes are min⁡=−0.7668\min = -0.7668 and max⁡=0.6995\max = 0.6995, both inside the bound.
  • A uniform U(−a,a)U(-a, a) has mean 00 and standard deviation a/3≈0.447a / \sqrt{3} \approx 0.447; this sample of 2424 gives mean 0.06070.0607 and std 0.45810.4581, close to those.
  • Rounded to 4 decimals: {'weight_shape': [4, 6], 'weight_mean': 0.0607, 'weight_std': 0.4581, 'weight_min': -0.7668, 'weight_max': 0.6995}.

Constraints

  • Use nn.init.xavier_uniform_
  • Default gain (1.0)
  • Apply to weight only (not bias)

Learn this in

More from PyTorch Advanced

solution.py

Test results

Sign in to run your code against the test cases.