{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"variance-through-a-layer","title":"How variance travels through a layer, and the initialisation that follows","url":"https://addaly.com/learn/maths-you-need/variance-through-a-layer","position":73,"of":76,"minutes":9,"answer":"Because a unit's output is a sum of fan_in independent products, its variance is fan_in × σ_w² × σ_x².","keyIdea":"Because a unit's output is a sum of fan_in independent products, its variance is fan_in × σ_w² × σ_x², so a weight spread of √(2/fan_in) keeps activations level through every ReLU layer while 0.05 or 0.01 explodes or vanishes them within twenty."},"body":{"format":"markdown","text":"## One unit's output is a sum\n\nA unit in a linear layer computes `y = Σ w_i x_i` over `fan_in` inputs. Treat the weights and inputs as random: each `w_i` drawn with variance `σ_w²`, each `x_i` with variance `σ_x²`, all independent and centred on zero. Module 4's rule that variances of independent terms add, and that the variance of a product of independent centred variables is the product of the variances, gives\n\n```\nVar(y) = fan_in × σ_w² × σ_x²\n```\n\nThat one line decides whether a deep network can be trained from a random start.\n\n## What happens over twenty layers\n\nSuppose you initialise every weight with a \"small random\" standard deviation of 0.05, a common default in hand-written code, and the layer has `fan_in = 1024`. Then\n\n```\nVar(y) / Var(x) = 1024 × 0.0025 = 2.56\n```\n\nEach layer multiplies the variance by 2.56. A ReLU after it zeroes half the values and roughly halves the variance, so the net factor is about 1.28 per layer. After twenty layers the activations have variance `1.28^20 ≈ 139` times the input's. After sixty, `10^6`. The final logits are enormous, the softmax saturates, and module 5's gradient `p − y` is either 0 or ±1 everywhere: the model begins its life confidently wrong and with no useful gradient.\n\nTry 0.01 instead:\n\n```\n1024 × 0.0001 × 0.5 = 0.051 per layer\n```\n\nAfter twenty layers the variance is `10^-26`. Every activation is effectively zero, every gradient with it, and by forty layers the values are below anything float32 can represent. The model does not learn because there is no signal to learn from. Module 3's vanishing gradients, from the other end.\n\n## The value that keeps it level\n\nSet the per-layer factor to 1:\n\n```\nfan_in × σ_w² × ½ = 1    →    σ_w = √(2 / fan_in)\n```\n\nFor `fan_in = 1024`, `σ_w = 0.044`. Between the two failures above sits a single number that keeps the variance constant through every layer, and it depends only on the width of the layer. That is the He, or Kaiming, initialisation, and the `2` is the ReLU's halving. Without a ReLU, or with tanh, the factor is `1/fan_in`, the Xavier or LeCun rule. `machine-learning-foundations` states these rules; this is where they come from, and the derivation is short enough that you could rediscover it if you forgot.\n\n## The backward pass has the same arithmetic\n\nGradients flowing backward through the layer are also sums, over `fan_out` terms this time, so the same argument gives `σ_w = √(2 / fan_out)` to keep gradient variance level. The two demands conflict unless `fan_in = fan_out`; the usual compromise averages them, and the disagreement is small enough not to matter for layers of similar widths. It matters for a layer that maps 4,096 dimensions to 32: initialise for the forward pass and the backward variance is off by a factor of 128 in that layer alone.\n\n## Why normalisation layers were the other answer\n\nInitialisation gets the variance right at step zero. Training moves the weights, and nothing then holds the variance in place. Layer normalisation and batch normalisation re-standardise the activations at every step, subtracting the mean and dividing by the spread, which enforces `Var = 1` by construction regardless of what the weights have become. They are the runtime version of the calculation above, and the reason very deep networks became trainable. Residual connections are a third answer: adding the input straight through means the variance grows only additively per block rather than multiplicatively, and `20 × small` is survivable where `1.28^20` is not.\n\n## Check it in three lines\n\n```python\nimport numpy as np\nrng = np.random.default_rng(0)\nx = rng.standard_normal((1024, 512))                    # 512 examples, 1024 wide\nfor sigma in (0.05, 0.01, np.sqrt(2 / 1024)):\n    h = x.copy()\n    for _ in range(20):\n        W = rng.standard_normal((1024, 1024)) * sigma\n        h = np.maximum(W @ h, 0)                        # ReLU\n    print(sigma, h.std())                               # huge, ~0, ~1\n```\n\nThe first run's standard deviation is in the tens, the second's is around `10^-13`, and the third's is about 1. Twenty lines of NumPy reproduce the whole argument, and the same script, pointed at your own architecture, tells you whether its initialisation is sane before you spend a GPU-hour finding out.\n\n## What the argument assumed\n\nIndependence between weights and inputs, which holds at initialisation and fails once training correlates them. Zero means, which a ReLU breaks by producing only non-negative outputs, so the halving is approximate. And a linear layer; convolutions have `fan_in = channels × kernel area`, attention has its own scaling, the `1/√d` in module 1's dot-product lesson, which is the same variance argument applied to a dot product of two `d`-dimensional vectors. The idea survives all of these. The constant changes."},"check":{"question":"A 40-layer ReLU network with layers 2,048 wide is initialised with weights of standard deviation 0.05. At the first step the loss is enormous and the gradients are all zero or ±1. Why?","options":[{"text":"Each layer multiplies the variance by about 2.6, and after 40 layers the softmax has saturated.","correct":true},{"text":"The learning rate is too high and the first update has already blown up the weights.","correct":false,"tempting":"The symptom is present before any update; it is the forward pass at initialisation that has produced saturated logits."},{"text":"0.05 is too small for such a wide layer, so the activations have vanished and the gradients underflowed.","correct":false,"tempting":"Vanishing would give a loss near ln(classes) and tiny gradients; a huge loss with gradients of ±1 is the saturated, exploded case, and 0.05 exceeds the √(2/2048) ≈ 0.031 that would keep variance level."},{"text":"ReLU zeroes half the activations, which halves the information at every layer until none remains.","correct":false,"tempting":"The halving is real but is exactly what the factor of 2 in the He rule accounts for; it does not by itself produce a huge loss."}]},"prev":{"slug":"condition-numbers","title":"The condition number, and how many digits a problem throws away","url":"https://addaly.com/learn/maths-you-need/condition-numbers"},"next":{"slug":"random-numbers-and-seeds","title":"Random numbers that are not, and what a seed does and does not fix","url":"https://addaly.com/learn/maths-you-need/random-numbers-and-seeds"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'How variance travels through a layer, and the initialisation that follows', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/variance-through-a-layer (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"How variance travels through a layer, and the initialisation that follows","courseTitle":"The Maths You Actually Need","module":{"index":8,"slug":"numbers-in-a-machine","title":"Numbers inside a machine"},"answer":null,"keyIdea":"Because a unit’s output is a sum of fan_in independent products, its variance is fan_in × σ_w² × σ_x², so a weight spread of √(2/fan_in) keeps activations level through every ReLU layer while 0.05 or 0.01 explodes or vanishes them within twenty.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"one-unit-s-output-is-a-sum","runs":[{"text":"One unit’s output is a sum"}]},{"t":"p","runs":[{"text":"A unit in a linear layer computes "},{"text":"y = Σ w_i x_i","code":true},{"text":" over "},{"text":"fan_in","code":true},{"text":" inputs. Treat the weights and inputs as random: each "},{"text":"w_i","code":true},{"text":" drawn with variance "},{"text":"σ_w²","code":true},{"text":", each "},{"text":"x_i","code":true},{"text":" with variance "},{"text":"σ_x²","code":true},{"text":", all independent and centred on zero. Module 4’s rule that variances of independent terms add, and that the variance of a product of independent centred variables is the product of the variances, gives"}]},{"t":"code","lang":"","label":"Text","text":"Var(y) = fan_in × σ_w² × σ_x²"},{"t":"p","runs":[{"text":"That one line decides whether a deep network can be trained from a random start."}]},{"t":"h2","id":"what-happens-over-twenty-layers","runs":[{"text":"What happens over twenty layers"}]},{"t":"p","runs":[{"text":"Suppose you initialise every weight with a “small random” standard deviation of 0.05, a common default in hand-written code, and the layer has "},{"text":"fan_in = 1024","code":true},{"text":". Then"}]},{"t":"code","lang":"","label":"Text","text":"Var(y) / Var(x) = 1024 × 0.0025 = 2.56"},{"t":"p","runs":[{"text":"Each layer multiplies the variance by 2.56. A ReLU after it zeroes half the values and roughly halves the variance, so the net factor is about 1.28 per layer. After twenty layers the activations have variance "},{"text":"1.28^20 ≈ 139","code":true},{"text":" times the input’s. After sixty, "},{"text":"10^6","code":true},{"text":". The final logits are enormous, the softmax saturates, and module 5’s gradient "},{"text":"p − y","code":true},{"text":" is either 0 or ±1 everywhere: the model begins its life confidently wrong and with no useful gradient."}]},{"t":"p","runs":[{"text":"Try 0.01 instead:"}]},{"t":"code","lang":"","label":"Text","text":"1024 × 0.0001 × 0.5 = 0.051 per layer"},{"t":"p","runs":[{"text":"After twenty layers the variance is "},{"text":"10^-26","code":true},{"text":". Every activation is effectively zero, every gradient with it, and by forty layers the values are below anything float32 can represent. The model does not learn because there is no signal to learn from. Module 3’s vanishing gradients, from the other end."}]},{"t":"h2","id":"the-value-that-keeps-it-level","runs":[{"text":"The value that keeps it level"}]},{"t":"p","runs":[{"text":"Set the per-layer factor to 1:"}]},{"t":"code","lang":"","label":"Text","text":"fan_in × σ_w² × ½ = 1    →    σ_w = √(2 / fan_in)"},{"t":"p","runs":[{"text":"For "},{"text":"fan_in = 1024","code":true},{"text":", "},{"text":"σ_w = 0.044","code":true},{"text":". Between the two failures above sits a single number that keeps the variance constant through every layer, and it depends only on the width of the layer. That is the He, or Kaiming, initialisation, and the "},{"text":"2","code":true},{"text":" is the ReLU’s halving. Without a ReLU, or with tanh, the factor is "},{"text":"1/fan_in","code":true},{"text":", the Xavier or LeCun rule. "},{"text":"machine-learning-foundations","code":true},{"text":" states these rules; this is where they come from, and the derivation is short enough that you could rediscover it if you forgot."}]},{"t":"h2","id":"the-backward-pass-has-the-same-arithmetic","runs":[{"text":"The backward pass has the same arithmetic"}]},{"t":"p","runs":[{"text":"Gradients flowing backward through the layer are also sums, over "},{"text":"fan_out","code":true},{"text":" terms this time, so the same argument gives "},{"text":"σ_w = √(2 / fan_out)","code":true},{"text":" to keep gradient variance level. The two demands conflict unless "},{"text":"fan_in = fan_out","code":true},{"text":"; the usual compromise averages them, and the disagreement is small enough not to matter for layers of similar widths. It matters for a layer that maps 4,096 dimensions to 32: initialise for the forward pass and the backward variance is off by a factor of 128 in that layer alone."}]},{"t":"h2","id":"why-normalisation-layers-were-the-other-answer","runs":[{"text":"Why normalisation layers were the other answer"}]},{"t":"p","runs":[{"text":"Initialisation gets the variance right at step zero. Training moves the weights, and nothing then holds the variance in place. Layer normalisation and batch normalisation re-standardise the activations at every step, subtracting the mean and dividing by the spread, which enforces "},{"text":"Var = 1","code":true},{"text":" by construction regardless of what the weights have become. They are the runtime version of the calculation above, and the reason very deep networks became trainable. Residual connections are a third answer: adding the input straight through means the variance grows only additively per block rather than multiplicatively, and "},{"text":"20 × small","code":true},{"text":" is survivable where "},{"text":"1.28^20","code":true},{"text":" is not."}]},{"t":"h2","id":"check-it-in-three-lines","runs":[{"text":"Check it in three lines"}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\nrng = np.random.default_rng(0)\nx = rng.standard_normal((1024, 512))                    # 512 examples, 1024 wide\nfor sigma in (0.05, 0.01, np.sqrt(2 / 1024)):\n    h = x.copy()\n    for _ in range(20):\n        W = rng.standard_normal((1024, 1024)) * sigma\n        h = np.maximum(W @ h, 0)                        # ReLU\n    print(sigma, h.std())                               # huge, ~0, ~1"},{"t":"p","runs":[{"text":"The first run’s standard deviation is in the tens, the second’s is around "},{"text":"10^-13","code":true},{"text":", and the third’s is about 1. Twenty lines of NumPy reproduce the whole argument, and the same script, pointed at your own architecture, tells you whether its initialisation is sane before you spend a GPU-hour finding out."}]},{"t":"h2","id":"what-the-argument-assumed","runs":[{"text":"What the argument assumed"}]},{"t":"p","runs":[{"text":"Independence between weights and inputs, which holds at initialisation and fails once training correlates them. Zero means, which a ReLU breaks by producing only non-negative outputs, so the halving is approximate. And a linear layer; convolutions have "},{"text":"fan_in = channels × kernel area","code":true},{"text":", attention has its own scaling, the "},{"text":"1/√d","code":true},{"text":" in module 1’s dot-product lesson, which is the same variance argument applied to a dot product of two "},{"text":"d","code":true},{"text":"-dimensional vectors. The idea survives all of these. The constant changes."}]}],"check":{"question":[{"text":"A 40-layer ReLU network with layers 2,048 wide is initialised with weights of standard deviation 0.05. At the first step the loss is enormous and the gradients are all zero or ±1. Why?"}],"options":[{"text":[{"text":"Each layer multiplies the variance by about 2.6, and after 40 layers the softmax has saturated."}],"correct":true,"tempting":null},{"text":[{"text":"The learning rate is too high and the first update has already blown up the weights."}],"correct":false,"tempting":[{"text":"The symptom is present before any update; it is the forward pass at initialisation that has produced saturated logits."}]},{"text":[{"text":"0.05 is too small for such a wide layer, so the activations have vanished and the gradients underflowed."}],"correct":false,"tempting":[{"text":"Vanishing would give a loss near ln(classes) and tiny gradients; a huge loss with gradients of ±1 is the saturated, exploded case, and 0.05 exceeds the √(2/2048) ≈ 0.031 that would keep variance level."}]},{"text":[{"text":"ReLU zeroes half the activations, which halves the information at every layer until none remains."}],"correct":false,"tempting":[{"text":"The halving is real but is exactly what the factor of 2 in the He rule accounts for; it does not by itself produce a huge loss."}]}],"order":[3,2,0,1],"why":null},"prev":{"slug":"condition-numbers","title":"The condition number, and how many digits a problem throws away","minutes":9},"next":{"slug":"random-numbers-and-seeds","title":"Random numbers that are not, and what a seed does and does not fix","minutes":9}}}