Concepts / Embedded Input Vectors

Embedded Input Vectors

A context vector is the weighted sum of all input vectors, where each input is multiplied by its attention weight for one chosen query.

  • Programming

Why Attention Weights Are Not Enough

Attention weights tell us how strongly each input token should influence a chosen query token. On their own, though, they are only numbers, and they sum to 1.0. They do not yet give the model a representation it can use. The step that turns them into something usable is the context vector: a single vector that blends the whole input sequence according to those weights.

In this article you will run the computation that produces a context vector, trace the loop that does the work, and reason about why the output looks the way it does. By the end you should be able to predict the result before running anything, and to find the exact step responsible when the result is wrong.

The Three Ingredients of the Recipe

Read the definition of a context vector as a recipe with three ingredients. You need the full list of input vectors. You need the attention weight attached to each of those inputs for one chosen query. And you need a running total into which the weighted vectors are added.

Definition

A context vector is the weighted sum of all input vectors, where each input is multiplied by its attention weight for one chosen query.

Nothing in the recipe depends on the order of the inputs, except that each weight must stay paired with its own input vector. Swapping two inputs is harmless only if their weights are swapped along with them.

Formula

In this notation, $z^{(2)}$ is the context vector for the second token, $x^{(i)}$ is the i-th input vector, and $a_{2,i}$ is the attention weight that the second token assigns to input i. The sum runs over all $T$ input tokens, so every input contributes to the result, including those that receive a very small weight.

Tracing the Accumulation Loop

The implementation follows the formula directly. It starts from a zero vector with the same shape as the query. Then it walks through the inputs one at a time. On each pass it scales the current input vector by its matching weight and adds the result into the running vector. Because the loop uses enumerate, the index i selects the correct weight for the input at the same position.

Consider a small generated example. Suppose there are three input vectors with two values each, x1 = [1, 2], x2 = [3, 4], and x3 = [5, 6], and the chosen query assigns them the weights 0.2, 0.5, and 0.3. The running vector starts as [0, 0], because it has the same shape as the query. Each pass scales one input by its weight and adds it in.

Worked example

Computing the Context Vector Step by Step

Inputs x1 = [1, 2], x2 = [3, 4], x3 = [5, 6]; weights 0.2, 0.5, 0.3 for the chosen query. Find the context vector.

  1. 1
    Start the running vector

    Begin with the zero vector [0, 0], shaped like the query, so no leftover values carry into the sum.

  2. 2
    Pass 1, input x1

    Scale x1 by 0.2 to get [0.2, 0.4]. Add it to the running vector: [0, 0] + [0.2, 0.4] = [0.2, 0.4].

  3. 3
    Pass 2, input x2

    Scale x2 by 0.5 to get [1.5, 2.0]. Add it: [0.2, 0.4] + [1.5, 2.0] = [1.7, 2.4].

  4. 4
    Pass 3, input x3

    Scale x3 by 0.3 to get [1.5, 1.8]. Add it: [1.7, 2.4] + [1.5, 1.8] = [3.2, 4.2].

Result

The context vector is [3.2, 4.2]. It has the same number of values as each input, because the embedding size sets the length, not the number of inputs.

Notice what determines the shape of the result. The number of inputs controls how many terms the loop adds. The embedding size controls how many values each term has. The output is one vector with the embedding size, no matter how many inputs were summed. The source's own printed output for its query is three values, which you can reproduce by reasoning through the same loop.

Why the Running Total Starts at Zero

The zero start matters. The running vector has to begin empty so that the first addition does not carry leftover values from anywhere else. Once the loop finishes, the running vector holds the complete sum, and printing it shows the context vector.

What do you think happens?

Inputs are x1 = [1, 2], x2 = [3, 4], x3 = [5, 6], and the weights are 0.5, 0.5, and 0.0. What is the context vector?

Reveal answer
Answer:

[2.0, 3.0]

The third input gets weight 0.0, so it adds [0, 0] and changes nothing. The first two give 0.5 times [1, 2] plus 0.5 times [3, 4], which is [0.5, 1.0] plus [1.5, 2.0], or [2.0, 3.0]. An input with zero weight still participates in the loop, but it contributes nothing.

Finding Where the Computation Goes Wrong

When a context vector comes out wrong, the error almost always sits in one of three places. Work through them in order: check the query index, check the normalization of the weights, and check the pairing of each weight with its input. The chain runs from scoring to normalizing weights to scaling inputs to summing, so an error at an earlier step propagates into everything after it.

  • Common mistake

    Using the wrong query index

    What it looks like

    Computing the context vector for token 1 while the code labels it as token 2.

    Why it fails

    The weights come from one row of the attention table, and that row belongs to a specific query. A wrong index selects weights that describe a different token.

    Do this instead

    Confirm which query index was used to pick the row of weights before examining the sum itself.

  • Common mistake

    Weights that do not sum to 1.0

    What it looks like

    Weights 0.2, 0.5, and 0.6 for the three inputs above.

    Why it fails

    The attention weights are meant to be normalized so they sum to 1.0. Unnormalized weights scale the blend incorrectly.

    Do this instead

    Check that the weights for one query add up to 1.0 before the scaling step.

  • Common mistake

    Mispairing a weight with the wrong input

    Why it fails

    Each weight must stay paired with its own input vector. If the pairing shifts, the loop multiplies x2 by the weight meant for x3.

    Do this instead

    Verify that the enumerate index selects the weight at the same position as the input it multiplies.

  • Common mistake

    Starting the running total from a non-zero value

    Why it fails

    Leftover values carried into the first addition appear in the final sum even though no input produced them.

    Do this instead

    Initialize the running vector as a zero vector shaped like the query.

From One Query to Every Query

Computing one context vector answers a single question: how should the second token's representation be blended from the whole sequence? A model needs an answer for every token. The source handles this by repeating the same three steps for each query rather than inventing a new method.

Attention weights are first computed for every input token acting as a query. This produces a full table with one row per query. Each row then feeds the same weighted sum, and the result is one context vector per query position.

The source states that changing the code is the only modification needed to compute all context vectors instead of only the second one. The underlying three steps remain the same: choose the weights for a query, scale each input by its matching weight, and add the results into a zero-started running total. The contexts can be produced one after another, or with a matrix formulation in a single organized pass.

Practice Tracing a New Query

MEDIUM

Use the same three inputs as the worked example, x1 = [1, 2], x2 = [3, 4], and x3 = [5, 6], but assume a different query assigns the weights 0.1, 0.1, and 0.8. Before calculating, predict whether the context vector will move closer to x1 or to x3. Then trace the loop pass by pass and state the final vector.

Hints
  • Start the running vector at [0, 0].
  • Multiply each input by its own weight, then add the result into the running vector.
  • Compare the final vector with x1 and x3 to see which input dominates.
EASY

Suppose a context vector is wrong, and the weights you used for one query sum to 0.9 instead of 1.0. Which of the three error locations do you check first, and why?

Hints
  • Think about which of the three error locations concerns the weights themselves.
  • The other two locations concern the query index and the pairing of weights with inputs.

Key Takeaways

  • A context vector is the weighted sum of all input vectors, where each input is multiplied by its attention weight for one chosen query.
  • The computation starts from a zero vector shaped like the query and adds each scaled input into the running total, using enumerate so each weight stays paired with its input.
  • The result has the embedding size as its length; the number of inputs controls how many terms are summed, not the shape of the output.
  • Errors can be located by checking the query index, the normalization of the weights, and the pairing of each weight with its input.
  • Computing every context vector repeats the same three steps once per query, using one row of the attention weight table for each.

Related concepts

More concepts starting with E