liveBook · Manning

Value

As explained in Transformers in Action

In attention, a value is a vector representation containing the content aggregated according to attention weights.

A value is the content an attention mechanism weighs and combines to produce an output vector. Each input vector has three roles: its query looks for relevant inputs, its key helps determine relevance, and its value supplies content to the weighted sum. Keys help set the attention weights; values are multiplied by those weights after softmax. section 2.2.3

In scaled dot-product attention, input embeddings are multiplied by separate matrices to form Q, K, and V. Softmax converts scores computed from Q and K into attention weights; multiplying those weights by V produces the attention output. Values may start from input embeddings, while stacked attention layers develop more contextual representations. section 2.2.3

“The movie was not bad”

If “not” is the query, “bad” may receive a high attention weight because the words are closely related in the phrase. The values for “not” and “bad,” each multiplied by its weight, contribute to the output for “not.” section 2.2.3

Listing 2.3 Simplified scaled dot-attention implementation
embed_1 = np.array([0, 1, 0]) #1
embed_2 = np.array([1, 0, 1])
embed_3 = np.array([0, 1, 1])
embed_4 = np.array([1, 1, 0])

embeddings = np.array([embed_1, embed_2, embed_3, embed_4]) #2

Wq = rand(3, 3)  #3
Wk = rand(3, 3) 
Wv = rand(3, 3)

Q = embeddings.dot(Wq)  #4
K = embeddings.dot(Wk)
V = embeddings.dot(Wv)

attention_scores = softmax(Q.dot(K.T) / sqrt(K.shape[1]), axis=1) #5

attention_output = attention_scores.dot(V) #6

The NumPy listing forms V with `embeddings.dot(Wv)`, then computes `attention_scores.dot(V)`. With four three-component embeddings, this produces one three-component output per input. section 2.2.3

How Transformers in Action covers it

How deep it goes
Explains the value role intuitively and implements its projection and weighted aggregation in simplified attention code.
Examples
Uses a sentence about a movie and a NumPy scaled dot-product attention listing.
What it assumes
Assumes input embeddings and basic matrix operations while introducing query, key, and value roles through plain-language intuition.
What you can do after
Build it from scratch in NumPy by projecting embeddings into V and multiplying V by attention scores.
How it connects
Connects values to queries, keys, softmax, self-attention, and multihead attention.

Also explained in

Compare how 8 books explain value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.