Matrix Calculus

Dive into Deep Learning · §34.4

Matrix Calculus
the Jacobian · chain rule & multiplication order · differential method · layer backward rules (VJPs) · softmax & cross-entropy

From Scalar Nodes to Vector and Matrix Layers

Motivation

  • 2-2-1 ReLU Network at \mathbf{x} = (1, 1)^\top: g(\mathbf{x}) = \mathbf{w}_2^\top\boldsymbol{\varphi}(\mathbf{W}_1\mathbf{x} + \mathbf{b}) gives \mathbf{a} = (3, -2)^\top, \mathbf{h} = (3, 0)^\top, and g = 6.
  • Three Distinct Derivative Shapes: Input Jacobian \partial g/\partial\mathbf{x} \in \mathbb{R}^{1 \times 2}, weight gradient \nabla_{\mathbf{W}_1}g \in \mathbb{R}^{2 \times 2}, and hidden-layer Jacobian \partial\mathbf{h}/\partial\mathbf{x} \in \mathbb{R}^{2 \times 2}.
  • Transpose Bug Hazard: A missing transpose on a square weight matrix raises no shape error; the differential method prevents it.

The Jacobian: Best Linear Map for Vector Functions

Definition & Entries

  • First-Order Vector Linearization: \mathbf{f}(\mathbf{x} + \boldsymbol{\delta}) = \mathbf{f}(\mathbf{x}) + \mathbf{J}_{\mathbf{f}}(\mathbf{x})\boldsymbol{\delta} + o(\|\boldsymbol{\delta}\|), where [\mathbf{J}_{\mathbf{f}}(\mathbf{x})]_{ij} = \frac{\partial f_i}{\partial x_j}(\mathbf{x}) \in \mathbb{R}^{m \times n}.
  • Row & Column Views: Row i is \nabla f_i(\mathbf{x})^\top; column j is \partial\mathbf{f}/\partial x_j.
  • Curved Map (x+\sin y, y+x^2/2)^\top at (0.5, 0.5)^\top: \mathbf{J}_{\mathbf{f}} = \left(\begin{smallmatrix}1 & \cos 0.5 \\ 0.5 & 1\end{smallmatrix}\right).

Geometry of the Jacobian: Circles Map to Ellipses

Singular Value Distortion

  • Local affine distortion: Near \mathbf{x}_0 = (0.5, 0.5)^\top, \mathbf{f}(\mathbf{x}_0 + \boldsymbol{\delta}) acts like the linear transformation \mathbf{f}(\mathbf{x}_0) + \mathbf{J}(\mathbf{x}_0)\boldsymbol{\delta}.
  • Singular values govern semi-axes: Any matrix maps a circle of radius r to an ellipse whose semi-axes are r\sigma_1 and r\sigma_2, where \sigma_1 \approx 1.706 and \sigma_2 \approx 0.329 are the singular values of \mathbf{J}(\mathbf{x}_0).
  • Quadratic convergence of the gap: At radius r = 0.35, the true image (blue) deviates from the Jacobian ellipse (orange dashed) by at most 0.061; halving the radius to r = 0.175 cuts the gap fourfold to 0.015.

Verifying Quadratic Remainder Decay of the Jacobian

Empirical Check

Shrinking the step \boldsymbol{\delta} tenfold at a time confirms that the relative linear approximation error \|\mathbf{f}(\mathbf{x}_0 + \boldsymbol{\delta}) - \mathbf{f}(\mathbf{x}_0) - \mathbf{J}(\mathbf{x}_0)\boldsymbol{\delta}\| / \|\boldsymbol{\delta}\| vanishes linearly with \|\boldsymbol{\delta}\|:

|delta| = 2.2e-01   error / |delta| = 9.0e-02
|delta| = 2.2e-02   error / |delta| = 9.0e-03
|delta| = 2.2e-03   error / |delta| = 9.0e-04
|delta| = 2.2e-04   error / |delta| = 9.0e-05

Sufficient condition for a Jacobian: Just as in §34.2, existence of partial derivatives alone is not enough (2xy/(x^2+y^2) at \mathbf{0} has none); if all partials \partial f_i / \partial x_j are continuous near \mathbf{x}, \mathbf{f} is differentiable at \mathbf{x} and smooth (C^2) maps have O(\|\boldsymbol{\delta}\|^2) error.

Numerator vs. Denominator Layout and Shape Rules

