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{.}\) The set \(U^\perp\) is then precisely the set of \(\mathbf{w}\) satisfying the system of linear equations \(x + y = 0\) and \(y + z = 0\text{.}\) This system has a single basic solution \((1,-1,1)\text{,}\) and so any vector in \(U^\perp\) is a multiple of this vector, i.e.,
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.
Use LemmaΒ 4.2.3 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.4.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{.}\)
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}=\proj_U(\mathbf{x})\text{,}\) and \(\mathbf{x} - \proj_U(\mathbf{x}) \in U^\perp\text{.}\) In particular, if \(\mathbf{x}\) is already in \(U\text{,}\) then \(\mathbf{x}\) equals the displayed sum.
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.3 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.7) 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 represents \(((\mathbf{x}\cdot\mathbf{u})/(\mathbf{u}\cdot\mathbf{u}))\mathbf{u}\text{.}\)
The Unit 1 attention activities, including Many-token attention output, used dot products to make scores and then used weights to mix value vectors. In the language of this unit, the output lies in the span of the value vectors. Thus attention uses the same two operations we have been studying: dot products and linear combinations.
Attention and projection both use dot products, but they solve different problems. In a simple attention step, dot products produce scores, the scores are normalized into weights, and the output is a weighted average of value vectors.
So both ideas use dot products, but they answer different questions. Attention asks which stored vectors should be mixed. Projection asks which vector in a subspace is closest. This distinction matters: not every dot-product method is a projection method.
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
Let \(\mathbf{x}_1, \mathbf{x}_2, \mathbf{x}_3\) be the three rows of \(A\text{,}\) written in order from top to bottom, and apply the Gram-Schmidt algorithm. We start by writing
The plane has normal vector \(\mathbf{n}=(2,1,-1)\text{.}\) Since the plane passes through the origin, it is the subspace \(U=\mathbf{n}^\perp\text{.}\) The closest point in \(U\) is obtained by subtracting the component of \(\mathbf{x}\) in the normal direction: