An \(n\)-vector is an ordered list of \(n\) scalars, often written in the form \(\mathbf{v} = (v_1,v_2,\ldots,v_n)\text{.}\) The collection of all \(n\)-vectors is denoted \(\R^n\text{.}\)
It is customary to use bold letters for variables, such as \(\mathbf{v}\) or \(\mathbf{w}\text{,}\) to represent vectors. We let \(\mathbf{0}\) denote the vector \((0,\dots,0)\) consisting only of zeros. A point in \(n\)-dimensional space can also be identified with an ordered list of \(n\) numbers using Cartesian coordinates. When referring specifically to a point \(P\text{,}\) we write \(P(x_1,x_2,\ldots,x_n)\) to indicate its coordinates.
Vectors can be used to represent displacements between points in space. The vector \(\mathbf{v}=(v_1,v_2,v_3)\) represents the displacement from \(P(x_1,y_1,z_1)\) to \(Q(x_2,y_2,z_2)\text{,}\) where \(x_2 = x_1 + v_1\text{,}\)\(y_2 = y_1 + v_2\text{,}\) and \(z_2 = z_1 + v_3\text{.}\) This vector is also written as \(\overrightarrow{PQ}\text{.}\) Because vectors often indicate displacements, they are drawn pictorially as arrows starting at a point, and ending where that point is displaced by the vector.
Three coordinate axes labeled \(x\text{,}\)\(y\text{,}\) and \(z\) meet at an origin. A thick arrow labeled \(\mathbf{v}\) starts at the red point \(P\) and ends at the red point \(Q\text{,}\) showing the displacement from \(P\) to \(Q\text{.}\)
If \(\mathbf{v} = (v_1,v_2)\) was the displacement, then we would have \((1 + v_1, 2 + v_2) = (5,7)\text{,}\) i.e., so that \(1 + v_1 = 5\) and \(2 + v_2 = 7\text{.}\) Solving these equations gives \(v_1 = 4\) and \(v_2 = 5\text{,}\) so that the displacement vector is \(\mathbf{v} = (4,5)\text{.}\)
Let \(\mathbf{v} = (x,y)\) be an arbitrary \(2\)-vector. Consider a triangle with vertices \(P(0,0)\text{,}\)\(Q(x,0)\text{,}\) and \(R(x,y)\) (see FigureΒ 1.1.4).
The figure shows \(x\)- and \(y\)-axes with a vector \(\mathbf{v}\) drawn from \(P(0,0)\) to \(R(x,y)\text{.}\) A horizontal segment from \(P\) to \(Q(x,0)\) and a vertical segment from \(Q\) to \(R\) form a right triangle whose hypotenuse is the vector.
Figure1.1.4.A triangle with vertices \(P(0,0)\text{,}\)\(Q(x,0)\text{,}\) and \(R(x,y)\text{,}\) drawn to determine the length of the line-segment upon which a \(2\)-vector is based.
This triangle is right-angled (the two legs are horizontal and vertical), and the hypotenuse is the line-segment upon which the vector \(\mathbf{v}\) is built. The length of the leg from \(P(0,0)\) to \(Q(x,0)\) is \(|x|\text{,}\) and the length of the leg from \(Q(x,0)\) to \(R(x,y)\) is \(|y|\text{.}\) By Pythagorasβ theorem, we conclude that the length of the hypotenuse is
In a calculus course, you may have mostly seen a vector as a displacement, but in this course it can also be a row of data or a list of model parameters, among other important applications. The same operations below will later compare documents, tokens, and feature vectors.
The sum of two \(n\)-vectors \(\mathbf{v} = (v_1,\dots,v_n)\) and \(\mathbf{w} = (w_1,\dots,w_n)\text{,}\) denoted \(\mathbf{v} + \mathbf{w}\text{,}\) is the vector given by the tuple \((v_1 + w_1, \dots, v_n + w_n)\text{.}\)
The sum of two vectors can be illustrated pictorially as in FigureΒ 1.1.7 by drawing the parallelogram with the vectors placed along two sides of the parallelogram.
A blue vector \(\mathbf{v}\) and a red vector \(\mathbf{w}\) start at the origin. Dashed copies of each vector are translated to the tip of the other vector, and both translated arrows meet at a shared endpoint. A green arrow from the origin to that endpoint is labeled \(\mathbf{v}+\mathbf{w}\text{.}\)
If \(\mathbf{v} = (v_1,\cdots,v_n)\) is an \(n\)-vector and \(t \in \R\) is a scalar, we let \(t \mathbf{v}\) denote the vector \((tv_1, \cdots, tv_n)\text{,}\) i.e., multiplying each entry of the vector \(\mathbf{v}\) by \(t\text{.}\)
is a linear combination of \(\mathbf{v}_1,\ldots,\mathbf{v}_k\text{.}\) The scalars \(c_1,\ldots,c_k\) are the coefficients of the linear combination. If \(c_i\ge 0\) for all \(i\) and \(c_1+\cdots+c_k=1\text{,}\) then the linear combination is a convex combination.
Let \(\mathbf{v}_1=(10,0)\text{,}\)\(\mathbf{v}_2=(0,10)\text{,}\)\(\mathbf{v}_3=(10,10)\text{,}\) and \(\alpha=(1/4,1/4,1/2)\text{.}\) Compute \(\alpha_1\mathbf{v}_1+\alpha_2\mathbf{v}_2+\alpha_3\mathbf{v}_3\text{.}\)
Geometrically, the dot product of two vectors is a quantity which is related to the angle between the two vectors \(\mathbf{v}\) and \(\mathbf{w}\text{.}\) If we draw the two vectors as arrows with the same starting point, then they form an angle on the plane containing both vectors, and the angle \(\theta\) is given below.
The dot product can be thought of as a measure of the similarity between two vectors. Suppose \(\theta\) is the angle between two vectors \(\mathbf{v}\) and \(\mathbf{w}\text{:}\)
If \(\theta\) is close to zero, then the two vectors are close to pointing in the same direction. Since \(\cos(0^\circ) = 1\text{,}\) this occurs precisely when
If \(\theta\) is close to \(180^\circ\text{,}\) then the two vectors are close to pointing in opposite directions. Since \(\cos(180^\circ) = -1\text{,}\) this occurs precisely when
If \(\theta\) is close to \(90^\circ\text{,}\) the two vectors are close to pointing at right angles. Since \(\cos(90^\circ) = 0\text{,}\) this occurs precisely when
ranges between \(-1\) and \(1\text{,}\) and measures the degree to which the two vectors \(\mathbf{v}\) and \(\mathbf{w}\) point in the same, or opposite, directions. In some contexts, especially in certain areas of data science, this quantity is called the cosine similarity between the vectors \(\mathbf{v}\) and \(\mathbf{w}\text{.}\)
A dashed coordinate grid is shown with \(x\)- and \(y\)-axes. A blue arrow from the origin ends at \((1,2)\text{,}\) while a longer red arrow from the origin ends at \((\sqrt{3}+2, 2\sqrt{3}-1)\text{.}\) The two arrows form the angle used in the dot-product activity.
Vectors can represent quantities other than displacements. The entries of a vector can record measurements, counts, samples, or features. The meaning of a vector depends on what its coordinates represent.
measured attributes such as size, price, weight, or rating
The same vector operations can have different interpretations. A sum can add purchases, add word counts, or add two time series. A scalar multiple can rescale an image, double a portfolio, or change units. A dot product can produce a score. A distance can compare two feature vectors. Cosine similarity compares direction after normalization.
Compute the cosine similarities and Euclidean distances between \(\mathbf{q}\) and each \(\mathbf{D}_i\text{.}\) Rank the documents by cosine similarity.
The vector \(\mathbf{D}_1\) points in exactly the same direction as \(\mathbf{q}\) because \(\mathbf{D}_1=2\mathbf{q}\text{.}\) The vector \(\mathbf{D}_2\) shares the word βlinearβ with the query but also contains βmatrixβ. The vector \(\mathbf{D}_3\) contains only βmatrixβ, so it is orthogonal to the query.
The score for \(\mathbf{D}_2\) is score2. The expression D1 @ q computes \(\mathbf{D}_1\cdot\mathbf{q}\text{.}\) Since score1 is largest, \(\mathbf{D}_1\) is most similar to the query. The output agrees with the hand calculation: \(1,\frac12,0\text{.}\)
A neural network is a function built from layers. A layer takes numbers as input, combines them using weights, applies a rule, and sends output numbers to the next layer. In many neural networks, the input and output of a layer are vectors. The weights are adjusted from data during training.
A transformer is a neural-network architecture for sequences. In a language model, text is first broken into tokens. A token can be a word, part of a word, punctuation mark, or other text fragment. Each token is represented by a vector.
Three red input nodes are arranged vertically on the left, four blue hidden-layer nodes appear in the center, and two green output nodes appear on the right. Black arrows connect each input node to the hidden layer, and gray arrows connect the hidden layer to the outputs, showing information moving left to right through the network.
The diagram has two vertical stacks: an encoder stack for a source sequence on the left and a decoder stack for a target sequence on the right. Each stack shows embeddings and positional encoding feeding into attention and feed-forward blocks, with arrows indicating repeated layers. The decoder also points upward to a final linear and softmax prediction step.
Suppose a transformer layer is updating the token βbirdβ. Its query vector represents what this token is looking for in the sequence. Each key vector represents how another token can be matched by a query. Each value vector is the vector that may be averaged into the updated representation.
The token βbirdβ receives the largest weight because its key vector has the largest dot product with \(\mathbf{q}_{\text{bird}}\text{.}\) The updated vector is a weighted average of the value vectors.
This toy calculation uses normalization by the sum of positive scores to keep the arithmetic simple. Real transformer attention usually uses a different weighting rule.