Shape Conventions

  • Jacobian \mathbf{J}_{\mathbf{f}} \in \mathbb{R}^{m \times n} (Numerator Layout): Composes directly via \mathbf{J}_{\mathbf{g}}\mathbf{J}_{\mathbf{f}} without transposes.
  • Gradient \nabla_{\mathbf{X}} f (Golden Shape Invariant): Always matches the exact shape of \mathbf{X} (column \mathbb{R}^{n \times 1} or matrix \mathbb{R}^{p \times q}) via df = \langle \nabla_{\mathbf{X}} f, d\mathbf{X}\rangle.
  • Hessian \mathbf{H}_f = \mathbf{J}_{\nabla f} \in \mathbb{R}^{n \times n}: Jacobian of the gradient map.

The Chain Rule for Jacobians: Matrix Multiplication

Composition of Maps

  • Matrix Chain Rule: If \mathbf{f} : \mathbb{R}^n \to \mathbb{R}^p and \mathbf{g} : \mathbb{R}^p \to \mathbb{R}^m are differentiable, then \mathbf{J}_{\mathbf{g} \circ \mathbf{f}}(\mathbf{x}) = \mathbf{J}_{\mathbf{g}}(\mathbf{f}(\mathbf{x}))\,\mathbf{J}_{\mathbf{f}}(\mathbf{x}).
  • Three-Line Proof: \boldsymbol{\eta} = \mathbf{A}\boldsymbol{\delta} + o(\|\boldsymbol{\delta}\|) \implies \mathbf{g}(\mathbf{y}+\boldsymbol{\eta}) = \mathbf{g}(\mathbf{y}) + \mathbf{B}\mathbf{A}\boldsymbol{\delta} + o(\|\boldsymbol{\delta}\|).
  • Path-Sum Link: [\mathbf{B}\mathbf{A}]_{ik} = \sum_{j=1}^p \frac{\partial g_i}{\partial y_j}\frac{\partial f_j}{\partial x_k}.

Jacobian of a Multilayer Perceptron

Layer Factors

  • Three composed layers in our 2-2-1 \mathrm{ReLU} network:
    1. Affine layer \mathbf{a} = \mathbf{W}_1\mathbf{x} + \mathbf{b} has Jacobian \mathbf{J}_1 = \mathbf{W}_1.
    2. Elementwise \mathrm{ReLU} \mathbf{h} = \boldsymbol{\varphi}(\mathbf{a}) decouples coordinates, so its Jacobian is diagonal: \mathbf{J}_2 = \operatorname{diag}(\mathbf{m}) with indicator mask m_i = \mathbf{1}(a_i > 0).
    3. Output layer g = \mathbf{w}_2^\top \mathbf{h} has 1 \times 2 row Jacobian \mathbf{J}_3 = \mathbf{w}_2^\top.
  • At \mathbf{x} = (1, 1)^\top (\mathbf{a} = (3, -2)^\top, \mathbf{m} = (1, 0)^\top): The product gives (2, 4): \mathbf{J}_g(\mathbf{x}) = \mathbf{w}_2^\top \operatorname{diag}(\mathbf{m})\,\mathbf{W}_1

Checking the 2-2-1 Network Jacobian in Code

Autograd Verification

