PIXELBANKv8.2.1
Menu

Apply Xavier Uniform Initialization

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.0727, 'weight_std': 0.4648, 'weight_min': -0.6817, 'weight_max': 0.7337}
Reasoning:
  • The function xavier_uniform_test() starts by seeding the random number generator with torch.manual_seed(42) to ensure reproducibility.
  • It then creates a linear layer nn.Linear(6, 4), which has a weight matrix of shape (4,6)(4, 6), where 44 is the number of outputs (fan_out) and 66 is the number of inputs (fan_in).
  • The nn.init.xavier_uniform_ function is applied to the linear layer with default gain, which initializes the weights from a uniform distribution U(a,a)U(-a, a) where a=66+4=610=35a = \sqrt{\frac{6}{6 + 4}} = \sqrt{\frac{6}{10}} = \sqrt{\frac{3}{5}}.
  • The weights are then analyzed to calculate the mean, standard deviation, minimum, and maximum values, which are rounded to 4 decimals to produce the output dictionary.

Constraints:

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

Test Results

0/0
Run code to see test results.