Standardise conv weights per output channel

Weight standardisation normalises the weights instead of the activations: each output filter is shifted to mean 0 and scaled to variance 1 before the convolution. It is often paired with GroupNorm when batches are tiny.

Write weight_standardize(W, eps=1e-5). W has shape (out, ...) such as (out, in) or (out, in, kh, kw). For each output channel o, compute the mean and population variance over all of W[o] and return (W[o] - mean) / sqrt(var + eps). The result has the same shape as W.

Signaturedef weight_standardize(W, eps=1e-5)

Example

W = [[1.0, 3.0]] gives about [[-1.0, 1.0]] (exactly -1 / sqrt(1 + 1e-5) and its negative).

Understand the idea

Weight standardisation cleans up the filters themselves instead of the activations. Each output filter is shifted to mean 0 and scaled to variance 1 before it is used, so every filter starts from the same footing. The statistics are computed per output channel, over everything else in that filter: inputs and kernel positions together. Worked example: one filter with weights [1, 2, 3]. Mean = 2. Population variance = ((1 - 2)^2 + 0 + (3 - 2)^2) / 3 = 2 / 3, so std = 0.8165. The standardised filter is [-1.2247, 0, 1.2247] (with eps = 1e-5 inside the square root it is a hair smaller). For a conv weight of shape (out, in, kh, kw), each W[o] holds in * kh * kw numbers and gets its own mean and variance. Rule: for each o, W_std[o] = (W[o] - mean(W[o])) / sqrt(var(W[o]) + eps), with var dividing by the count, and the output keeps the input shape. Normalising across output channels (axis 0) is the common mistake. It is paired with GroupNorm for training with very small batches, such as detection and segmentation.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 4.

  • Sample 1: exampleweight_standardize([[1.0, 3.0]])
  • Sample 2: dense 4x5weight_standardize(_Wd)
  • Test 3: conv 3x2x3x3weight_standardize(_Wc)
  • Test 4: per-filter means are 0np.round(weight_standardize(_Wc).reshape(3, -1).mean(axis=1), 10)