Skip to content
Glacius
CalculusConcept reference

Matrix Product: Left-Input Gradient

Let Z=AB, where A is m\times d and fixed B is d\times k.

On this page 7 sections
  1. Overview
  2. Send output sensitivities to the left input
  3. Collect all output columns
  4. Match the input
  5. Key takeaway
  6. Sources & further reading
  7. Concept connections

01Send output sensitivities to the left input#

Let Z=ABZ=AB, where AA is m×dm\times d and fixed BB is d×kd\times k. Suppose a scalar loss supplies the incoming gradient G=ZLG=\nabla_ZL, shaped m×km\times k. We want the gradient with respect to AA.

Output Zij=rAirBrjZ_{ij}=\sum_r A_{ir}B_{rj} depends on AirA_{ir} with derivative BrjB_{rj}. That input entry affects every output column in its row. Add their contributions:

LAir=jGijBrj.\frac{\partial L}{\partial A_{ir}}=\sum_j G_{ij}B_{rj}.

This is a row-column product with a transpose:

AL=GBT.\nabla_A L=GB^T.

02Collect all output columns#

Let BB have rows (1,2,0),(1,0,3)(1,2,0),(-1,0,3) and let GG have rows (2,1,1),(0,3,2)(2,1,-1),(0,3,2). For gradient entry (1,1)(1,1), compute 2(1)+1(2)1(0)=42(1)+1(2)-1(0)=4.

For entry (1,2)(1,2), compute 2(1)+1(0)1(3)=52(-1)+1(0)-1(3)=-5. The second row gives 66 and 66. The left-input gradient therefore has rows (4,5),(6,6)(4,-5),(6,6).

Check your reasoning

B has rows (1,2),(3,0), and G=(2,−1) is one row. For Z=AB, find ∇A L.

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

GBᵀ pairs G with each row of B: 2−2=0 and 6.

03Match the input#

The product has shape (m×k)(k×d)=m×d(m\times k)(k\times d)=m\times d, matching AA. An entrywise product cannot collect the contributions from several output columns.

Check your reasoning

A draft uses GB for the left gradient and dimensions happen to be square. What rule should replace it?

  1. AGᵀB
  2. BGBᵀ
  3. CBᵀGᵀ
Show answer and explanation
GBᵀ

The index sum pairs a G row with a B row, requiring Bᵀ.

Use GG exactly as supplied. If an earlier mean-loss derivative already divided by batch size, do not divide again here. This operation propagates sensitivities; it does not choose a new loss reduction.

Key takeaway

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

  • Compute the left-input gradient GBᵀ 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: Left-Input Gradient.” Math behind ML. /learn/c-matmul-left-backward