#289 · Deep Learning · Medium
⊣ Solve on deep-ml.comImplement Xavier (Glorot) weight initialization for neural networks. Given the number of input units (fan_in) and output units (fan_out), generate a weight matrix sampled from the appropriate distribution to maintain variance across layers.
import numpy as np
def xavier_uniform(fan_in: int, fan_out: int) -> np.ndarray:
limit = np.sqrt(6.0 / (fan_in + fan_out))
return np.random.uniform(-limit, limit, size=(fan_in, fan_out))
def xavier_normal(fan_in: int, fan_out: int) -> np.ndarray:
std = np.sqrt(2.0 / (fan_in + fan_out))
return np.random.normal(0, std, size=(fan_in, fan_out))