{"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":"the-chain-rule","title":"The chain rule, which is the whole trick","url":"https://addaly.com/learn/maths-you-need/the-chain-rule","position":22,"of":76,"minutes":8,"answer":"When functions are nested, their rates of change multiply, which is why a network of many layers is differentiable at all and why signals can shrink or blow up as they pass back.","keyIdea":"When functions are nested, their rates of change multiply, which is why a network of many layers is differentiable at all and why signals can shrink or blow up as they pass back."},"body":{"format":"markdown","text":"## Rates multiply\n\nIf a car's fuel use depends on speed, and speed depends on the accelerator position, how does fuel use depend on the accelerator? Multiply the two rates. That is the chain rule, and it is the only calculus fact you have to internalise for machine learning.\n\nFormally, if `y = f(u)` and `u = g(x)`:\n\n```\ndy/dx = (dy/du) * (du/dx)\n```\n\nWorked. Let `y = (3x + 1)^2`. Set `u = 3x + 1`, so `y = u^2`.\n\n```\ndy/du = 2u = 2(3x + 1)\ndu/dx = 3\ndy/dx = 2(3x + 1) * 3 = 6(3x + 1) = 18x + 6\n```\n\nCheck it by expanding first: `y = 9x² + 6x + 1`, derivative `18x + 6`. Same answer. The chain rule saved you the expansion, and in a network the expansion is not available at any price.\n\n## Longer chains\n\nNesting three deep just multiplies three factors:\n\n```\ny = f(g(h(x)))\ndy/dx = f'(g(h(x))) * g'(h(x)) * h'(x)\n```\n\nA neural network with 40 layers is a function nested 40 deep, plus an activation function inside each layer, so more like 80. The derivative of the loss with respect to a first-layer weight is a product of around 80 terms.\n\nThat single sentence explains two of the most important phenomena in deep learning.\n\n## Why gradients vanish, in arithmetic\n\nSuppose each of those factors is around 0.5, entirely plausible for a sigmoid, whose derivative maxes out at 0.25.\n\n```\n0.5^10 = 0.00098\n0.5^20 = 0.00000095\n0.5^40 = 9.1e-13\n```\n\nBy layer 40 the gradient reaching the first layer is a millionth of a millionth of what reaches the last. In 32-bit floating point it is indistinguishable from zero. The early layers receive no signal and do not learn. This is the vanishing gradient problem, and it is not a bug in anyone's code: it is a product of small numbers, behaving exactly as multiplication does.\n\nNow suppose each factor is 1.5:\n\n```\n1.5^10 = 57.7\n1.5^20 = 3,325\n1.5^40 = 11,057,332\n```\n\nThat is the exploding gradient, which shows up as a loss that suddenly becomes `NaN`.\n\nThe architectural responses all attack the factors directly. ReLU has derivative exactly 1 for positive inputs, so it contributes a factor of 1 rather than 0.25. A residual connection computes `x + F(x)`, whose derivative is `1 + F'(x)`: a path through which the gradient passes with factor 1 no matter what the layer does. Normalisation layers keep activations in a range where derivatives are not tiny. Every one of these is a fix for a product of eighty numbers.\n\n## The multivariable version\n\nWhen a variable influences the output through several paths, the chain rule sums over the paths as well as multiplying along them:\n\n```\ndL/dx = sum over paths of (product of derivatives along that path)\n```\n\nThis is what automatic differentiation implements. The computation is a graph; each edge has a local derivative; the derivative of the output with respect to any input is the sum over all routes of the product along each route. Backpropagation is an efficient ordering of that sum, not a different idea.\n\n## Worked on a tiny network\n\nOne neuron, sigmoid activation, squared loss. Forward:\n\n```\nz = w x + b\na = sigmoid(z)\nL = (a - y)^2\n```\n\nBackward, one factor at a time:\n\n```\ndL/da = 2(a - y)\nda/dz = a(1 - a)\ndz/dw = x\n\ndL/dw = 2(a - y) * a(1 - a) * x\n```\n\nPut numbers in: `x = 2`, `w = 0.5`, `b = 0`, `y = 1`.\n\n```\nz = 1.0\na = 1/(1 + e^-1) = 0.7311\nL = (0.7311 - 1)^2 = 0.0723\ndL/dw = 2(-0.2689) * (0.7311)(0.2689) * 2 = -0.2115\n```\n\nNegative, so increasing w decreases the loss, which is right, since a needs to rise towards 1. Notice the middle factor, `a(1 − a) = 0.197`. If `a` had been 0.99, that factor would be 0.0099 and the gradient would be twenty times smaller. A confident sigmoid learns slowly even when it is confidently wrong, which is the specific reason cross-entropy loss replaced squared error for classification: the `a(1 − a)` term cancels out of the algebra.\n\n## The rule to keep\n\nNested functions multiply their rates. Chains of many small numbers vanish and chains of many large ones explode, and almost every architectural trick in deep learning is an attempt to keep those factors near one."},"check":{"question":"Why does a residual connection, computing x + F(x) instead of F(x), help gradients reach early layers?","options":[{"text":"It halves the number of layers the gradient must pass through, since the skip path bypasses one layer each time.","correct":false,"tempting":"The gradient still traverses every block; what changes is the factor contributed, not the count."},{"text":"It normalises the activations, keeping them in a range where derivatives are large.","correct":false,"tempting":"That describes what a normalisation layer does; a residual connection adds an identity path and does not rescale anything."},{"text":"Its derivative is 1 + F'(x), so there is a path contributing a factor of exactly 1 at each block, and a product of ones does not shrink.","correct":true},{"text":"It makes the loss surface convex, so gradient descent converges from any starting point.","correct":false,"tempting":"Residual networks are not convex; they are easier to optimise empirically, which is a different and weaker claim."}]},"prev":{"slug":"partial-derivatives-and-the-gradient","title":"Many knobs, one derivative each","url":"https://addaly.com/learn/maths-you-need/partial-derivatives-and-the-gradient"},"next":{"slug":"backpropagation-as-bookkeeping","title":"Backpropagation, and where the memory goes","url":"https://addaly.com/learn/maths-you-need/backpropagation-as-bookkeeping"},"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) 'The chain rule, which is the whole trick', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/the-chain-rule (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":"The chain rule, which is the whole trick","courseTitle":"The Maths You Actually Need","module":{"index":3,"slug":"change-and-descent","title":"Change, slopes and the walk downhill"},"answer":null,"keyIdea":"When functions are nested, their rates of change multiply, which is why a network of many layers is differentiable at all and why signals can shrink or blow up as they pass back.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"rates-multiply","runs":[{"text":"Rates multiply"}]},{"t":"p","runs":[{"text":"If a car’s fuel use depends on speed, and speed depends on the accelerator position, how does fuel use depend on the accelerator? Multiply the two rates. That is the chain rule, and it is the only calculus fact you have to internalise for machine learning."}]},{"t":"p","runs":[{"text":"Formally, if "},{"text":"y = f(u)","code":true},{"text":" and "},{"text":"u = g(x)","code":true},{"text":":"}]},{"t":"code","lang":"","label":"Text","text":"dy/dx = (dy/du) * (du/dx)"},{"t":"p","runs":[{"text":"Worked. Let "},{"text":"y = (3x + 1)^2","code":true},{"text":". Set "},{"text":"u = 3x + 1","code":true},{"text":", so "},{"text":"y = u^2","code":true},{"text":"."}]},{"t":"code","lang":"","label":"Text","text":"dy/du = 2u = 2(3x + 1)\ndu/dx = 3\ndy/dx = 2(3x + 1) * 3 = 6(3x + 1) = 18x + 6"},{"t":"p","runs":[{"text":"Check it by expanding first: "},{"text":"y = 9x² + 6x + 1","code":true},{"text":", derivative "},{"text":"18x + 6","code":true},{"text":". Same answer. The chain rule saved you the expansion, and in a network the expansion is not available at any price."}]},{"t":"h2","id":"longer-chains","runs":[{"text":"Longer chains"}]},{"t":"p","runs":[{"text":"Nesting three deep just multiplies three factors:"}]},{"t":"code","lang":"","label":"Text","text":"y = f(g(h(x)))\ndy/dx = f'(g(h(x))) * g'(h(x)) * h'(x)"},{"t":"p","runs":[{"text":"A neural network with 40 layers is a function nested 40 deep, plus an activation function inside each layer, so more like 80. The derivative of the loss with respect to a first-layer weight is a product of around 80 terms."}]},{"t":"p","runs":[{"text":"That single sentence explains two of the most important phenomena in deep learning."}]},{"t":"h2","id":"why-gradients-vanish-in-arithmetic","runs":[{"text":"Why gradients vanish, in arithmetic"}]},{"t":"p","runs":[{"text":"Suppose each of those factors is around 0.5, entirely plausible for a sigmoid, whose derivative maxes out at 0.25."}]},{"t":"code","lang":"","label":"Text","text":"0.5^10 = 0.00098\n0.5^20 = 0.00000095\n0.5^40 = 9.1e-13"},{"t":"p","runs":[{"text":"By layer 40 the gradient reaching the first layer is a millionth of a millionth of what reaches the last. In 32-bit floating point it is indistinguishable from zero. The early layers receive no signal and do not learn. This is the vanishing gradient problem, and it is not a bug in anyone’s code: it is a product of small numbers, behaving exactly as multiplication does."}]},{"t":"p","runs":[{"text":"Now suppose each factor is 1.5:"}]},{"t":"code","lang":"","label":"Text","text":"1.5^10 = 57.7\n1.5^20 = 3,325\n1.5^40 = 11,057,332"},{"t":"p","runs":[{"text":"That is the exploding gradient, which shows up as a loss that suddenly becomes "},{"text":"NaN","code":true},{"text":"."}]},{"t":"p","runs":[{"text":"The architectural responses all attack the factors directly. ReLU has derivative exactly 1 for positive inputs, so it contributes a factor of 1 rather than 0.25. A residual connection computes "},{"text":"x + F(x)","code":true},{"text":", whose derivative is "},{"text":"1 + F'(x)","code":true},{"text":": a path through which the gradient passes with factor 1 no matter what the layer does. Normalisation layers keep activations in a range where derivatives are not tiny. Every one of these is a fix for a product of eighty numbers."}]},{"t":"h2","id":"the-multivariable-version","runs":[{"text":"The multivariable version"}]},{"t":"p","runs":[{"text":"When a variable influences the output through several paths, the chain rule sums over the paths as well as multiplying along them:"}]},{"t":"code","lang":"","label":"Text","text":"dL/dx = sum over paths of (product of derivatives along that path)"},{"t":"p","runs":[{"text":"This is what automatic differentiation implements. The computation is a graph; each edge has a local derivative; the derivative of the output with respect to any input is the sum over all routes of the product along each route. Backpropagation is an efficient ordering of that sum, not a different idea."}]},{"t":"h2","id":"worked-on-a-tiny-network","runs":[{"text":"Worked on a tiny network"}]},{"t":"p","runs":[{"text":"One neuron, sigmoid activation, squared loss. Forward:"}]},{"t":"code","lang":"","label":"Text","text":"z = w x + b\na = sigmoid(z)\nL = (a - y)^2"},{"t":"p","runs":[{"text":"Backward, one factor at a time:"}]},{"t":"code","lang":"","label":"Text","text":"dL/da = 2(a - y)\nda/dz = a(1 - a)\ndz/dw = x\n\ndL/dw = 2(a - y) * a(1 - a) * x"},{"t":"p","runs":[{"text":"Put numbers in: "},{"text":"x = 2","code":true},{"text":", "},{"text":"w = 0.5","code":true},{"text":", "},{"text":"b = 0","code":true},{"text":", "},{"text":"y = 1","code":true},{"text":"."}]},{"t":"code","lang":"","label":"Text","text":"z = 1.0\na = 1/(1 + e^-1) = 0.7311\nL = (0.7311 - 1)^2 = 0.0723\ndL/dw = 2(-0.2689) * (0.7311)(0.2689) * 2 = -0.2115"},{"t":"p","runs":[{"text":"Negative, so increasing w decreases the loss, which is right, since a needs to rise towards 1. Notice the middle factor, "},{"text":"a(1 − a) = 0.197","code":true},{"text":". If "},{"text":"a","code":true},{"text":" had been 0.99, that factor would be 0.0099 and the gradient would be twenty times smaller. A confident sigmoid learns slowly even when it is confidently wrong, which is the specific reason cross-entropy loss replaced squared error for classification: the "},{"text":"a(1 − a)","code":true},{"text":" term cancels out of the algebra."}]},{"t":"h2","id":"the-rule-to-keep","runs":[{"text":"The rule to keep"}]},{"t":"p","runs":[{"text":"Nested functions multiply their rates. Chains of many small numbers vanish and chains of many large ones explode, and almost every architectural trick in deep learning is an attempt to keep those factors near one."}]}],"check":{"question":[{"text":"Why does a residual connection, computing x + F(x) instead of F(x), help gradients reach early layers?"}],"options":[{"text":[{"text":"It halves the number of layers the gradient must pass through, since the skip path bypasses one layer each time."}],"correct":false,"tempting":[{"text":"The gradient still traverses every block; what changes is the factor contributed, not the count."}]},{"text":[{"text":"It normalises the activations, keeping them in a range where derivatives are large."}],"correct":false,"tempting":[{"text":"That describes what a normalisation layer does; a residual connection adds an identity path and does not rescale anything."}]},{"text":[{"text":"Its derivative is 1 + F’(x), so there is a path contributing a factor of exactly 1 at each block, and a product of ones does not shrink."}],"correct":true,"tempting":null},{"text":[{"text":"It makes the loss surface convex, so gradient descent converges from any starting point."}],"correct":false,"tempting":[{"text":"Residual networks are not convex; they are easier to optimise empirically, which is a different and weaker claim."}]}],"order":[3,2,0,1],"why":null},"prev":{"slug":"partial-derivatives-and-the-gradient","title":"Many knobs, one derivative each","minutes":8},"next":{"slug":"backpropagation-as-bookkeeping","title":"Backpropagation, and where the memory goes","minutes":10}}}