📘
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 withtorch.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), where 4 is the number of outputs (fan_out) and 6 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) where a=6+46=106=53. - 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
Python 3.13.1
Test Results
0/0Run code to see test results.