liveBook · Manning

Cross-attention

As explained in Machine Learning Algorithms in Depth

Cross-attention computes attention with queries from one sequence or representation source and keys and values from another.

Cross-attention is multihead attention in which queries come from one representation source while keys and values come from another. In an encoder-decoder transformer, the decoder supplies queries and the encoder supplies keys and values, linking generated output positions to the input sequence. section 11.3

The encoder-decoder arrangement works as follows:

  • Treat the encoder representation as a database of keys and values. section 11.3
  • Use the decoder’s target sequence as the query source. section 11.3
  • Associate each target word with related input words while generating the output sentence. section 11.3
  • Combine this operation with masked self-attention, which preserves causality during one-token-at-a-time generation. section 11.3

Neural machine translation

While translating, the decoder queries the encoded input sentence to determine which input words are relevant to the next output word. section 11.3

Figure 11.10 Transformer architecture
figure

Use the transformer architecture figure to locate encoder-decoder attention alongside the decoder’s other attention components. section 11.3

How Machine Learning Algorithms in Depth covers it

How deep it goes
Defines cross-attention through differing query, key, and value sources and explains its role, but does not derive numerical computation.
Examples
Uses neural machine translation, with decoder target tokens querying encoded input tokens.
What it assumes
Assumes familiarity with multihead self-attention, queries, keys, values, and encoder-decoder transformers.
What you can do after
Apply cross-attention to relate each generated target word to relevant words in an encoder-decoder transformer.
How it connects
Connects multihead attention, encoder-decoder architecture, masked self-attention, softmax, and machine translation.

Also explained in

Compare how 4 books explain cross-attention →

Related concepts

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