liveBook · Manning

Value

As explained in Deep Learning with PyTorch, Second Edition

The transformed representation vectors that are combined using attention weights to produce the output for each position.

A value is the content attention retrieves after deciding which input positions matter. Unlike a static character embedding, it contributes context-dependent information to the output. section 9.5

In self-attention, a learnable linear transformation converts each input embedding into a value vector. For position n, attention combines all value vectors using weights that describe how much position n should focus on position m. section 9.5.1

The value participates in attention as follows:

  • Transform every input embedding into a value vector with a learnable linear layer. section 9.5.1
  • Compute attention weights from query-key relationships, then normalize them with softmax. section 9.5.1
  • Produce each output by taking the weighted sum of the value vectors. section 9.5.1

Values in a small attention calculation

A PyTorch example applies F.linear separately to create query, key, and value tensors, then computes attention weights with q @ k.T, normalizes them with F.softmax, and returns attn_weights @ v. section 9.5.1

Listing 9.2 attention.ipynb
# In[]:
torch.manual_seed(0)
x = torch.rand(2, 3)

query = F.linear(x, weight=torch.rand(3, 3),
bias=torch.rand(3))                          #1
key = F.linear(x, weight=torch.rand(3, 3), bias=torch.rand(3))
value = F.linear(x, weight=torch.rand(3, 3), bias=torch.rand(3))

def dot_product_attention_single(q, k, v):
    attn_weights = q @ k.T    #2
    attn_weights = F.softmax(attn_weights, dim=-1)  #3
    output = attn_weights @ v   #4
    return output

dot_product_attention_single(query, key, value)

# Out[]:
tensor([[1.3667, 0.6913, 1.0614],
        [1.3718, 0.6930, 1.0627]])

Inspect the runnable attention listing to see value-vector creation and the final weighted combination in PyTorch. section 9.5.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains linear value projection and weighted summation, while treating query-key weight calculation as the surrounding attention mechanism.
Examples
Demonstrates character embeddings, next-character prediction, and a PyTorch calculation using F.linear, softmax, and matrix multiplication.
What it assumes
Builds from character embeddings and introduces values alongside queries and keys within attention.
What you can do after
Apply via PyTorch by projecting inputs into values and combining them with attention weights.
How it connects
Connects values with queries, keys, attention weights, self-attention, embeddings, softmax, and transformers.

Also explained in

Compare how 8 books explain value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.