Skip to content
Glacius
CalculusConcept reference

Squared Frobenius-Loss Gradients

Let Z contain model outputs and Y contain fixed targets of the same shape.

On this page 7 sections
  1. Overview
  2. A mean squared loss for matrix outputs
  3. One entry at a time
  4. Average exactly once
  5. Key takeaway
  6. Sources & further reading
  7. Concept connections

01A mean squared loss for matrix outputs#

Let ZZ contain model outputs and YY contain fixed targets of the same shape. The loss ZYF2/(2m)\|Z-Y\|_F^2/(2m) squares every entrywise error, sums them, and divides by a fixed positive scalar 2m2m.

In a batch, mm often counts examples. It need not count all scalar output entries. Read the stated loss reduction before differentiating.

02One entry at a time#

Entry ZijZ_{ij} appears in the term (ZijYij)2/(2m)(Z_{ij}-Y_{ij})^2/(2m). The power and chain rules give (ZijYij)/m(Z_{ij}-Y_{ij})/m. Placing these partials into their original positions gives

ZL=ZYm.\nabla_Z L=\frac{Z-Y}{m}.

Suppose ZZ has rows (3,0),(1,4)(3,0),(1,4) and YY has rows (1,1),(1,2)(1,1),(1,2), with m=2m=2. The residual rows are (2,1),(0,2)(2,-1),(0,2). The loss is (4+1+0+4)/4=2.25(4+1+0+4)/4=2.25.

The gradient rows are (1,0.5),(0,1)(1,-0.5),(0,1). Each entry indicates the local sensitivity to its matching prediction. A prediction already equal to its target contributes a zero gradient entry.

Check your reasoning

Z−Y has rows (2,−4),(0,6), and m=2. For L=||Z−Y||F²/(2m), gradient rows?

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

Divide every residual by m=2.

03Average exactly once#

Dividing by four output entries instead of two examples would give a different objective and gradient. Both conventions can be useful, but they cannot be mixed halfway through a calculation.

Check your reasoning

Which function is differentiated by the residual-over-m rule?

  1. AThe unsquared Frobenius norm divided by 2m
  2. BThe squared Frobenius norm divided by 2m
  3. CThe sum of signed residuals
Show answer and explanation
The squared Frobenius norm divided by 2m

Differentiating each squared error cancels the factor two.

The norm is squared. Differentiating the unsquared Frobenius norm would be a different operation. This gradient is with respect to predictions ZZ; reaching an earlier weight matrix requires another chain-rule step.

Key takeaway

Compute ∇_Z [||Z − Y||_F²/(2m)] = (Z − Y)/m for fixed real Y and a fixed positive scalar m.

  • Compute ∇_Z [||Z − Y||_F²/(2m)] = (Z − Y)/m for fixed real Y and a fixed positive scalar m.

Sources & further reading

  1. [1]
  2. [2]

Reference this concept

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

Glacius. “Squared Frobenius-Loss Gradients.” Math behind ML. /learn/c-frobenius-gradient