Skip to main content

MATH 345: Linear Algebra and Optimization

Section 3.6 Chain rule as matrix multiplication

Unit 1 used matrix multiplication to represent composition of linear maps: applying \(B\) first and then \(A\) gives \(A(B\mathbf{x})=(AB)\mathbf{x}\text{.}\) For differentiable nonlinear maps, the same idea reappears locally. The local linear maps compose, so their Jacobian matrices multiply.

Subsection The chain rule

The chain rule describes derivatives of composite functions.

Subsubsection Compositions

Definition 3.6.1. Composite Function.
If \(G : \R^n \to \R^p\) and \(F : \R^p \to \R^m\) are multivariable functions, we can form a new composite function as follows. Take an input in \(\R^n\text{,}\) and then apply \(G\) to the input to obtain a vector in \(\R^p\text{.}\) Apply \(F\) to this vector to obtain a vector \(\R^m\text{.}\) This is the output of the composite function. In other words, our input is fed through the following diagram
\begin{equation*} \R^n \xrightarrow{G} \R^p \xrightarrow{F} \R^m\text{.} \end{equation*}
We write the composite function as \(F \circ G\) (the symbol \(\circ\) should be read as β€œcomposed with”). In symbols, the composite function is defined as
\begin{equation*} (F \circ G)(\mathbf{x}) = F(G(\mathbf{x}))\text{.} \end{equation*}
Note 3.6.2.
Note that the composite \(F \circ G\) is only defined when the output of \(G\) is a possible input to \(F\text{,}\) i.e., the output of \(G\) is \(\R^p\text{,}\) which is the space of inputs to \(F\text{.}\)
Unit 1 used matrix multiplication to represent composition of linear maps: applying \(B\) first and then \(A\) gives \(A(B\mathbf{x})=(AB)\mathbf{x}\text{.}\)
Activity 3.6.1.
Let \(T_1: \R^2 \to \R^3\) and \(T_2: \R^3 \to \R^2\) be linear maps defined by their component functions as follows:
\begin{equation*} T_1(x,y) = \begin{bmatrix} 2x-y \\ 3y \\ x+y \end{bmatrix} \qquad T_2(u,v,w) = \begin{bmatrix} u + v - w \\ 2u - v \end{bmatrix}\text{.} \end{equation*}
(a)
Find the matrices \(A_1\) and \(A_2\) associated with these maps, so that \(T_1 = T_{A_1}\) and \(T_2 = T_{A_2}\) (recall the notation from DefinitionΒ 1.3.14).
Solution.
We recall the method of TheoremΒ 1.3.17. In particular, we obtain the first column of \(A_1\) from the entries of \(T_1(\mathbf{e}_1)\text{,}\) and the second column from \(T_1(\mathbf{e}_2)\text{.}\) We calculate that
\begin{equation*} T_1(\mathbf{e}_1) = T_1(1,0) = \begin{bmatrix} 2 \cdot 1 - 0 \\ 3 \cdot 0 \\ 1 + 0 \end{bmatrix} = \begin{bmatrix} 2 \\ 0 \\ 1 \end{bmatrix} \end{equation*}
and that
\begin{equation*} T_1(\mathbf{e}_2) = T_1(0,1) = \begin{bmatrix} 2 \cdot 0 - 1 \\ 3 \cdot 1 \\ 0 + 1 \end{bmatrix} = \begin{bmatrix} -1 \\ 3 \\ 1 \end{bmatrix}\text{.} \end{equation*}
So the matrix \(A_1\) is equal to
\begin{equation*} A_1 = \begin{bmatrix} 2 \amp -1 \\ 0 \amp 3 \\ 1 \amp 1 \end{bmatrix}\text{.} \end{equation*}
Similarly, we calculate that
\begin{equation*} T_2(\mathbf{e}_1) = T_2(1,0,0) = \begin{bmatrix} 1 + 0 - 0 \\ 2 \cdot 1 - 0 \end{bmatrix} = \begin{bmatrix} 1 \\ 2 \end{bmatrix}\text{,} \end{equation*}
that
\begin{equation*} T_2(\mathbf{e}_2) = T_2(0,1,0) = \begin{bmatrix} 0 + 1 - 0 \\ 2 \cdot 0 - 1 \end{bmatrix} = \begin{bmatrix} 1 \\ -1 \end{bmatrix}\text{,} \end{equation*}
and that
\begin{equation*} T_2(\mathbf{e}_3) = T_2(0,0,1) = \begin{bmatrix} 0 + 0 - 1 \\ 2 \cdot 0 - 0 \end{bmatrix} = \begin{bmatrix} -1 \\ 0 \end{bmatrix}\text{.} \end{equation*}
So the matrix \(A_2\) is given by
\begin{equation*} A_2 = \begin{bmatrix} 1 \amp 1 \amp -1 \\ 2 \amp -1 \amp 0 \end{bmatrix}\text{.} \end{equation*}
(b)
Calculate the composite map \(T_2 \circ T_1\) and express it as a matrix map from \(\R^2\) to \(\R^2\text{.}\)
Solution.
Recalling ActivityΒ 1.4.3, we have that \(T_2 \circ T_1 = T_{A_2 A_1}\text{.}\) Now
\begin{align*} A_2 \cdot A_1 \amp= \begin{bmatrix} 1 \amp 1 \amp -1 \\ 2 \amp -1 \amp 0 \end{bmatrix} \begin{bmatrix} 2 \amp -1 \\ 0 \amp 3 \\ 1 \amp 1 \end{bmatrix}\\ \amp= \begin{bmatrix} 1 \cdot 2 + 1 \cdot 0 + (-1) \cdot 1 \amp 1 \cdot (-1) + 1 \cdot 3 + (-1) \cdot 1 \\ 2 \cdot 2 + (-1) \cdot 0 + 0 \cdot 1 \amp 2 \cdot (-1) + (-1) \cdot 3 + 0 \cdot 1 \end{bmatrix}\\ \amp= \begin{bmatrix} 2 + 0 - 1 \amp -1 + 3 - 1 \\ 4 + 0 + 0 \amp -2 - 3 + 0 \end{bmatrix}\\ \amp= \begin{bmatrix} 1 \amp 1 \\ 4 \amp -5 \end{bmatrix} \end{align*}
Therefore, the composite map \(T_2 \circ T_1: \R^2 \to \R^2\) can be expressed as:
\begin{equation*} (T_2 \circ T_1) \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 1 \amp 1 \\ 4 \amp -5 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} x + y \\ 4x - 5y \end{bmatrix} \end{equation*}
Here is a nonlinear example of the composition of two functions.
Activity 3.6.2.
Consider \(F : \R^2 \to \R^2\) and \(G : \R^2 \to \R^2\) defined by
\begin{equation*} F(u, v) = (uv, u + v) \quad\text{and}\quad G(x, y) = (e^{xy}, x - y)\text{.} \end{equation*}
Find expressions for \((F \circ G)(x, y)\) and \((G \circ F)(u, v)\text{.}\)
Solution.
We calculate that
\begin{align*} (F \circ G)(x, y) \amp= F(G(x, y))\\ \amp= F(e^{xy}, x - y)\\ \amp= (e^{xy}(x - y), e^{xy} + (x - y))\\ \amp= (e^{xy}x - e^{xy}y, e^{xy} + x - y)\text{,} \end{align*}
and that
\begin{align*} (G \circ F)(u, v) \amp= G(F(u, v))\\ \amp= G(uv, u + v)\\ \amp= (e^{uv(u+v)}, uv - (u + v))\\ \amp= (e^{u^2v + uv^2}, uv - u - v)\text{.} \end{align*}
Warning 3.6.3.
Observe that \(F \circ G\) and \(G \circ F\) are very different functions. We must therefore be careful that we compose functions in the correct order.
The order matters. The rightmost Jacobian matrix acts first on the input change.
Activity 3.6.3.
Consider \(\mathbf{r} : \R \to \R^3\) and \(\mathbf{p} : \R^3 \to \R^2\) defined by
\begin{equation*} \mathbf{r}(t) = (t, \cos(t), \sin(t)) \quad\text{and}\quad \mathbf{p}(x, y, z) = (y, z) \end{equation*}
(a)
Find an expression for \((\mathbf{p} \circ \mathbf{r})(t)\text{.}\)
Solution.
We calculate that
\begin{align*} (\mathbf{p} \circ \mathbf{r})(t) \amp= \mathbf{p}(\mathbf{r}(t))\\ \amp= \mathbf{p}(t, \cos(t), \sin(t))\\ \amp= (\cos(t), \sin(t))\text{.} \end{align*}
(b)
Is the composition \(\mathbf{r} \circ \mathbf{p}\) defined? Why or why not?
Solution.
For the composition \(\mathbf{r} \circ \mathbf{p}\) to be defined, the outputs of \(\mathbf{p}\) must be contained in the inputs of \(\mathbf{r}\text{.}\) But \(\mathbf{p}\) outputs points in \(\R^2\text{,}\) whereas \(\mathbf{r}\) takes in points in \(\R\) as input, so the composition does not make sense.

