Skip to content
Glacius
CalculusConcept reference

Matrix Product: Right-Input Gradient

Let Z=AB, with fixed A of shape m\times d and variable B of shape d\times k.

On this page 7 sections
  1. Overview
  2. Accumulate across output rows
  3. Sum contributions from the batch
  4. Keep the incoming scale
  5. Key takeaway
  6. Sources & further reading
  7. Concept connections

01Accumulate across output rows#

Let Z=ABZ=AB, with fixed AA of shape m×dm\times d and variable BB of shape d×kd\times k. A scalar loss supplies G=ZLG=\nabla_ZL, of shape m×km\times k. We want one derivative for every entry of BB.

The entry BrjB_{rj} contributes to every output row in column jj. Its contribution to output ZijZ_{ij} is scaled by AirA_{ir}. Summing the loss sensitivities gives

LBrj=iAirGij.\frac{\partial L}{\partial B_{rj}}=\sum_i A_{ir}G_{ij}.

Collect those sums as

BL=ATG.\nabla_B L=A^TG.

02Sum contributions from the batch#

Let AA have rows (1,2),(2,0),(0,1)(1,2),(2,0),(0,1) and let GG have rows (2,1),(1,3),(2,4)(2,-1),(1,3),(-2,4). Entry (1,1)(1,1) of ATGA^TG is 1(2)+2(1)+0(2)=41(2)+2(1)+0(-2)=4.

The other entries give gradient rows (4,5),(2,2)(4,5),(2,2). The three output rows contribute to just two parameter rows. Shared parameters collect contributions from every example that uses them.

Check your reasoning

A has rows (1,2),(3,0); G is column (2,−1). Find ∇B L for Z=AB.

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

AᵀG gives 1×2+3×−1=−1 and 2×2=4.

03Keep the incoming scale#

The shape is (d×m)(m×k)=d×k(d\times m)(m\times k)=d\times k, matching BB. The transpose aligns the summation over output rows. Correct dimensions are a useful check, but the entrywise argument explains why this order is right.

If GG already represents an average loss, its averaging factor is already present in this sum. Dividing by mm again makes the update too small for the stated objective.

Check your reasoning

A draft uses AG instead of AᵀG for the right-input gradient. Which operation is missing?

  1. ATranspose G
  2. BTranspose A
  3. CDivide A by its row count
Show answer and explanation
Transpose A

The summation must run down each input feature column.

This rule treats the displayed AA as fixed for the partial derivative with respect to BB. If the same underlying parameter appears elsewhere in a computation, its other derivative paths must also be accumulated.

Key takeaway

Compute the right-input gradient AᵀG for Z = AB from a supplied incoming scalar-loss gradient G.

  • Compute the right-input gradient AᵀG for Z = AB from a supplied incoming scalar-loss gradient G.

Sources & further reading

  1. [1]
  2. [2]

Reference this concept

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

Glacius. “Matrix Product: Right-Input Gradient.” Math behind ML. /learn/c-matmul-right-backward