Instance normalization
Background
Instance Normalization normalizes each channel of each sample independently over its spatial dimensions. It is the standard in style transfer and image generation, where per-image, per-channel contrast normalization removes instance-specific contrast that would otherwise leak into the output.
Problem statement
Implement instance_normalization(X, gamma, beta, epsilon=1e-5) for X of shape (B, C, H, W). For each (sample, channel) normalize over the spatial dimensions, then apply the per-channel affine:
with the mean/variance over .
Input
X—np.ndarray(B, C, H, W).gamma,beta—np.ndarray(C,): per-channel scale and shift.epsilon—float.
Output
Returns an np.ndarray (B, C, H, W).
Examples
Example 1
Input: X = [[[[1, 2], [3, 4]]]] (shape 1x1x2x2), gamma = [1], beta = [0]
Output: [[[[-1.3416, -0.4472], [0.4472, 1.3416]]]]
Explanation: the single channel's four values are normalized (mean 2.5, std ), then the identity affine is applied.
Constraints
- Mean/variance over spatial axes
(2, 3)per (sample, channel), with keepdims. - Apply per-channel
gamma/beta(broadcast as(1, C, 1, 1)). - Tests compare with
atol=1e-4.
Notes
- Instance Norm equals GroupNorm with
num_groups = C(each channel is its own group). - Stripping per-instance contrast is exactly why it works for style transfer: the "style" lives in channel statistics, which IN removes before re-styling.
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example
- •Each (sample, channel) is normalized to ~0 mean, ~1 var
- •Output shape matches input
- •Affine: gamma scales, beta shifts