For a K-layer network \mathbf{f} = \mathbf{f}_K \circ \cdots \circ \mathbf{f}_1, iterating the chain rule yields \mathbf{J} = \mathbf{J}_K \mathbf{J}_{K-1} \cdots \mathbf{J}_1, interleaving weight matrices \mathbf{W}_k with O(n) diagonal activation masks \operatorname{diag}(\boldsymbol{\varphi}_k'):

W1, b = torch.tensor([[1.0, 2.0], [3.0, -1.0]]), torch.tensor([0.0, -4.0])
w2, x = torch.tensor([2.0, -1.0]), torch.tensor([1.0, 1.0])

def g(x, W1, b, w2):                         # the 2-2-1 ReLU network
    return w2 @ torch.relu(W1 @ x + b)

m = (W1 @ x + b > 0).float()                 # the ReLU mask
print('by hand    :', (w2 @ torch.diag(m) @ W1).numpy())
print('by autograd:', torch.func.grad(g)(x, W1, b, w2).numpy())
by hand    : [2. 4.]
by autograd: [2. 4.]

Deep Jacobian Products: Vanishing & Exploding Norms

Spectral Scaling

  • Exponential Depth Scaling (\mathbf{J} = \mathbf{J}_K \cdots \mathbf{J}_1): For repeated \mathbf{W} = \mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{-1}, \mathbf{W}^K = \mathbf{V}\boldsymbol{\Lambda}^K\mathbf{V}^{-1} scales as |\lambda_{\max}|^K.
  • At Depth K = 50: |\lambda_{\max}| = 0.9 \implies 0.9^{50} \approx 0.005 (vanishing); |\lambda_{\max}| = 1.1 \implies 1.1^{50} \approx 117 (exploding).
  • Input vs. Weight Gradients: \mathbf{J}_K \cdots \mathbf{J}_1 gives \partial g/\partial\mathbf{x}; training needs \nabla_{\mathbf{W}_k}\ell.

Associativity and the 670× Cost Gap

Multiplication Order

  • Same product, different parenthesizations: To differentiate a scalar loss \ell, we multiply a 1 \times h row vector \mathbf{u}^\top = \partial\ell/\partial\mathbf{h}_3 into \mathbf{J}_3\mathbf{J}_2\mathbf{J}_1 (with h = n = 1000).
  • Left-to-right (Reverse Mode / VJP): (((\mathbf{u}^\top \mathbf{J}_3)\mathbf{J}_2)\mathbf{J}_1) keeps every intermediate a 1 \times h row vector: three matrix–vector products costing h^2 + h^2 + hn = 3 \times 10^6 multiplications.
  • Right-to-left (Matrix–Matrix trap): Forming \mathbf{J}_2\mathbf{J}_1 and \mathbf{J}_3(\mathbf{J}_2\mathbf{J}_1) first costs 2h^2n + hn = 2.001 \times 10^9 multiplications—a 667\times (2h/3) slowdown.

The Differential Method: One Rule Replaces Tables

First-Order Differentials

  • Step 1: Expand df: Apply linearity, matrix product rule d(\mathbf{A}\mathbf{B}) = (d\mathbf{A})\mathbf{B} + \mathbf{A}(d\mathbf{B}), transpose d(\mathbf{A}^\top) = (d\mathbf{A})^\top, and elementwise d\varphi(\mathbf{X}) = \varphi'(\mathbf{X})\odot d\mathbf{X}.
  • Step 2: Match Canonical Inner Product: Rearrange into df = \langle \mathbf{M}, d\mathbf{X}\rangle = \operatorname{tr}(\mathbf{M}^\top d\mathbf{X}) and read off \nabla_{\mathbf{X}} f = \mathbf{M}.

The Trace Move Rule for Isolating dX

Adjoint Algebra

  • Cyclic Trace Identity (\operatorname{tr}(\mathbf{U}\mathbf{V}) = \operatorname{tr}(\mathbf{V}\mathbf{U})):
    • Left multiplier: \langle \mathbf{A}, \mathbf{B}\mathbf{C}\rangle = \langle \mathbf{B}^\top\mathbf{A}, \mathbf{C}\rangle.
    • Right multiplier: \langle \mathbf{A}, \mathbf{B}\mathbf{C}\rangle = \langle \mathbf{A}\mathbf{C}^\top, \mathbf{B}\rangle.
    • Hadamard mask: \langle \mathbf{A}, \mathbf{B} \odot \mathbf{C}\rangle = \langle \mathbf{A} \odot \mathbf{B}, \mathbf{C}\rangle.

Deriving Linear, Quadratic, and Least-Squares Gradients

Worked Derivations

  • 1. Linear Form \mathbf{a}^\top\mathbf{x}: df = \langle \mathbf{a}, d\mathbf{x}\rangle \implies \nabla_{\mathbf{x}}(\mathbf{a}^\top\mathbf{x}) = \mathbf{a}.
  • 2. Quadratic Form \mathbf{x}^\top\mathbf{A}\mathbf{x}: df = \langle (\mathbf{A} + \mathbf{A}^\top)\mathbf{x}, d\mathbf{x}\rangle \implies \nabla_{\mathbf{x}}(\mathbf{x}^\top\mathbf{A}\mathbf{x}) = (\mathbf{A} + \mathbf{A}^\top)\mathbf{x} (= 2\mathbf{A}\mathbf{x} only if \mathbf{A} = \mathbf{A}^\top).
  • 3. Least Squares \|\mathbf{W}\mathbf{x} - \mathbf{y}\|_2^2: d\ell = \langle 2\mathbf{r}\mathbf{x}^\top, d\mathbf{W}\rangle \implies \nabla_{\mathbf{W}}\ell = 2(\mathbf{W}\mathbf{x} - \mathbf{y})\mathbf{x}^\top.

Sanity Checks: Shape, 1×1 Scalar, and Nonsymmetric Test

Debugging Matrix Gradients

  • 1. Shape Check: Catches missing transposes on rectangular matrices (p \neq q), but misses square matrices (p = q).
  • 2. 1 \times 1 Scalar Check: Catches missing factors of 2 and signs, but misses all transposes.
  • 3. Nonsymmetric Numerical Test: Comparing against autograd on a random nonsymmetric square matrix catches every bug.

The General Backward Rule (VJP) of a Layer

Layer Adjoints

  • No 10^{12}-Entry Tensor Jacobians: A layer \mathbf{Y} = \mathbf{F}(\mathbf{X}) maps the upstream adjoint \bar{\mathbf{Y}} = \nabla_{\mathbf{Y}}\ell directly to \bar{\mathbf{X}} = \nabla_{\mathbf{X}}\ell.
  • Adjoint Duality Identity: \bar{\mathbf{X}} is the unique matrix satisfying \langle \bar{\mathbf{X}}, d\mathbf{X}\rangle = \langle \bar{\mathbf{Y}}, d\mathbf{Y}\rangle for all d\mathbf{X}.
  • Vector VJP: When d\mathbf{y} = \mathbf{J}\,d\mathbf{x}, \bar{\mathbf{x}} = \mathbf{J}^\top\bar{\mathbf{y}}.

Backward Rules for Batched Matrix Multiplication

Core MLP Primitives

  • Batched Matmul (\mathbf{Y} = \mathbf{X}\mathbf{W}): \langle \bar{\mathbf{Y}}, (d\mathbf{X})\mathbf{W} + \mathbf{X}(d\mathbf{W})\rangle immediately gives \bar{\mathbf{X}} = \bar{\mathbf{Y}}\mathbf{W}^\top and \bar{\mathbf{W}} = \mathbf{X}^\top\bar{\mathbf{Y}}.
  • Elementwise & Bias Rules: \mathbf{Y} = \boldsymbol{\varphi}(\mathbf{X}) \implies \bar{\mathbf{X}} = \varphi'(\mathbf{X}) \odot \bar{\mathbf{Y}}; \mathbf{Z} = \mathbf{Y} + \mathbf{1}\mathbf{b}^\top \implies \bar{\mathbf{b}} = \bar{\mathbf{Z}}^\top\mathbf{1}.

Catching Square-Matrix Transpose Bugs in Code

Numerical Verification

We can test the matrix-product backward rules directly by differentiating the scalar probe s = \langle \bar{\mathbf{Y}}, \mathbf{X}\mathbf{W}\rangle on random square nonsymmetric 3 \times 3 matrices alongside plausible transpose errors of the same shape:

grad x^T A x: (A + A^T) x True | wrong guess 2 A x        False
Xbar        : Ybar W^T    True | wrong guess Ybar W       False
Wbar        : X^T Ybar    True | wrong guess (X^T Ybar)^T False

Every derived rule prints True, while every plausible wrong guess (2 A x, Ybar W, and (X^T Ybar)^T) prints False. Always test custom backward rules on nonsymmetric matrices.

Hand-Tracing All Weight Gradients of the 2-2-1 Network

Complete Layer Sweep

Applying our three local VJP rules backward from \bar{g} = 1 at \mathbf{x} = (1, 1)^\top (where \mathbf{a} = (3, -2)^\top, mask \mathbf{m} = (1, 0)^\top, \mathbf{h} = (3, 0)^\top) yields every parameter gradient of the 2-2-1 network:

  1. Output layer g = \mathbf{w}_2^\top\mathbf{h}: \bar{\mathbf{h}} = \bar{g}\mathbf{w}_2 = (2, -1)^\top and \bar{\mathbf{w}}_2 = \bar{g}\mathbf{h} = (3, 0)^\top.
  2. \mathrm{ReLU} & bias \mathbf{h} = \boldsymbol{\varphi}(\mathbf{a}), \mathbf{a} = \mathbf{W}_1\mathbf{x} + \mathbf{b}: \bar{\mathbf{a}} = \mathbf{m} \odot \bar{\mathbf{h}} = (2, 0)^\top and \bar{\mathbf{b}} = \bar{\mathbf{a}} = (2, 0)^\top.
  3. First linear layer: \bar{\mathbf{W}}_1 = \bar{\mathbf{a}}\mathbf{x}^\top = \begin{pmatrix} 2 & 2 \\ 0 & 0 \end{pmatrix} and \bar{\mathbf{x}} = \mathbf{W}_1^\top\bar{\mathbf{a}} = (2, 4)^\top.
dg/dW1 = (w2*m) x^T: [[2.0, 2.0], [0.0, 0.0]]   autograd: [[2.0, 2.0], [0.0, 0.0]]
dg/db  = w2*m      : [2.0, 0.0]   autograd: [2.0, 0.0]
dg/dw2 = h         : [3.0, 0.0]   autograd: [3.0, 0.0]

The Softmax Jacobian and Log-Sum-Exp Convexity

Classifier Derivatives

  • Softmax Jacobian: Differentiating p_i = e^{z_i}/\sum_k e^{z_k} gives \mathbf{J}_{\operatorname{softmax}}(\mathbf{z}) = \operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^\top, with null vector \mathbf{1} from shift invariance.
  • Statistical Proof of LSE Convexity: Since \mathbf{p} = \nabla\operatorname{LSE}(\mathbf{z}), \mathbf{v}^\top\mathbf{H}_{\operatorname{LSE}}\mathbf{v} = \operatorname{Var}_{I \sim \mathbf{p}}(v_I) \ge 0 everywhere.

Softmax + Cross-Entropy Collapses to p − y

Logit Gradient

  • Clean Cancellation of 1/p_i: Multiplying \frac{\partial\ell}{\partial p_i} = -\frac{y_i}{p_i} into \mathbf{J}_{\operatorname{softmax}} = \operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^\top cancels p_i in the denominator.
  • Logit Gradient Formula: \nabla_{\mathbf{z}}\ell = \bigl(\sum_{i=1}^C y_i\bigr)\mathbf{p} - \mathbf{y}, which collapses to \mathbf{p} - \mathbf{y} whenever \sum_{i=1}^C y_i = 1.

Verifying Softmax Spectrum and Two-Hot Target Pitfall

Code Verification

At logits \mathbf{z} = (1, -0.5, 2)^\top, the softmax Jacobian has one zero eigenvalue (along \mathbf{1}) and two positive eigenvalues, while an unnormalized two-hot target \mathbf{y} = (1, 1, 0)^\top (\sum_i y_i = 2) has gradient 2\mathbf{p} - \mathbf{y} rather than \mathbf{p} - \mathbf{y}:

eigenvalues of J: [0.    0.079 0.378]
y = [0. 1. 0.]: d loss / d z = [ 0.2537 -0.9434  0.6897], p - y = [ 0.2537 -0.9434  0.6897]
y = [1. 1. 0.]: d loss / d z = [-0.4926 -0.8868  1.3793], p - y = [-0.7463 -0.9434  0.6897]

Concept Check: Matrix Calculus

Review & Practice

Use the differential method, trace move rule, and Jacobian chain rule to answer these three questions:

  1. Matrix factorization gradient: For loss \ell(\mathbf{U}, \mathbf{V}) = \tfrac{1}{2}\|\mathbf{U}\mathbf{V} - \mathbf{X}\|_F^2 with residual \mathbf{R} = \mathbf{U}\mathbf{V} - \mathbf{X} \in \mathbb{R}^{m \times n}, \mathbf{U} \in \mathbb{R}^{m \times r}, and \mathbf{V} \in \mathbb{R}^{r \times n}, use d\ell = \langle \mathbf{R}, (d\mathbf{U})\mathbf{V} + \mathbf{U}(d\mathbf{V})\rangle to find \nabla_{\mathbf{U}}\ell and \nabla_{\mathbf{V}}\ell.
  2. Softmax VJP complexity: In self-attention over C classes/tokens, forming \mathbf{J}_{\operatorname{softmax}} \in \mathbb{R}^{C \times C} and multiplying by \mathbf{u} takes O(C^2) operations. How fast can \mathbf{J}_{\operatorname{softmax}}^\top\mathbf{u} = (\operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^\top)\mathbf{u} be evaluated without forming \mathbf{J}?
  3. Dead ReLU parameters: In our 2-2-1 \mathrm{ReLU} network at \mathbf{x} = (1, 1)^\top, hidden unit 2 had pre-activation a_2 = -2 < 0. Which parameters of unit 2 received a nonzero gradient on this example?

Answers:

  1. Applying the trace move rule gives d\ell = \langle \mathbf{R}\mathbf{V}^\top, d\mathbf{U}\rangle + \langle \mathbf{U}^\top\mathbf{R}, d\mathbf{V}\rangle, so \nabla_{\mathbf{U}}\ell = \mathbf{R}\mathbf{V}^\top \in \mathbb{R}^{m \times r} and \nabla_{\mathbf{V}}\ell = \mathbf{U}^\top\mathbf{R} \in \mathbb{R}^{r \times n} (both matching their variable shapes).
  2. Rewriting (\operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^\top)\mathbf{u} = \mathbf{p} \odot \bigl(\mathbf{u} - (\mathbf{p}^\top\mathbf{u})\mathbf{1}\bigr) requires only one dot product \mathbf{p}^\top\mathbf{u}, one broadcast subtraction, and one elementwise multiply: O(C) time and memory instead of O(C^2).
  3. None of them: row 2 of \bar{\mathbf{W}}_1 is (0, 0), \bar{b}_2 = 0 (because m_2 = 0), and \bar{w}_{2,2} = h_2 = 0. If a \mathrm{ReLU} unit is negative across the entire training set, gradient descent never updates its weights (“dead \mathrm{ReLU}”).

Summary: Matrix Calculus & Layer Backward Rules

Key Takeaways

Operation / Concept Forward Formula Jacobian / Backward VJP Rule
Vector Map Linearization \mathbf{y} = \mathbf{f}(\mathbf{x}) : \mathbb{R}^n \to \mathbb{R}^m \mathbf{f}(\mathbf{x}+\boldsymbol{\delta}) \approx \mathbf{f}(\mathbf{x}) + \mathbf{J}_{\mathbf{f}}\boldsymbol{\delta}, [\mathbf{J}_{\mathbf{f}}]_{ij} = \frac{\partial f_i}{\partial x_j}
Chain Rule & VJP Order \mathbf{f} = \mathbf{f}_K \circ \cdots \circ \mathbf{f}_1 \mathbf{u}^\top\mathbf{J} = ((\mathbf{u}^\top\mathbf{J}_K)\cdots)\mathbf{J}_1 (O(Kh^2) vs. O(Kh^3) right-to-left)
Differential & Move Rule df = \langle \nabla_{\mathbf{X}}f, d\mathbf{X}\rangle = \operatorname{tr}((\nabla_{\mathbf{X}}f)^\top d\mathbf{X}) \langle \mathbf{A}, \mathbf{B}\mathbf{C}\rangle = \langle \mathbf{B}^\top\mathbf{A}, \mathbf{C}\rangle = \langle \mathbf{A}\mathbf{C}^\top, \mathbf{B}\rangle
Quadratic Form f(\mathbf{x}) = \mathbf{x}^\top\mathbf{A}\mathbf{x} \nabla_{\mathbf{x}} f = (\mathbf{A} + \mathbf{A}^\top)\mathbf{x} (= 2\mathbf{A}\mathbf{x} iff \mathbf{A} = \mathbf{A}^\top)
Batched Linear Layer \mathbf{Y} = \mathbf{X}\mathbf{W} + \mathbf{1}\mathbf{b}^\top \bar{\mathbf{X}} = \bar{\mathbf{Y}}\mathbf{W}^\top, \bar{\mathbf{W}} = \mathbf{X}^\top\bar{\mathbf{Y}}, \bar{\mathbf{b}} = \bar{\mathbf{Y}}^\top\mathbf{1}
Elementwise Activation \mathbf{Y} = \boldsymbol{\varphi}(\mathbf{X}) \bar{\mathbf{X}} = \varphi'(\mathbf{X}) \odot \bar{\mathbf{Y}} (diagonal mask in O(n) ops)
Softmax & Cross-Entropy \mathbf{p} = \operatorname{softmax}(\mathbf{z}), \ell = -\mathbf{y}^\top\log\mathbf{p} \mathbf{J}_{\operatorname{softmax}} = \operatorname{diag}(\mathbf{p}) - \mathbf{p}\mathbf{p}^\top; \nabla_{\mathbf{z}}\ell = \mathbf{p} - \mathbf{y} (when \sum_i y_i = 1)