Skip to content
Easy End-to-End

Weight Initialization

Compute two standard deviations used to initialize neural-network weights. This exercise returns the scales only: do not generate random weights or create a weight matrix.

Your task

Implement compute_init_scales(fan_in, fan_out) for positive integer inputs. For a dense layer, fan_in is the number of input features and fan_out is the number of output features. These counts are supplied directly, so you do not need to infer them from a matrix's storage layout.

Use these exact conventions:

xavier_scale = sqrt(2 / (fan_in + fan_out))
he_scale = sqrt(2 / fan_in)

The first is the standard deviation for Xavier normal initialization with gain 1. The second is the standard deviation for He normal initialization using fan-in scaling for ReLU. Do not add an activation-specific gain or substitute fan-out scaling.

Return a dictionary with keys "xavier_scale" and "he_scale", each holding a Python float rounded to seven decimal places. Use ordinary division before taking the square root; do not use integer division.

Example

For fan_in = 2 and fan_out = 2, the Xavier scale is sqrt(1/2) and the He scale is sqrt(1). Return:

{"xavier_scale": 0.7071068, "he_scale": 1.0}

Xavier depends on both counts. Swapping fan_in and fan_out leaves it unchanged, but generally changes the He scale.

What does scale mean?

For these normal initializers, scale means standard deviation, not variance or a hard bound on sampled weights. A zero-mean normal draw with standard deviation s has variance s**2 and can lie outside [-s, s]. Uniform initializer bounds use different formulas.

These are specified conventions for this exercise, not a guarantee that one formula is best for every model or activation. See the initialization reference for the gain and fan-mode options used by the library functions.