liveBook · Manning

Query

As explained in Deep Learning with PyTorch, Second Edition

A learned projection of an input position used to score how relevant other positions are in attention.

A query represents what the model is looking for at one position in a sequence. Attention compares each query with keys from other positions, then retrieves values from the positions that best match. This lets the model focus on contextually relevant information instead of treating every position equally. section 9.5

In dot-product self-attention, queries participate in this sequence:

  • Project each input position into a query vector using its own learned weights and bias. section 9.5.1
  • Compute dot products between query and key vectors to score how relevant each position is. section 9.5.1
  • Apply softmax to normalize the scores into attention weights, then use those weights to combine value vectors. section 9.5.1

Query projection in PyTorch

For an input x with shape [2, 3], a query matrix can be created with `F.linear(x, weight, bias)`. The resulting queries can then be compared with keys using `q @ k.T`. 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]])

The listing shows query, key, and value projections followed by dot-product scoring, softmax normalization, and value aggregation. section 9.5.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains query projections, query-key dot products, softmax weighting, and value aggregation, without detailing training or parameter learning.
Examples
Demonstrates query projections and attention scoring with a two-position, three-dimensional PyTorch tensor.
What it assumes
Assumes basic embeddings, linear layers, matrix multiplication, and softmax while motivating attention through sequence prediction.
What you can do after
Build it from scratch by projecting inputs into queries and using them to compute normalized attention-weighted outputs.
How it connects
Connects queries with keys, values, self-attention, cross-attention, softmax, transformers, and attention weights.

Also explained in

Compare how 7 books explain query →

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.