Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

What Is Self-Attention? How Transformers Connect Tokens

Self-attention lets Transformer tokens incorporate information from other positions. Here’s how queries, keys, values, multiple heads, masks, and sequence-length costs fit together.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Self-attention lets a Transformer update each token’s representation using information from other positions in the same sequence. It does this by comparing learned query and key vectors, turning those comparisons into weights, and using the weights to mix value vectors. The operation is central to Transformers, but it is only one part of the architecture.

How self-attention works

Think of each position as determining which other positions may be useful for updating its representation. This is an analogy, not a claim that a model consciously focuses: the computation uses learned vectors and normalized scores.

As an Amazon Associate I earn from qualifying purchases.

Given input representations X, learned linear projections produce queries Q, keys K, and values V. The scaled dot-product attention operation is:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Attention(Q, K, V) = softmax(QKT / √dk)V

Here, a query is compared with keys to score how relevant each position is; dividing by the square root of the key dimension dk scales those scores. Softmax turns the scores into weights, and those weights are applied to the values to produce a weighted mixture. In self-attention, queries, keys, and values are projected from the same sequence representation. A mask may restrict which positions can contribute.

Why Transformers use multiple heads and positional information

Multiple heads

Multi-head attention uses separate learned projections to calculate several attention operations in parallel. Their outputs are concatenated and projected to form the layer output. Multiple heads provide distinct learned projections, but that does not mean a particular head always corresponds to a fixed linguistic concept.

Position matters

Self-attention alone does not encode token order: by itself, it does not know whether a token came first or later in the sequence. The original Transformer adds positional encodings to its input embeddings to provide that information.

Attention is one component of a block

The original Transformer also uses position-wise feed-forward networks, residual connections, and layer normalization. These components help transform information across layers; self-attention is not the whole architecture.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Self-attention and cross-attention are different

Operation Queries come from Keys and values come from What it connects
Self-attention The sequence being updated The same sequence representation Positions within one sequence
Cross-attention The decoder representation Encoder outputs Decoder positions to encoded input information

In encoder self-attention, positions can draw on other positions in the input sequence. Decoder self-attention is different when generating outputs: a causal mask blocks access to subsequent output positions, so a prediction cannot use future target tokens. In encoder-decoder cross-attention, decoder queries are compared with encoder keys and values; because the two sides use different representations, this is not self-attention.

Why full self-attention can be expensive

Full self-attention computes interactions across sequence positions. Its attention-score matrix therefore grows quadratically with sequence length: doubling the length means four times as many pairwise position interactions. This cost can make memory and computation substantial for long sequences. In exchange, positions can interact directly, and the operation allows parallel computation across positions during training.

Linear-attention alternatives

Alternative formulations change how attention is computed, and their trade-offs depend on the method and task. Katharopoulos and coauthors describe a linear-attention formulation using kernel feature maps and matrix associativity to reduce sequence-length complexity from O(N²) to O(N). In their 2020 experiments, they reported up to 4000× speed for autoregressive prediction of very long sequences. That is a result for their experiments, not a general speed guarantee for every model, task, or workload. Read the paper on linear attention.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What attention weights can—and cannot—tell you

The equation explains how a layer combines value vectors; it does not establish that attention weights provide a complete explanation of a model’s reasoning. They show the weights used in that operation, not a full account of why the model produced an answer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A theoretical result also needs careful scope. Dong, Cordonnier, and Loukas analyzed pure self-attention without skip connections or MLPs and found that it converges toward rank one with depth. Their analysis describes a restricted architecture; residual connections and feed-forward networks prevent the specific degeneration they describe. It does not show that ordinary Transformers collapse in practice. Read the analysis of pure self-attention.

What the original Transformer paper reported

Vaswani and coauthors introduced the Transformer architecture in 2017. Their paper reports 28.4 BLEU on the WMT 2014 English-to-German task and 41.8 BLEU on WMT 2014 English-to-French. These are reported translation results, not measures of attention quality. The Google Research record displays 41.0 for English-to-French, while the original arXiv paper abstract states 41.8; the figures should not be merged. See the original paper and Google Research’s record.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.