Skip to content
Glacius
CalculusConcept reference

Least-Squares Gradients

Consider the scalar objective L(x)=\tfrac12\|Ax-b\|_2^2, with fixed A and b.

On this page 7 sections
  1. Overview
  2. Send residuals back to the parameters
  3. Follow the intermediate values
  4. Interpret the transpose
  5. Key takeaway
  6. Sources & further reading
  7. Concept connections

01Send residuals back to the parameters#

Consider the scalar objective L(x)=12Axb22L(x)=\tfrac12\|Ax-b\|_2^2, with fixed AA and bb. The prediction is AxAx and the residual is r=Axbr=Ax-b. The residual has one entry per output; the gradient needs one entry per parameter.

The half-squared norm is 12iri2\tfrac12\sum_i r_i^2, so its gradient with respect to rr is rr. The derivative of the residual map AxbAx-b is AA. Reverse-mode differentiation sends the output sensitivity back through its transpose.

xL=AT(Axb).\nabla_xL=A^T(Ax-b).

02Follow the intermediate values#

Let AA have rows (1,2),(2,0),(0,1)(1,2),(2,0),(0,1), b=(2,4,1)b=(2,4,1), and x=(0,0)x=(0,0). Predictions are (0,0,0)(0,0,0) and residuals are (2,4,1)(-2,-4,-1).

The first gradient entry is 1(2)+2(4)+0(1)=101(-2)+2(-4)+0(-1)=-10. The second is 2(2)+0(4)+1(1)=52(-2)+0(-4)+1(-1)=-5. Thus the gradient is (10,5)(-10,-5), while the loss is (4+16+1)/2=10.5(4+16+1)/2=10.5.

Check your reasoning

A has rows (1,2),(0,1), x=(1,0), b=(0,2). For L=||Ax−b||²/2, find ∇L.

  1. A(1,0)
  2. B(1,−2)
  3. C(1,2)
Show answer and explanation
(1,0)

Ax−b=(1,−2); Aᵀ times that residual is (1,0).

03Interpret the transpose#

For AA of shape m×dm\times d, the residual is an mm-vector. Multiplying by ATA^T, of shape d×md\times m, collects residual contributions into a dd-vector. Each parameter receives contributions from every output it affects.

If the objective is the mean half-squared loss, divide both loss and gradient by mm. The gradient above is for the stated sum. A different reduction changes its scale.

Check your reasoning

A draft differentiates ||Ax−b||²/2 as 2Aᵀ(Ax−b). What factor should be removed?

  1. AThe transpose
  2. BThe leading 2
  3. CThe residual
Show answer and explanation
The leading 2

The half in the objective cancels the square derivative factor.

A zero gradient does not require a zero residual. It means the residual is perpendicular to every column of AA. This connects the derivative calculation to the earlier geometry of the closest attainable output.

Key takeaway

Compute the gradient of one-half the squared Euclidean residual norm ||Ax − b||² as Aᵀ(Ax − b) for fixed A and b.

  • Compute the gradient of one-half the squared Euclidean residual norm ||Ax − b||² as Aᵀ(Ax − b) for fixed A and b.

Sources & further reading

  1. [1]
  2. [2]

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Least-Squares Gradients.” Math behind ML. /learn/c-least-squares-gradient