Subsubsection Chain rule for one-parameter inputs

Recall the chain rule from single-variable calculus, that for two functions \(f: \R \to \R\) and \(g: \R \to \R\text{,}\) we have
\begin{equation*} (f \circ g)'(t) = (f' \circ g)(t) g'(t)\text{.} \end{equation*}
The next theorem is the component form of the chain rule.
Recall that for a function \(f(x,y)\text{,}\) \(D_1 f = f_x = \frac{\partial f}{\partial x}\text{,}\) and that \(D_2 f = f_y = \frac{\partial f}{\partial y}\text{.}\)
Why is this true?.
We note that the right hand side of the equation can be written as \(L \circ \mathbf{r} - f(\mathbf{r}(t_0))\text{,}\) where \(L\) is the linear approximation of \(f\) at \(\mathbf{r}(t_0)\text{.}\) Because \(f\) is differentiable, we know that
\begin{equation*} \lim_{\mathbf{x} \to \mathbf{r}(t_0)} \frac{f(\mathbf{x}) - L(\mathbf{x})}{\| \mathbf{x} - \mathbf{r}(t_0) \|} = 0\text{.} \end{equation*}
Now
\begin{equation*} \lim_{t \to t_0} \frac{\| \mathbf{r}(t) - \mathbf{r}(t_0) \|}{t - t_0} = \left\| \lim_{t \to t_0} \frac{\mathbf{r}(t) - \mathbf{r}(t_0)}{t - t_0} \right\| = \| \mathbf{r}'(t_0) \|\text{.} \end{equation*}
Using these two equations (substituting \(\mathbf{r}(t)\) for \(\mathbf{x}\) in the first equation), we find that
\begin{align*} \lim_{t \to t_0} \frac{f(\mathbf{r}(t)) - L(\mathbf{r}(t))}{t - t_0} \amp = \lim_{t \to t_0} \frac{f(\mathbf{r}(t)) - L(\mathbf{r}(t))}{\| \mathbf{r}(t) - \mathbf{r}(t_0) \|} \frac{\| \mathbf{r}(t) - \mathbf{r}(t_0) \|}{t - t_0}\\ \amp = 0 \cdot \| \mathbf{r}'(t_0) \| = 0\text{.} \end{align*}
But recalling that
\begin{equation*} L(x,y) = f(\mathbf{r}(t_0)) + D_1 f(\mathbf{r}(t_0)) (x - r_1(t_0)) + D_2 f(\mathbf{r}(t_0)) (y - r_2(t_0))\text{,} \end{equation*}
we conclude that
\begin{align*} \lim_{t \to t_0} \frac{f(\mathbf{r}(t)) - f(\mathbf{r}(t_0))}{t - t_0} \amp = \lim_{t \to t_0} \frac{f(\mathbf{r}(t)) - f(\mathbf{r}(t_0))}{t - t_0} - \frac{f(\mathbf{r}(t)) - L(\mathbf{r}(t))}{t - t_0}\\ \amp = \lim_{t \to t_0} \frac{L(\mathbf{r}(t)) - f(\mathbf{r}(t_0))}{t - t_0}\\ \amp = \lim_{t \to t_0} D_1 f(\mathbf{r}(t_0)) \frac{r_1(t) - r_1(t_0)}{t - t_0}\\ \amp \quad\quad + D_2 f(\mathbf{r}(t_0)) \frac{r_2(t) - r_2(t_0)}{t - t_0}\\ \amp = f_x(\mathbf{r}(t_0)) r_1'(t_0) + D_2 f(\mathbf{r}(t_0)) r_2'(t_0)\text{.} \end{align*}
Note 3.6.5.
There are several ways to aid in remembering the formula for the chain rule. The first is to simplify the equation, writing it as
\begin{equation*} (f \circ \mathbf{r})'(t) = (\nabla f \circ \mathbf{r})(t) \cdot \mathbf{r}'(t), \end{equation*}
so that the equation looks more like the chain rule from single variable calculus. The second is the following mnemonic: if we write \(z\) for \(f \circ \mathbf{r}\) and for \(f\text{,}\) and write the components of \(\mathbf{r}\) as \(x\) and \(y\text{,}\) then the chain rule reads that
\begin{equation*} \frac{dz}{dt} = \frac{\partial z}{\partial x} \frac{dx}{dt} + \frac{\partial z}{\partial y} \frac{dy}{dt}\text{,} \end{equation*}
so that the symbols \(dx\) and \(dy\) in the numerators on the right hand side, and the symbols \(\partial x\) and \(\partial y\) that occur in the denominators appear to β€˜cancel’ like when multiplying fractions.
Activity 3.6.4.
Suppose \(z = x^2y - y^2x\text{,}\) where \(x = \sin t\) and \(y = e^t\text{.}\) Compute \(dz/dt\text{.}\)
Solution.
First, we need to calculate
\begin{align*} \frac{\partial z}{\partial x} \amp= \frac{\partial}{\partial x}(x^2y - y^2x) = 2xy - y^2\\ \frac{\partial z}{\partial y} \amp= \frac{\partial}{\partial y}(x^2y - y^2x) = x^2 - 2yx\\ \frac{dx}{dt} \amp= \frac{d}{dt}(\sin t) = \cos t\\ \frac{dy}{dt} \amp= \frac{d}{dt}(e^t) = e^t \end{align*}
Now we substitute these into the chain rule formula:
\begin{align*} \frac{dz}{dt} \amp= (2xy - y^2) \cdot \cos t + (x^2 - 2yx) \cdot e^t\\ \amp= (2 \sin t \cdot e^t - e^{2t}) \cdot \cos t + (\sin^2 t - 2e^t \cdot \sin t) \cdot e^t\\ \amp= 2 \sin t \cdot e^t \cdot \cos t - e^{2t} \cdot \cos t + e^t \sin^2 t - 2e^{2t} \sin t \end{align*}
We can simplify this further if we want:
\begin{align*} \frac{dz}{dt} \amp= 2 e^t \sin t \cos t - e^{2t} \cos t + e^t \sin^2 t - 2e^{2t} \sin t\\ \amp= e^t (\sin^2 t + 2\sin t \cos t) - e^{2t}(\cos t + 2\sin t)\\ \amp= e^t (\sin^2 t + \sin 2t) - e^{2t}(\cos t + 2\sin t) \end{align*}
Therefore,
\begin{equation*} \frac{dz}{dt} = e^t (\sin^2 t + \sin 2t) - e^{2t}(\cos t + 2\sin t) \end{equation*}
Activity 3.6.5.
If \(f(x,y) = x^2 + y^2\text{,}\) and \(\mathbf{r}(t) = (\sin t, \cos(2t))\text{,}\) find \((f \circ \mathbf{r})'(t)\text{.}\)
Solution.
We calculate that
\begin{equation*} \nabla f(x,y) = \begin{bmatrix} 2x \\ 2y \end{bmatrix} \end{equation*}
and that
\begin{equation*} \mathbf{r}'(t) = \begin{bmatrix} \cos t \\ - 2 \sin(2t) \end{bmatrix}\text{.} \end{equation*}
Now
\begin{equation*} \nabla f \circ \mathbf{r}(t) = \nabla f(\sin t, \cos(2t)) = \begin{bmatrix} 2 \sin t \\ 2 \cos(2t) \end{bmatrix} \end{equation*}
Thus
\begin{align*} (f \circ \mathbf{r})'(t) \amp = \nabla f(\mathbf{r}(t)) \cdot \mathbf{r}'(t)\\ \amp = \begin{bmatrix} 2 \sin t \\ 2 \cos(2t) \end{bmatrix} \cdot \begin{bmatrix} \cos t \\ -2 \sin(2t) \end{bmatrix}\\ \amp = 2 \sin t \cos t - 4 \sin(2t) \cos(2t)\text{.} \end{align*}

Subsubsection Chain rule for two independent variables

Why is this true?.
Fix the second variable, and apply TheoremΒ 3.6.4 to the first to obtain the formula for \((f \circ G)_x\text{.}\) Then fix the first variable and apply TheoremΒ 3.6.4 to the second variable to obtain the formula for \((f \circ G)_y\text{.}\)
Remark 3.6.7.
Recall from Section 3.4 that the Jacobian matrix collects the partial derivatives of a vector-valued map. For \(G\text{,}\) this gives
\begin{equation*} J_G = \begin{bmatrix} D_1 G_1 \amp D_2 G_1 \\ D_1 G_2 \amp D_2 G_2 \end{bmatrix}\text{.} \end{equation*}
Then we can write the pair of equations in TheoremΒ 3.6.6 as the single vector equation
\begin{equation*} \nabla (f \circ G) = J_G^T (\nabla f \circ G)\text{,} \end{equation*}
where we interpret \(\nabla f\) as a column vector.
The second method is mnemonic. If we let \(x\) and \(y\) be the variables of \(f\text{,}\) and \(u\) and \(v\) be the variables of \(G\text{,}\) and we write \(z\) for \(f \circ G\) and for \(f\text{,}\) and write the components of \(G(u,v)\) as \(x\) and \(y\text{,}\) then the chain rule reads that
\begin{equation*} \frac{\partial f}{\partial u} = \frac{\partial f}{\partial x} \frac{\partial x}{\partial u} + \frac{\partial f}{\partial y} \frac{\partial y}{\partial u} \end{equation*}
and that
\begin{equation*} \frac{\partial f}{\partial v} = \frac{\partial f}{\partial x} \frac{\partial x}{\partial v} + \frac{\partial f}{\partial y} \frac{\partial y}{\partial v}\text{,} \end{equation*}
so that the partials in the numerator and denominator again appear to β€˜cancel’.
Activity 3.6.6.
Suppose that
\begin{equation*} z = x^2 + xy - y^2 \end{equation*}
that \(x = e^{2s + t}\text{,}\) and that \(y = \ln(t/s)\text{.}\) Compute
\begin{equation*} \frac{\partial z}{\partial s} \quad\text{and}\quad \frac{\partial z}{\partial t}\text{.} \end{equation*}
(a)
First compute the partial derivatives of \(z\) in \(x\) and \(y\text{,}\) and the partial derivatives of \(x\) and \(y\) in \(t\) and \(s\text{.}\)
Solution.
We calculate that
\begin{equation*} \frac{\partial z}{\partial x} = \frac{\partial}{\partial x}(x^2 + xy - y^2) = 2x + y\text{,} \end{equation*}
that
\begin{equation*} \frac{\partial z}{\partial y} = \frac{\partial}{\partial y}(x^2 + xy - y^2) = x - 2y\text{,} \end{equation*}
that
\begin{equation*} \frac{\partial x}{\partial s} = \frac{\partial}{\partial s}(e^{2s+t}) = 2e^{2s+t}\text{,} \end{equation*}
that
\begin{equation*} \frac{\partial x}{\partial t} = \frac{\partial}{\partial t}(e^{2s+t}) = e^{2s+t}\text{,} \end{equation*}
that
\begin{equation*} \frac{\partial y}{\partial s} = \frac{\partial}{\partial s}\left(\ln\left(\frac{t}{s}\right)\right) = \frac{\partial}{\partial s}(\ln(t) - \ln(s)) = -\frac{1}{s}\text{,} \end{equation*}
and that
\begin{equation*} \frac{\partial y}{\partial t} = \frac{\partial}{\partial t}\left(\ln\left(\frac{t}{s}\right)\right) = \frac{1}{t} \end{equation*}
(b)
Now use the chain rule to compute the partial derivatives of \(z\) in \(s\) and \(t\text{.}\)
Solution.
Now we apply the chain rule, from which we conclude that
\begin{align*} \frac{\partial z}{\partial s} \amp= \frac{\partial z}{\partial x}\frac{\partial x}{\partial s} + \frac{\partial z}{\partial y}\frac{\partial y}{\partial s}\\ \amp= (2x + y) \cdot 2e^{2s+t} + (x - 2y) \cdot (-1/s)\\ \amp= 4xe^{2s+t} + 2ye^{2s+t} - (x/s) + (2y/s) \end{align*}
Substituting \(x = e^{2s+t}\) and \(y = \ln(t/s)\text{:}\)
\begin{align*} \frac{\partial z}{\partial s} \amp= 4e^{2s+t} \cdot e^{2s+t} + 2 \ln(t/s) \cdot e^{2s+t} - \frac{e^{2s+t}}{s} + \frac{2\ln(t/s)}{s}\\ \amp= 4e^{4s+2t} + 2e^{2s+t}\ln(t/s) - \frac{e^{2s+t}}{s} + \frac{2\ln(t/s)}{s} \end{align*}
Similarly for \(\frac{\partial z}{\partial t}\text{:}\)
\begin{align*} \frac{\partial z}{\partial t} \amp= \frac{\partial z}{\partial x}\frac{\partial x}{\partial t} + \frac{\partial z}{\partial y}\frac{\partial y}{\partial t}\\ \amp= (2x + y) \cdot e^{2s+t} + (x - 2y) \cdot \frac{1}{t}\\ \amp= 2xe^{2s+t} + ye^{2s+t} + \frac{x}{t} - \frac{2y}{t} \end{align*}
Substituting \(x = e^{2s+t}\) and \(y = \ln(t/s)\text{:}\)
\begin{align*} \frac{\partial z}{\partial t} \amp= 2e^{2s+t} \cdot e^{2s+t} + \ln(t/s) \cdot e^{2s+t} + \frac{e^{2s+t}}{t} - \frac{2\ln(t/s)}{t}\\ \amp= 2e^{4s+2t} + e^{2s+t}\ln(t/s) + \frac{e^{2s+t}}{t} - \frac{2\ln(t/s)}{t} \end{align*}
Activity 3.6.7.
Use the Jacobian matrix version of the chain rule to calculate the partial derivatives of the function \(f \circ G\text{,}\) where \(f(x,y) = 3x^2 - 2xy + y^2\text{,}\) and \(G(u,v) = (3u + 2v, 4u - v)\text{.}\)
Solution.
We compute that
\begin{equation*} \nabla f(x,y) = \begin{bmatrix} 6x - 2y \\ -2x + 2y \end{bmatrix} \end{equation*}
and that
\begin{equation*} J_G = \begin{bmatrix} D_1 \{ 3u + 2v \} \amp D_2 \{ 3u + 2v \} \\ D_1 \{ 4u - v \} \amp D_2 \{ 4u - v \} \end{bmatrix} = \begin{bmatrix} 3 \amp 2 \\ 4 \amp -1 \end{bmatrix} \end{equation*}
Since
\begin{align*} \nabla f(3u + 2v, 4u - v) \amp = \begin{bmatrix} 6(3u + 2v) - 2 (4u - v) \\ -2(3u + 2v) + 2(4u - v) \end{bmatrix}\\ \amp = \begin{bmatrix} 10u + 14v \\ 2u - 6v \end{bmatrix}\text{,} \end{align*}
we find that
\begin{align*} \nabla (f \circ G) \amp = \begin{bmatrix} 3 \amp 2 \\ 4 \amp -1 \end{bmatrix}^T \begin{bmatrix} 10u + 14v \\ 2u - 6v \end{bmatrix}\\ \amp = \begin{bmatrix} 3 \amp 4 \\ 2 \amp -1 \end{bmatrix} \begin{bmatrix} 10 u + 14v \\ 2u - 6v \end{bmatrix}\\ \amp = \begin{bmatrix} (3)(10u + 14v) + (4)(2u - 6v) \\ (2)(10u + 14v) + (-1) (2u - 6v) \end{bmatrix}\\ \amp = \begin{bmatrix} 38u + 18v \\ 18u + 34v \end{bmatrix}\text{.} \end{align*}

Subsubsection Generalized chain rule

Before multiplying Jacobian matrices, check the input and output dimensions of each map.
The order matters. The rightmost Jacobian matrix acts first on the input change.
Note 3.6.9.
If \(f\) is a scalar-valued function, then the generalized chain rule tells us that
\begin{equation*} \nabla (f \circ G) = J_G^T (\nabla f \circ G) \end{equation*}
as in the two variable case.
Activity 3.6.8.
Suppose \(f(x,y,z) = 3x^2 - 2xy + 4z^2\text{,}\) and that \(G(u,v) = (e^u \sin v, e^u \cos v, e^u )\text{.}\) Compute the partial derivatives of \(f \circ G\text{.}\)
Solution.
We calculate that
\begin{equation*} \nabla f(x,y,z) = \begin{bmatrix} 6x - 2y \\ -2x \\ 8z \end{bmatrix} \end{equation*}
so that
\begin{equation*} \nabla f(e^u \sin v, e^u \cos v, e^u) = e^u \begin{bmatrix} 6\sin v - 2 \cos v \\ -2 \sin v \\ 8 \end{bmatrix}\text{.} \end{equation*}
Now
\begin{equation*} J_G(u,v) = \begin{bmatrix} e^u \sin v \amp e^u \cos v \\ e^u \cos v \amp - e^u \sin v \\ e^u \amp 0 \end{bmatrix} = e^u \begin{bmatrix} \sin v \amp \cos v \\ \cos v \amp - \sin v \\ 1 \amp 0 \end{bmatrix}\text{.} \end{equation*}
Thus
\begin{align*} \nabla (f \circ G)(u,v) \amp = J_G(u,v)^T (\nabla f \circ G)(u,v)\\ \amp = (e^u)^2 \begin{bmatrix} \sin v \amp \cos v \amp 1 \\ \cos v \amp - \sin v \amp 0 \end{bmatrix} \begin{bmatrix} 6 \sin v - 2 \cos v \\ -2 \sin v \\ 8 \end{bmatrix}\\ \amp = e^{2u} \begin{bmatrix} 6 (\sin v)^2 - 2 \sin v \cos v - 2 \sin v \cos v + 8 \\ 6 \sin v cos v - 2 (\cos v)^2 + 2 (\sin v)^2 \end{bmatrix}\\ \amp = e^{2u} \begin{bmatrix} 6 (\sin v)^2 - 4 \sin v \cos v + 8 \\ 4 (\sin v)^2 + 6 \sin v \cos v - 2 \end{bmatrix}\text{.} \end{align*}
From this calculation, we can read that the partial derivative of \(f \circ G\) in the first variable is equal to
\begin{equation*} e^{2u} ( 6 (\sin v)^2 - 4 \sin v \cos v + 8 ) \end{equation*}
and the derivative in the second variable is
\begin{equation*} e^{2u} ( 4 (\sin v)^2 + 6 \sin v \cos v - 2 )\text{.} \end{equation*}
Note 3.6.10.
The generalized chain rule says that local linear maps compose. The matrix formulation records that composition by multiplying Jacobian matrices in the correct order.

Subsection A tiny sigmoid block

A small neural-network block can be read as a composition of three steps: an affine map, a coordinatewise nonlinear rule, and another affine map.

Note 3.6.11. Sigmoid and its derivative.

Let
\begin{equation*} \sigma(t)=\frac{1}{1+e^{-t}}. \end{equation*}
The derivative is
\begin{equation*} \sigma'(t)=\sigma(t)(1-\sigma(t)). \end{equation*}
When \(\sigma\) is applied to a vector, it is applied coordinate by coordinate.

Activity 3.6.9. A tiny sigmoid block.

Let
\begin{equation*} W_1= \begin{bmatrix} 1&0\\ 0&1\\ 1&1 \end{bmatrix}, \qquad W_2= \begin{bmatrix} 1&0&-1/2\\ 0&1&1/2 \end{bmatrix}, \end{equation*}
and let \(\mathbf{b}_1=\mathbf{0}\) and \(\mathbf{b}_2=\mathbf{0}\text{.}\)
Define
\begin{equation*} \mathbf{s}=W_1\mathbf{x}+\mathbf{b}_1, \qquad \mathbf{u}=\sigma(\mathbf{s}), \qquad N(\mathbf{x})=W_2\mathbf{u}+\mathbf{b}_2, \end{equation*}
where \(\sigma\) is applied coordinatewise.
At the base point
\begin{equation*} \mathbf{a} = \begin{bmatrix} 0\\ 0 \end{bmatrix}, \end{equation*}
do the following.
  1. Compute the pre-activation vector \(\mathbf{s}_{\mathbf{a}}=W_1\mathbf{a}+\mathbf{b}_1\text{.}\)
  2. Compute the hidden vector \(\mathbf{u}_{\mathbf{a}}=\sigma(\mathbf{s}_{\mathbf{a}})\text{.}\)
  3. Compute \(N(\mathbf{a})\text{.}\)
  4. Use \(\sigma'(0)=1/4\) to compute
    \begin{equation*} D_{\mathbf{a}} = \operatorname{diag}\bigl(\sigma'((\mathbf{s}_{\mathbf{a}})_1),\sigma'((\mathbf{s}_{\mathbf{a}})_2),\sigma'((\mathbf{s}_{\mathbf{a}})_3)\bigr). \end{equation*}
  5. Use the chain rule to compute
    \begin{equation*} J_N(\mathbf{a})=W_2D_{\mathbf{a}}W_1. \end{equation*}
  6. Use
    \begin{equation*} N(\mathbf{a}+\mathbf{h})\approx N(\mathbf{a})+J_N(\mathbf{a})\mathbf{h} \end{equation*}
    to predict the output change caused by a small input change \(\mathbf{h}\text{.}\)
Tags. [U3-LO5, U3-LO7 | C+R | Core]
Solution.
Since \(\mathbf{a}=\mathbf{0}\) and \(\mathbf{b}_1=\mathbf{0}\text{,}\)
\begin{equation*} \mathbf{s}_{\mathbf{a}} = W_1\mathbf{a}+\mathbf{b}_1 = \begin{bmatrix} 0\\ 0\\ 0 \end{bmatrix} \end{equation*}
Therefore
\begin{equation*} \mathbf{u}_{\mathbf{a}} = \sigma(\mathbf{s}_{\mathbf{a}}) = \begin{bmatrix} 1/2\\ 1/2\\ 1/2 \end{bmatrix}. \end{equation*}
Since \(\mathbf{b}_2=\mathbf{0}\text{,}\)
\begin{equation*} N(\mathbf{a}) = W_2\mathbf{u}_{\mathbf{a}} = \begin{bmatrix} 1&0&-1/2\\ 0&1&1/2 \end{bmatrix} \begin{bmatrix} 1/2\\ 1/2\\ 1/2 \end{bmatrix} = \begin{bmatrix} 1/4\\ 3/4 \end{bmatrix}. \end{equation*}
Since \(\sigma'(0)=1/4\text{,}\)
\begin{equation*} D_{\mathbf{a}} = \begin{bmatrix} 1/4&0&0\\ 0&1/4&0\\ 0&0&1/4 \end{bmatrix}. \end{equation*}
Therefore
\begin{equation*} J_N(\mathbf{a}) = W_2D_{\mathbf{a}}W_1 = \begin{bmatrix} 1&0&-1/2\\ 0&1&1/2 \end{bmatrix} \begin{bmatrix} 1/4&0&0\\ 0&1/4&0\\ 0&0&1/4 \end{bmatrix} \begin{bmatrix} 1&0\\ 0&1\\ 1&1 \end{bmatrix} = \begin{bmatrix} 1/8&-1/8\\ 1/8&3/8 \end{bmatrix}. \end{equation*}
\begin{equation*} N(\mathbf{a}+\mathbf{h}) \approx \begin{bmatrix} 1/4\\ 3/4 \end{bmatrix} + \begin{bmatrix} 1/8&-1/8\\ 1/8&3/8 \end{bmatrix} \mathbf{h}. \end{equation*}
This gives the intended local prediction: the Jacobian matrix of the composition is a product of local matrices.

Note 3.6.12. Why sigmoid here.

The sigmoid function is smooth, so the Jacobian matrix of this block is defined at every input. Some neural-network models use nonsmooth activation rules. Those require extra case analysis. Here the point is the chain rule for Jacobian matrices.
Square-grid visualization for a two-output sigmoid block.
A colored grid in the unit square is shown next to its smooth image under a two-output sigmoid block. The image bends and compresses because the sigmoid derivatives vary with the input.
Figure 3.6.13. Square-grid visualization for a two-output sigmoid block. The map is smooth. The grid bends and compresses because the sigmoid derivative changes with the input. Near one base point, the Jacobian matrix gives the local linear approximation.

Activity 3.6.10. A one-query softmax weighting map.

A single-query attention-style map can be viewed as a nonlinear vector-valued map.
Let
\begin{equation*} K= \begin{bmatrix} 3&0\\ 0&3\\ -3&-3 \end{bmatrix}, \qquad V= \begin{bmatrix} 1&0\\ 0&1\\ 0&0 \end{bmatrix}. \end{equation*}
For a query vector
\begin{equation*} \mathbf{q} = \begin{bmatrix} q_1\\ q_2 \end{bmatrix}, \end{equation*}
define the score vector
\begin{equation*} \mathbf{s}=K\mathbf{q}, \end{equation*}
the softmax weight vector
\begin{equation*} \boldsymbol{\alpha}(\mathbf{q})=\operatorname{softmax}(\mathbf{s}), \end{equation*}
and the output
\begin{equation*} F(\mathbf{q})=V^T\boldsymbol{\alpha}(\mathbf{q}). \end{equation*}
  1. Compute \(\mathbf{s}=K\mathbf{q}\) in terms of \(q_1\) and \(q_2\text{.}\)
  2. What are the rows of \(V\text{?}\)
  3. Explain why \(F(\mathbf{q})\) is a weighted average of the rows of \(V\text{.}\)
  4. Compute \(\boldsymbol{\alpha}(\mathbf{0})\) and \(F(\mathbf{0})\text{.}\)
  5. Explain why \(F\) is not a linear map.
  6. What operation makes \(F\) nonlinear?
Tags. [U3-LO5, U3-LO7, U1-LO8 | C+R | Core]
Solution.
The score vector is
\begin{equation*} \mathbf{s} = K\mathbf{q} = \begin{bmatrix} 3q_1\\ 3q_2\\ -3q_1-3q_2 \end{bmatrix}. \end{equation*}
The rows of \(V\) are the value vectors
\begin{equation*} \mathbf{v}_1= \begin{bmatrix} 1\\ 0 \end{bmatrix}, \qquad \mathbf{v}_2= \begin{bmatrix} 0\\ 1 \end{bmatrix}, \qquad \mathbf{v}_3= \begin{bmatrix} 0\\ 0 \end{bmatrix}. \end{equation*}
The softmax vector
\begin{equation*} \boldsymbol{\alpha}(\mathbf{q}) = \begin{bmatrix} \alpha_1(\mathbf{q})\\ \alpha_2(\mathbf{q})\\ \alpha_3(\mathbf{q}) \end{bmatrix} \end{equation*}
has positive entries that add to \(1\text{.}\) Therefore
\begin{equation*} F(\mathbf{q}) = V^T\boldsymbol{\alpha}(\mathbf{q}) = \alpha_1(\mathbf{q})\mathbf{v}_1 + \alpha_2(\mathbf{q})\mathbf{v}_2 + \alpha_3(\mathbf{q})\mathbf{v}_3 \end{equation*}
is a weighted average of the value vectors.
At \(\mathbf{q}=\mathbf{0}\text{,}\) the score vector is
\begin{equation*} \mathbf{s} = \begin{bmatrix} 0\\ 0\\ 0 \end{bmatrix}. \end{equation*}
All three scores are equal, so
\begin{equation*} \boldsymbol{\alpha}(\mathbf{0}) = \begin{bmatrix} 1/3\\ 1/3\\ 1/3 \end{bmatrix}. \end{equation*}
Thus
\begin{equation*} F(\mathbf{0}) = V^T \begin{bmatrix} 1/3\\ 1/3\\ 1/3 \end{bmatrix} = \begin{bmatrix} 1/3\\ 1/3 \end{bmatrix}. \end{equation*}
A linear map must send \(\mathbf{0}\) to \(\mathbf{0}\text{.}\) Since \(F(\mathbf{0})\ne \mathbf{0}\text{,}\) the map \(F\) is not linear.
The matrix multiplication \(K\mathbf{q}\) is linear in \(\mathbf{q}\text{.}\) The final weighted average is linear in the weights if the weights are already fixed. The nonlinear step is the softmax rule that turns scores into weights.

Note 3.6.14. Softmax outputs stay in the value triangle.

The output always lies in the triangle spanned by the three value vectors. The query vector does not choose fixed weights. It changes the scores, and the softmax rule changes all weights together.
Square-grid visualization for an attention-style softmax map.
A colored square grid of query vectors is shown next to its image under a softmax-weighted average map. The output points lie inside a triangle formed by three value vectors. The transformed grid bends and compresses toward different corners of the triangle.
Figure 3.6.15. Square-grid visualization for an attention-style map \(\mathbf{q}\mapsto V^T\operatorname{softmax}(K\mathbf{q})\text{.}\) The output lies in the triangle spanned by the value vectors. The grid bends and compresses because the softmax weights depend nonlinearly on the query.

Note 3.6.16. An attention-style nonlinear block.

Unit 1 treated attention as three visible operations:
\begin{equation*} \text{dot-product scores}\longrightarrow \text{weights}\longrightarrow \text{weighted averages}. \end{equation*}
For a sequence of token vectors stored as rows of a matrix \(X\text{,}\) fixed weight matrices produce
\begin{equation*} Q=XW_Q,\qquad K=XW_K,\qquad V=XW_V. \end{equation*}
The score matrix is
\begin{equation*} S=QK^T. \end{equation*}
The entry \(S_{ij}\) compares the query for token \(i\) with the key for token \(j\text{.}\) A weighting rule, such as row-wise softmax, converts each row of \(S\) into weights. Write the resulting attention-weight matrix as \(A_{\mathrm{att}}\text{.}\) The attention output is
\begin{equation*} H=A_{\mathrm{att}}V. \end{equation*}
If \(A_{\mathrm{att}}\) were fixed, then \(V\mapsto A_{\mathrm{att}}V\) would be a linear map: each row of \(H\) would be a weighted average of rows of \(V\text{.}\) In attention, \(A_{\mathrm{att}}\) is not fixed. It is computed from \(X\) through \(Q\text{,}\) \(K\text{,}\) scores, and softmax. Thus the full rule \(X\mapsto H\) is generally nonlinear.
The Unit 3 question is local. Near one fixed input \(X_0\text{,}\) a nonlinear rule can be approximated by a linear map. After the entries of \(X\) and \(H\) are arranged as vectors, that local linear map is represented by a Jacobian matrix.

Activity 3.6.11. What depends on the input?

Assume the weight matrices \(W_Q\text{,}\) \(W_K\text{,}\) and \(W_V\) are fixed. The input is the token matrix \(X\text{.}\) Define
\begin{equation*} Q=XW_Q,\qquad K=XW_K,\qquad V=XW_V, \end{equation*}
\begin{equation*} S=QK^T,\qquad A_{\mathrm{att}}=\operatorname{softmax}_{\mathrm{rows}}(S), \qquad H=A_{\mathrm{att}}V. \end{equation*}
  1. Which objects are fixed before the input \(X\) is chosen?
  2. Which objects are computed from \(X\text{?}\)
  3. Why is \(X\mapsto XW_Q\) a linear map when \(W_Q\) is fixed?
  4. Why does \(S=QK^T\) depend on \(X\) in two places?
  5. If \(A_{\mathrm{att}}\) were fixed, what would \(H=A_{\mathrm{att}}V\) do to the rows of \(V\text{?}\)
  6. In actual attention, why is the full rule \(X\mapsto H\) not just one linear map?
  7. What Unit 3 object describes the local linear approximation near a fixed input \(X_0\text{?}\)
Tags. [U3-LO5, U3-LO6, U3-LO7 | C+R | Core]
Solution.
The matrices \(W_Q\text{,}\) \(W_K\text{,}\) and \(W_V\) are fixed. The matrices \(Q\text{,}\) \(K\text{,}\) \(V\text{,}\) \(S\text{,}\) \(A_{\mathrm{att}}\text{,}\) and \(H\) are computed from \(X\text{.}\)
The rule \(X\mapsto XW_Q\) is linear because \(W_Q\) is fixed and right multiplication by a fixed matrix distributes over sums and scalar multiples.
The score matrix \(S=QK^T\) depends on \(X\) through \(Q\) and through \(K\text{.}\) The entries of \(S\) are dot products between query rows and key rows, and both sets of rows were computed from \(X\text{.}\)
If \(A_{\mathrm{att}}\) were fixed, then \(H=A_{\mathrm{att}}V\) would form weighted averages of the rows of \(V\text{.}\) This is linear in \(V\text{.}\)
In attention, \(A_{\mathrm{att}}\) is not fixed. The weights are computed from the input through scores and softmax. The values \(V\) also depend on \(X\text{.}\) Therefore the full rule \(X\mapsto H\) is generally nonlinear.
Near a fixed input \(X_0\text{,}\) the local linear approximation is represented by a Jacobian matrix after the entries of the input and output matrices are arranged as vectors.

Warning 3.6.17. Specify the input.

Before asking whether a rule is linear, first say what is variable and what is fixed. The rule \(V\mapsto A_{\mathrm{att}}V\) is linear when \(A_{\mathrm{att}}\) is fixed. The attention rule \(X\mapsto A_{\mathrm{att}}(X)V(X)\) is generally nonlinear because the weights and values both depend on \(X\text{.}\)

Activity 3.6.12. Comparing square-grid visualizations.

This unit used three square-grid visualizations: the nonlinear shear from ActivityΒ 3.1.3, the sigmoid block from ActivityΒ 3.6.9, and the attention-style softmax map from ActivityΒ 3.6.10.
Map A:
\begin{equation*} F\left(\begin{bmatrix}x\\y\end{bmatrix}\right) = \begin{bmatrix} x+y^2\\ y \end{bmatrix}. \end{equation*}
Map B: a two-output sigmoid block
\begin{equation*} \mathbf{s}=W_1\mathbf{x}+\mathbf{b}_1, \qquad \mathbf{u}=\sigma(\mathbf{s}), \qquad N(\mathbf{x})=W_2\mathbf{u}+\mathbf{b}_2. \end{equation*}
Map C: an attention-style softmax map
\begin{equation*} F_{\mathrm{att}}(\mathbf{q}) = V^T\operatorname{softmax}(K\mathbf{q}). \end{equation*}
  1. Which map agrees with a linear shear on the four corners but not at an edge midpoint?
  2. Which map is built from affine maps with a coordinatewise sigmoid between them?
  3. For which map is the local matrix computed by \(W_2D_{\mathbf{a}}W_1\text{?}\)
  4. Which map produces outputs inside the triangle spanned by value vectors?
  5. For which map does the nonlinear step come from softmax weights that depend on the input?
  6. In all three examples, what does the square-grid visualization show that four corners alone may miss?
Tags. [U3-LO1, U3-LO5, U3-LO7, U1-LO8 | C+R | Core]
Solution.
Map A is the nonlinear shear. It can agree with a linear shear on selected points but differ between them.
Map B is the sigmoid block. Its Jacobian matrix near a base point has the form \(W_2D_{\mathbf{a}}W_1\text{,}\) where \(D_{\mathbf{a}}\) records the sigmoid derivatives at that base point. The local matrix can change from point to point because the sigmoid derivatives change.
Map C is the attention-style softmax map. Its outputs are weighted averages of the rows of \(V\text{,}\) so they stay inside the triangle spanned by the value vectors. The softmax weights depend on the input.
A square-grid visualization can show bending, compression, and changing local behavior that four corners alone may miss.