TensorTonic
Problems
Xavier Initialization
Neural Networks
Easy

Xavier (Glorot) initialization sets initial weights to maintain roughly the same variance of activations and gradients across layers. This prevents the vanishing or exploding gradient problem in networks using sigmoid or tanh activations.

Given a raw weight matrix W with values in [0, 1], fan_in (number of input units), and fan_out (number of output units), scale the weights to Xavier uniform initialization.

Algorithm

  1. Compute the Xavier uniform bound:
L=6fin+foutL = \sqrt{\frac{6}{f_{in} + f_{out}}}L=fin​+fout​6​​
  1. Map each raw weight from [0, 1] to [-L, L]:
Wij′=Wij×2L−LW'_{ij} = W_{ij} \times 2L - LWij′​=Wij​×2L−L

Return a matrix of four-decimal floats with the same shape as W.

Loading visualization...

Examples

Input: W = [[0.5, 0.5], [0.5, 0.5]], fan_in = 2, fan_out = 2

Output: [[0.0, 0.0], [0.0, 0.0]]

Explanation: A raw midpoint of 0.5 maps to the center of the symmetric Xavier range.

Input: W = [[0, 1], [1, 0]], fan_in = 2, fan_out = 2

Output: [[-1.2247, 1.2247], [1.2247, -1.2247]]

Hint 1

Compute the symmetric limit from fan_in plus fan_out.

Hint 2

Map each raw value v with v times two limits minus one limit.

Requirements

  • Scale raw uniform [0, 1] weights to Xavier uniform range [-limit, limit]
  • Use the Xavier uniform formula: limit = sqrt(6 / (fan_in + fan_out))
  • Return the scaled weight matrix as a list of lists of floats
  • You do not need to round the returned numbers; answers that match the expected values to 4 decimal places are accepted.

Constraints

  • W has at least 1 row and 1 column with values in [0, 1]
  • fan_in >= 1, fan_out >= 1
  • Return a list of lists of floats with the same shape as W
  • Time limit: 300 ms
Try Similar Problems
Linear Layer Forward Pass in PythonEasyImplement He Initialization in Python (With Code)EasyImplement Dropout (Training Mode)MediumGradient Clipping (Global Norm)MediumBatch Normalization (Forward)MediumEvaluate a Scalar NeuronEasyRNN Step Forward (Tanh Cell)EasyCreate a Multiplication Value NodeEasyActivation FunctionsEasyDouble DQN TargetEasyPerceptronEasyBatch NormalizationEasy
Scratchpad

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
Xavier Initialization

Xavier Initialization

Neural Networks
Easy

Xavier (Glorot) initialization sets initial weights to maintain roughly the same variance of activations and gradients across layers. This prevents the vanishing or exploding gradient problem in networks using sigmoid or tanh activations.

Given a raw weight matrix W with values in [0, 1], fan_in (number of input units), and fan_out (number of output units), scale the weights to Xavier uniform initialization.

Algorithm

  1. Compute the Xavier uniform bound:
L=6fin+foutL = \sqrt{\frac{6}{f_{in} + f_{out}}}L=fin​+fout​6​​
  1. Map each raw weight from [0, 1] to [-L, L]:
Wij′=Wij×2L−LW'_{ij} = W_{ij} \times 2L - LWij′​=Wij​×2L−L

Return a matrix of four-decimal floats with the same shape as W.

Loading visualization...

Examples

Input: W = [[0.5, 0.5], [0.5, 0.5]], fan_in = 2, fan_out = 2

Output: [[0.0, 0.0], [0.0, 0.0]]

Explanation: A raw midpoint of 0.5 maps to the center of the symmetric Xavier range.

Input: W = [[0, 1], [1, 0]], fan_in = 2, fan_out = 2

Output: [[-1.2247, 1.2247], [1.2247, -1.2247]]

Hint 1

Compute the symmetric limit from fan_in plus fan_out.

Hint 2

Map each raw value v with v times two limits minus one limit.

Requirements

  • Scale raw uniform [0, 1] weights to Xavier uniform range [-limit, limit]
  • Use the Xavier uniform formula: limit = sqrt(6 / (fan_in + fan_out))
  • Return the scaled weight matrix as a list of lists of floats
  • You do not need to round the returned numbers; answers that match the expected values to 4 decimal places are accepted.

Constraints

  • W has at least 1 row and 1 column with values in [0, 1]
  • fan_in >= 1, fan_out >= 1
  • Return a list of lists of floats with the same shape as W
  • Time limit: 300 ms
Try Similar Problems
Linear Layer Forward Pass in PythonEasyImplement He Initialization in Python (With Code)EasyImplement Dropout (Training Mode)MediumGradient Clipping (Global Norm)MediumBatch Normalization (Forward)MediumEvaluate a Scalar NeuronEasyRNN Step Forward (Tanh Cell)EasyCreate a Multiplication Value NodeEasyActivation FunctionsEasyDouble DQN TargetEasyPerceptronEasyBatch NormalizationEasy
Scratchpad

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1