Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Computing Weighted Sums of Vectors

Hello again. Last lesson, a dot product combined two vectors to produce one number. This lesson uses numbers in a different role: as weights that scale several vectors before we add them. The result is another vector.

This is one of the operations you will eventually recognize in Attention Is All You Need. For now, we will take the weights as given and concentrate on calculating the resulting vector. Allow about 35–40 minutes, including the video and reading.


Scaling and adding vectors

Suppose two vectors describe different directions. Multiplying a vector by a scalar changes its length; a negative scalar also reverses its direction. Adding the scaled vectors produces a new vector. We call this a weighted sum, or a linear combination:

Here each is a scalar weight, and each is a vector. There must be one weight for each vector, and all the vectors must have the same number of components so that they can be added.

Watch selected parts of 3Blue1Brown’s Linear combinations, span, and basis vectors for a visual account of this operation. Its coordinate-axis vectors are simply the vectors being scaled; you do not need to study the later discussion of span or basis yet.

Linear combinations, span, and basis vectors | Chapter 2, Essence of linear algebra

In 3Blue1Brown’s Linear combinations, span, and basis vectors, the animation makes the two operations in a weighted sum—scaling and addition—visible.

Watch scaled vectors to see how two numbers scale two vectors and their results add. Then jump to the name for this operation, and three vectors to see that adding a third vector does not change the rule. Focus on what the numbers do to the vectors, rather than on the terminology introduced elsewhere in the video.

The supplied diagram illustrates the same idea with two weights:

The diagram labels the orange resultant as \(\mathbf v=2\mathbf u_1+1.5\mathbf u_2\): each original vector receives its own scalar weight before the contributions are combined.

The picture helps with direction, but components are the reliable way to calculate an exact answer.


Calculating component by component

Take three two-component vectors and three weights:

First multiply every component of each vector by its weight. Then add the resulting vectors component by component:

There are three input vectors, but the output still has two components, because every vector being added has two components. Looking at one output coordinate at a time gives the same calculation:

The weights in this example are nonnegative and sum to one. That makes the result a weighted average of the vectors: each vector contributes a share of the output. But weighted sums do not generally require those conditions. For instance,

A negative weight reverses a vector’s contribution; a zero weight removes it. Neither case changes the procedure.

Notice the contrast with last lesson’s dot product. In , paired components are multiplied and added to obtain a scalar. In a weighted sum, each scalar multiplies an entire vector, and adding those scaled vectors produces a vector.


The matrix form is the same calculation

Matrices provide a compact way to arrange the vectors and weights. Place the vectors as columns of a matrix , and put their weights in a column vector . Matrix-vector multiplication then gives their weighted sum:

In our worked example, the three two-component vectors make a matrix:

This also connects to the shape rules you learned two lessons ago. The matrix has one column per weight and one row per output component. Thus a matrix times a weight vector produces a result.

Read the worked example and definition in the following LibreTexts section. Its numbers differ from ours, but the operation is identical.

2.2: Matrix multiplication and linear combinations

LibreTexts’ Matrix multiplication and linear combinations shows why multiplying a matrix by a vector means forming a weighted sum of the matrix’s columns. Read it to connect the component calculation to matrix notation and output shape.

In Section 2.2.2, “Matrix-vector multiplication and linear combinations,” begin at Example 2.2.1 with the sentence “Suppose we have the matrix” and read the example and definition through Definition 2.2.2, stopping before Activity 2.2.2. Track which numbers are weights, which vectors are columns, and why the output has three components. You can skip the activity and the later material on solving linear systems.

There is another common layout: store the same vectors as rows instead. Then a row of weights multiplying a matrix produces a weighted sum. The layout changes, but the component-by-component calculation does not.


Why this matters for attention

Recall that the dot products mentioned last lesson will eventually help an attention mechanism assign scores. After a further step turns scores into weights, attention uses those weights to combine vectors—often called value vectors—with precisely the operation learned here.

For example, if three value vectors receive weights , , and , the output is half of the first vector plus one quarter of each remaining vector, calculated component by component. A larger weight gives a vector more influence on that output; it does not replace the other vectors unless their weights are zero. We will examine where attention’s weights come from later.


Key takeaways

To compute a weighted sum, multiply each vector by its corresponding scalar and add the scaled vectors component by component. The output has the same number of components as each input vector. Writing the vectors as matrix columns makes matrix-vector multiplication a compact expression for exactly this calculation.

Next lesson, you will meet softmax, a way to turn a list of scores into nonnegative weights that sum to one.

Can't find a good explanation? Sign up and we'll make it for you

Sign up