The third property of TheoremΒ 4.2.2 implies that a vector \(\mathbf{w} = (x,y,z)\) lies in \(U^\perp\) if and only if it is orthogonal to both \((1,1,0)\) and \((0,1,1)\text{.}\) Taking these two dot products gives
From \(x+y=0\text{,}\) we obtain \(x=-y\text{.}\) From \(y+z=0\text{,}\) we obtain \(z=-y\text{.}\) The variable \(y\) is free, so every solution can be written as
If \(\mathbf{x} \not \in \spans \{\mathbf{f}_1,\ldots,\mathbf{f}_m\}\text{,}\) then \(\mathbf{f}_{m+1} \neq \mathbf{0}\) and \(\{\mathbf{f}_1,\ldots,\mathbf{f}_m,\mathbf{f}_{m+1}\}\) is an orthogonal set.
For convenience, let \(t_i = \frac{\mathbf{x} \cdot \mathbf{f}_i}{\|\mathbf{f}_i\|^2}\) for each \(i\text{.}\) Given \(1 \leq k \leq m\text{,}\) we calculate that
Since \(\{\mathbf{f}_1, \mathbf{f}_2, \ldots, \mathbf{f}_m\}\) is an orthogonal set, \(\mathbf{f}_i \cdot \mathbf{f}_k = 0\) for all \(i \neq k\text{.}\) So
This proves the first property of the lemma. To prove the second, we note that if \(\mathbf{f}_{m+1}\) was zero, then \(\mathbf{x}\)would be in the span of the vectors \(\{\mathbf{f}_1,\ldots,\mathbf{f}_m\}\text{.}\) So if \(\mathbf{x}\) is not in the span, then \(\mathbf{f}_{m+1}\) can not be equal to zero. And we have proved \(\mathbf{f}_{m+1}\) is orthogonal to the vectors \(\mathbf{f}_1, \dots, \mathbf{f}_m\text{,}\) so that \(\{ \mathbf{f}_1, \dots, \mathbf{f}_{m+1} \}\) is an orthogonal set.
Activity4.2.5.Extending an orthogonal set to a basis (U4-LO2).
Use LemmaΒ 4.2.4 to extend the orthogonal set \(\{ \mathbf{f}_1, \mathbf{f}_2 \}\) in \(\R^3\) to an orthogonal basis \(\{ \mathbf{f}_1, \mathbf{f}_2, \mathbf{f}_3 \}\text{,}\) where
Definition4.2.6.The orthogonal projection onto a subspace.
Let \(U\) be a subspace of \(\R^n\text{.}\) Then the orthogonal projection of the vector \(\mathbf{x} \in \R^n\) onto \(U\) is the vector \(\mathbf{y}=\proj_U(\mathbf{x}) \in U\) which is closest to \(\mathbf{x}\text{,}\) in the sense that for any \(\mathbf{z} \in U\) with \(\mathbf{z} \neq \mathbf{y}\text{,}\)\(\| \mathbf{x} - \mathbf{y} \| \lt \| \mathbf{x} - \mathbf{z} \|\text{.}\) The residual is the difference between the original vector and its projection:
Figure4.2.7.Projection and residual. Orthogonal projection writes \(\mathbf{x}=\widehat{\mathbf{x}}+\mathbf{r}\text{,}\) where \(\widehat{\mathbf{x}}\in U\) and \(\mathbf{r}\in U^\perp\text{.}\)
Let \(U\) be a subspace of \(\R^n\) with orthogonal basis \(\{\mathbf{f}_1,\ldots,\mathbf{f}_m\}\text{.}\) For any vector \(\mathbf{x} \in \R^n\text{,}\) define
then \(\mathbf{y}\) is evidently a linear combination of the basis \(\{ \mathbf{f}_1, \dots, \mathbf{f}_m \}\text{,}\) so \(\mathbf{y} \in U\text{.}\) And LemmaΒ 4.2.4 states that \(\mathbf{x} - \mathbf{y} \in U^\perp\text{.}\) Thus if \(\mathbf{z} \in U\) and \(\mathbf{z} \neq \mathbf{y}\text{,}\) the Pythagorean theorem (TheoremΒ 4.1.11) tells us that
Thus the vector \(\mathbf{y}\) we have defined is equal to \(\proj_U(\mathbf{x})\text{.}\) Since we have already verified that \(\mathbf{x} - \mathbf{y} = \mathbf{x} - \proj_U(\mathbf{x})\) is in \(U^\perp\text{,}\) the theorem follows.
The variable y stores the projection and r stores the residual. The value r @ u checks whether the residual is orthogonal to the direction of the line, so it should be close to zero. The expression (x @ u) / (u @ u) * u computes the orthogonal projection of \(\mathbf{x}\) onto \(L=\spans\{\mathbf{u}\}\text{:}\)
\begin{equation*}
\operatorname{proj}_{\operatorname{span}\{\mathbf v\}}\bigl(\nabla f(\mathbf a)\bigr)=D_{\mathbf v}f(\mathbf a)\mathbf v.
\end{equation*}
Thus \(D_{\mathbf v}f(\mathbf a)\) is the signed projection coefficient of the gradient in the oriented direction \(\mathbf v\text{.}\) Its absolute value is the length of the projection. The CauchyβSchwarz inequality in TheoremΒ 4.1.2 gives
The vectors \(\mathbf v_3\) and \(\mathbf v_4\) span the same line, so projection onto that line gives the same vector. The directional derivatives have opposite signs because the two unit vectors give opposite orientations of the line. FigureΒ 3.6.9 shows greatest increase in the gradient direction, greatest decrease in the opposite direction, and zero change in directions orthogonal to the gradient.
A common optimization problem that we have already encountered in DefinitionΒ 4.2.6 involves taking a subspace \(U \subseteq \R^n\) and a given vector \(\mathbf{x}_0 \in \R^n\text{,}\) and finding the vector \(\mathbf{x}^* \in U\) that is closest to \(\mathbf{x}_0\text{,}\) i.e., finding the vector that minimizes the scalar-valued function \(g: \R^n \to \R\) on \(U\text{,}\) defined by setting \(g(\mathbf{x}) = \| \mathbf{x}_0 - \mathbf{x} \|\text{.}\) Then \(\mathbf{x}^*\) is the orthogonal projection of \(\mathbf{x}_0\) onto \(U\text{,}\) and we have seen in TheoremΒ 4.2.8 that if \(U\) has an orthogonal basis \(\{ \mathbf{f}_1, \dots, \mathbf{f}_m \}\text{,}\) then
The unique critical point is \(t=2\text{.}\) Since
\begin{equation*}
g''(t)=2>0,
\end{equation*}
this critical point is a local minimum. Because \(g\) is an upward-opening quadratic, it is also the unique global minimum. Therefore the closest point in \(W\) is
collects the directional coefficients of \(\mathbf{x}\text{.}\) Because each \(\mathbf{q}_i\) has length \(1\text{,}\) these dot products are the coordinates used in the projection. If \(\mathbf{x}\) also has length \(1\text{,}\) they are cosine similarities with the directions \(\mathbf{q}_i\text{.}\)
Figure4.2.15.Score, then combine. Projection uses \(Q^T\mathbf{x}\) to score orthonormal directions and \(Q(Q^T\mathbf{x})\) to combine them. Attention uses query-key scores, converts them into weights, and combines value vectors.
Attention has the same broad pattern. Query-key dot products produce scores, a weighting rule converts the scores into weights, and the weights combine value vectors. Projection scores and combines the same orthonormal directions. Attention scores key vectors and then combines value vectors, which may be different vectors. Projection also chooses its coefficients so that the residual is orthogonal to the subspace. Attention is therefore not generally an orthogonal projection, but both computations are built from dot products and linear combinations.
Explain how the calculation follows the pattern βscore, then combine.β What extra condition identifies \(\widehat{\mathbf{x}}\) as an orthogonal projection?
Each entry of \(\mathbf{c}\) is a dot product with one of the unit directions, so it is both a directional score and a coordinate used to reconstruct the projection. The calculation first scores \(\mathbf{x}\) with \(Q^T\text{,}\) then combines the columns of \(Q\) using those scores. The condition \(Q^T\mathbf{r}=\mathbf{0}\) says that the residual is orthogonal to every column of \(Q\text{.}\) This is the extra condition that identifies \(\widehat{\mathbf{x}}\) as the orthogonal projection.
Gram-Schmidt is a sequence of projection, subtraction, measurement, and normalization steps. Use Perfectly Normal to practice the same sequence geometrically. The game uses an original column \(\mathbf{a}_j\text{,}\) its projection \(\mathbf{p}_j\text{,}\) the residual \(\mathbf{v}_j\text{,}\) the new unit direction \(\mathbf{q}_j\text{,}\) and coefficients \(r_{ij}\text{.}\)
If \(r_{jj}=0\text{,}\) then \(\mathbf{a}_j\) is already in the span of the earlier columns. The input columns are not linearly independent, and no new unit direction is produced.
The same algorithm in orthogonal form. Repeatedly applying LemmaΒ 4.2.4 gives the theorem below. The theorem first constructs orthogonal residuals \(\mathbf{f}_j\text{,}\) while the algorithm above normalizes each residual to obtain \(\mathbf{q}_j\text{.}\) The two forms agree because
Given a basis\(\{ \mathbf{x}_1, \dots, \mathbf{x}_m \}\) of \(U\text{,}\) we can iteratively define an orthogonal basis \(\{ \mathbf{f}_1, \dots, \mathbf{f}_m \}\) of \(U\) by defining
These columns are the rows of \(A\text{,}\) written as column vectors. At each stage, record the projection coefficients, projection, residual, residual length, and normalized direction. Give an orthogonal basis and an orthonormal basis for \(\row(A)\) (recall DefinitionΒ 2.4.33).
The columns of \(A^T\) are the rows of \(A\text{,}\) and Gram-Schmidt preserves each successive span. Therefore the residuals and their normalized unit vectors both span \(\row(A)\text{.}\)
Let \(U\) be the plane. Solving its equation for \(z\) gives \(z=2x+y\text{.}\) If we set \(x=s\) and \(y=t\text{,}\) then every vector in \(U\) has the form