liveBook · Manning

Attention score

As explained in Deep Learning with R, Third Edition

An attention score is a numerical value measuring the relevance of one vector or sequence position to another in an attention calculation.

An attention score measures how relevant a source vector is to the target vector currently being processed. In sequence-to-sequence translation, high-scoring source tokens contribute more information to the prediction, helping the model retrieve relevant context without passing it through a long recurrent loop. section 15.3.1

Dot-product attention turns scores into a context representation:

  • Compute the dot product between each target vector and each source vector; nearby vectors receive higher scores. section 15.3.1
  • Apply softmax across the source positions so the scores for each target sum to 1. section 15.3.1
  • Take the weighted sum of source vectors, using the normalized scores as weights. section 15.3.1
  • For target and source sequences, organize the results as a matrix whose rows describe one target position’s source relevance. section 15.3.1

Minimal attention calculation

For one target vector, score every source vector, normalize the scores, and combine the sources: scores <- sources |> sapply(\(source) score(target, source)) |> softmax(); combined <- sum(scores * sources).

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains dot products, softmax normalization, weighted sums, batched score matrices, and their role in attention computation.
Examples
Uses translation vectors, a normalized weighted-sum pseudocode example, and an R tensor implementation with sequence score matrices.
What it assumes
Starts from source and target vector sequences, introduces the scoring function, and assumes familiarity only with dot products and softmax operations.
What you can do after
Build dot-product attention that normalizes source relevance and combines source vectors into context representations.
How it connects
Connects attention scores to dot products, softmax, weighted sums, RNN limitations, and Transformer sequence modeling.

Also explained in

Compare how 4 books explain attention score →

Related concepts

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