Mathematical Prerequisites for the Delta Rule
Building the foundations for the delta rule and gated attention — the Frobenius norm and squared reconstruction loss, the gradient of a quadratic loss with respect to a matrix state, online stochastic gradient descent as a one-step update, and the chain rule for gated identity paths — all derived step by step with one consistent two-dimensional key-value example.
The next two posts in this series, Targeted Memory: The Delta Rule, Gated DeltaNet, and Kimi Delta Attention and Gated Attention: Replacing Residuals and ReLU with Learned Gates, both rest on the same idea: an attention update can be derived as one step of stochastic gradient descent on a reconstruction loss. The delta rule is
and is presented in the gated-deltanet paper as the closed-form result of taking one SGD step on with learning rate . Without matrix calculus the appearance of from this loss reads as magic. With it, every term is forced.
This post builds four tools. The Frobenius norm measures the size of a matrix as the square root of the sum of squared entries. The squared reconstruction loss is the natural objective for associative memory. Matrix calculus lets us differentiate a scalar loss with respect to a matrix variable, and the online SGD update with one matrix-valued step turns the gradient into the delta rule. The fifth tool is the chain rule for a gated identity path — needed to analyze the gradient flow through the residual-replacing gates in the gated-attention blog.
Before we get to norms and gradients, we need one small piece of associative-memory algebra. The delta rule updates a matrix state that stores key-value pairs, so we first derive what a write means and what a read returns. These are the only structural facts we need, and we derive them here so this post can stand on its own.
The Running Example
A 2-dimensional associative memory storing a single key–value pair. The state is a matrix . The key and value are
with learning rate . We will compute the reconstruction loss at , take its gradient with respect to , apply one SGD step, and verify that retrieval now returns exactly. Same setup, throughout.
1. Associative Memory Refresher
1.1 Motivation
The delta rule is an update for a matrix state . To understand why the loss is , we first need to know how stores key-value pairs and how a query reads them back. That starts with the outer product.
1.2 Outer Product
For column vectors and , the outer product is the matrix
Every column of is a scalar multiple of , and every row is a scalar multiple of , so the result has rank 1.
1.3 Derivation: a sum of outer products is an associative memory
Suppose we store key-value pairs in the state
Querying that state with a vector gives
The first step used , and the second used distributivity of matrix multiplication over addition. So the readout is a weighted combination of stored values, where each weight is the inner product between the query and a stored key .
1.4 Exact retrieval and interference
If we query with one of the stored keys, say , then
This returns exactly only when the keys are orthonormal: for and . In that case every other stored value disappears and we get
If the keys are not orthonormal, other values leak into the readout. That leakage is exactly the reconstruction error that the delta rule will try to reduce.
1.5 Numerical check with the running example
With the running key and value,
Read with the same key:
So a single outer-product write stores a single key-value pair exactly.
1.6 Interpretation
This is the baseline memory model. The delta rule keeps the same readout , but instead of always writing a full additive outer product, it takes one SGD step that both writes the new pair and corrects whatever the current state gets wrong about it.
2. The Frobenius Norm
2.1 Motivation
The reconstruction loss compares two vectors and produces a scalar. Internally it computes a vector difference and then “the size of the difference vector”. To define that size — and later to extend to matrices when we differentiate — we need the Frobenius norm. It is the matrix analogue of the squared Euclidean norm of a vector, and it specializes to the vector case for matrices.
2.2 Definition for vectors
For a vector , the squared Euclidean norm is
The square root is the length of . The squared form is what appears in least-squares losses because it is differentiable everywhere and its gradient has a clean closed form.
2.3 Definition for matrices
For a matrix , the Frobenius norm is the square root of the sum of squared entries:
The trace identity is a useful rewrite: , the same sum.
2.4 Numerical check with the running example
Let (the reconstruction error at ). With :
By the squared-norm definition:
So — the classical 3-4-5 triangle.
2.5 Interpretation
The Frobenius norm extends the vector squared-length to matrices by treating the matrix as a flattened vector. It is the simplest matrix norm, the one that gives matrix-valued gradient descent its closed-form updates, and the norm under which every objective in this post is defined.
3. The Reconstruction Loss for Associative Memory
3.1 Motivation
Section 1 showed that for an associative memory
the readout for query is
This returns exactly only when the stored keys are orthonormal, so that the query for one key does not pick up contributions from the others. When the keys are not orthonormal, retrieval is imperfect — there is a reconstruction error. The delta rule is derived by minimizing the squared size of this error.
3.2 Formal definition
For a single query–answer pair at step and a state matrix , the reconstruction loss is
The factor of is conventional and exists only to cancel the 2 that comes out when we differentiate the square. The objective is to choose so that retrieving with key returns a vector close to .
3.3 Numerical check with the running example
At the error is from Section 2.4, so
The loss is large because the empty memory cannot reconstruct anything — that is exactly the regime where the first SGD step (Section 5) will produce a large update.
3.4 Interpretation
The reconstruction loss is a differentiable surrogate for “did we store this pair?” — small loss means the memory returns the right value when queried. Section 4 differentiates this loss with respect to the matrix ; Section 5 turns the gradient into a one-step update. The two together are the delta rule.
4. Matrix Calculus: Gradient of the Reconstruction Loss
4.1 Motivation
Section 3 wrote the loss as a function of a matrix . To do gradient descent we need — a matrix of the same shape as , whose entry is the partial derivative of the scalar with respect to the entry . This is matrix calculus.
4.2 The convention
For a scalar function of a matrix , the gradient is the matrix
The output has the same shape as the input.
4.3 Derivation: gradient of with respect to
Start from the definition:
Let (the reconstruction error). Note depends linearly on : changing to changes by .
Expand the loss:
Differentiate using the product rule on . A small perturbation produces a first-order change in the loss
where the last step uses that both terms are scalars and equal each other (both are ). Substitute :
The two transposes flipped because the quantity is a scalar (a matrix is its own transpose). Now we want to read off the gradient. By the inner-product identity for matrices, , and
The first equality uses the cyclic property of the trace ( when shapes allow). So , which means the gradient is the matrix that pairs with under the Frobenius inner product:
The last equality just expanded .
4.4 Numerical check with the running example
At , the error is (from Section 2.4). The gradient is
Verify by entry-wise partial derivatives. Write . Then (since picks out the first row of , which is the first column of ). The error is and the loss is
The partials are , , and . At these evaluate to , so
which matches the boxed formula.
4.5 Interpretation
The gradient is a rank-1 matrix — it is the outer product of the key with the error. This is what makes the delta rule’s update structurally simple: it modifies only in the column direction (every column of is a multiple of ) and only changes the entries that affect the key being trained. Components of orthogonal to are untouched. The next section turns this gradient into the delta rule by taking one SGD step.
5. Online Stochastic Gradient Descent: One Step Becomes the Delta Rule
5.1 Motivation
Sections 3 and 4 gave us a loss and its gradient. The natural way to use them is gradient descent: move in the direction of decreasing loss, with a step size . Online SGD takes one such step per data point, never visiting the same example twice. For a sequence of key–value pairs, this gives one update per step — exactly the form of a recurrent update.
5.2 The SGD update
For a loss and learning rate , one SGD step is
The minus sign moves down the loss surface (descent). The step size controls how far.
5.3 Derivation: applying SGD to the reconstruction loss
Substitute the gradient from Section 4:
Distribute the minus sign and group the terms:
Factor the first two terms by pulling out on the right:
This is the delta rule. It is one step of online SGD on the reconstruction loss with learning rate — nothing more, nothing less. The mysterious factor, which the gated-deltanet paper calls a generalized Householder transformation, is just the identity minus the gradient’s “erase old value” component.
5.4 Numerical check with the running example
With and the gradient from Section 4.4:
Verify by the boxed factored form. With (so ):
Summing: . Both forms agree.
Now query: . Perfect retrieval after one step.
5.5 Delta rule vs plain additive write: same problem, two solutions
This is the cleanest distinction between the delta rule and plain linear attention. Plain additive write is the gradient of the loss — it just adds the rank-1 update without checking what was already there. Delta rule is the gradient of the reconstruction loss — it first erases the component of that corresponds to , then writes the new . Same memory structure (rank-1 updates, outer-product state). Different objective. Different update.
5.6 Interpretation
The delta rule is online SGD on a reconstruction loss. That sentence subsumes everything: where the factor comes from (the gradient of with respect to ), why it has the form of “identity minus a rank-1 erase” (because the gradient of a quadratic is rank-1 in ), and why the rule is sometimes called targeted memory (it changes only the column direction of the current key). Every variant in Targeted Memory — the gated delta rule, KDA, Kimi Linear — is derived by changing the loss and re-applying the same SGD recipe.
6. The Chain Rule for a Gated Identity Path
6.1 Motivation
Gated Attention replaces a fixed residual with a learned gate , then traces what happens to the gradient as varies. To follow that trace we need the chain rule for a gated identity path — specifically, the gradient of with respect to when is a convex combination of and with coefficients that themselves depend on .
6.2 The chain rule (one-variable, refresher)
For composed scalar functions :
The derivative of a composition is the product of derivatives. Generalizes to vector inputs and outputs via the Jacobian — the matrix of partial derivatives.
6.3 The product rule (refresher)
For two scalar functions and :
Each factor is differentiated in turn while the other is held fixed; the results are summed.
6.4 Derivation: gradient of a gated identity
Consider the scalar form (the vector form is identical entry by entry). Let
where are differentiable. Apply the product rule to each of the two terms:
Collect:
Three pieces. The first is the identity-path gradient — the gradient of flowing through, weighted by how much of the gate is “open to identity”. The second is the transformed-path gradient — the gradient through , weighted by the gate. The third comes from the gate itself depending on ; it vanishes when the gate is constant.
6.5 Numerical check with the running example
We re-use the running setup as a 1D scalar instance. Take , (constant for simplicity, so ), and so . Then by the boxed formula:
Verify directly. With these choices, , so . At : .
6.6 Interpretation
The two paths sum because the gate distributes a fraction of the gradient through each. When the identity path dominates and the layer behaves like a pure skip connection (gradient flows unattenuated). When the transformed path dominates and the layer behaves like a non-residual transformation. This is why gated attention can interpolate between “pure residual” and “no residual” — and why the gate’s gradient itself (the term) vanishes precisely at the points where the two paths agree. Every gradient-flow argument in Gated Attention is an application of this single decomposition.
Summary
We first established the associative-memory picture behind the delta rule: writing a pair means adding a rank-1 outer product , and reading with a query returns . Exact retrieval happens only when the stored keys are orthonormal; otherwise there is interference, which is why a reconstruction loss makes sense in the first place.
We then built five tools, each used in a follow-up post. The Frobenius norm measures matrix size and reduces to the squared Euclidean norm for vectors. The reconstruction loss measures how badly the memory misremembers a stored pair. Matrix calculus gave us — a rank-1 outer product of the key and the error. Online SGD applied this gradient with step produces the delta rule , deriving the gated-deltanet update from the loss in three lines. Finally, the chain rule for a gated identity path decomposed the gradient of into identity, transformed, and gate-derivative pieces — the lens through which the gated-attention blog analyzes residual replacement.
With these tools in hand, we are ready for Targeted Memory: The Delta Rule, Gated DeltaNet, and Kimi Delta Attention, where every linear-RNN variant becomes the closed-form solution to a different optimization problem, and Gated Attention, where the gradient flow through learned gates is traced through exactly the decomposition above.
Previous: DeepSeek Sparse Attention: Learned Token Selection from Scratch
Next: Gated Attention: Replacing Residuals and ReLU with Learned Gates
Enjoyed this post?
Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.