Please enable JavaScript to use CodeHS

AI Explained

Attention and transformers

Attention lets every word look at every other word and decide which ones are most relevant. Stack attention with feed-forward layers and you get the transformer.

AI ~13 min High school
The big idea

The attention idea

When you read the word “bank” in a sentence, you do not guess its meaning in isolation. You look at the surrounding words. If the sentence is “I sat by the river bank,” you think of the edge of a river. If it is “I went to the bank to deposit money,” you think of a financial building.

Attention gives a neural network the same ability. It lets every word in a sentence look at every other word and decide which ones matter most for understanding the current word.

Core idea: instead of compressing a sentence into one fixed-size vector, attention computes a weighted mix of every word’s vector, where the weights depend on the current word.


Mechanism

Query, key, value

For every word, the model creates three vectors:

  • Query (Q): “What am I looking for?”
  • Key (K): “What do I contain?”
  • Value (V): “What information should I pass along if I am picked?”

The attention score between word i and word j is the dot product of word i’s query and word j’s key. Then the scores are run through softmax so they become percentages that add to 1.

attention.py
import math

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def softmax(scores):
    exp = [math.exp(s) for s in scores]
    total = sum(exp)
    return [e/total for e in exp]

scores = [dot(query_i, key_j) for key_j in keys]
weights = softmax(scores)
new_word_i = sum(w * v for w, v in zip(weights, values))

Interactive

Attention heatmap

Below is a tiny self-attention model for the sentence “the robot fixed the code.” Click any word to see how strongly it attends to every other word. Bright cells mean high attention.

Rows = selected word; columns = words being attended to.

New vector for the selected word: —

Architecture

The transformer block

A transformer is built by stacking many identical blocks. Each block has two main pieces:

  1. Self-attention: mix information across words.
  2. Feed-forward network: process each word independently, adding non-linearity.

There are also a few important tricks:

  • Positional encoding: words are processed in parallel, so the model adds a positional signal to each embedding to know where a word appears in the sentence.
  • Residual connections: the output of each layer is added to its input, so the model can learn small updates on top of what it already knows.
  • Layer normalization: keeps the numbers stable as the network gets very deep.

Why it matters: the transformer architecture is the engine behind GPT, Claude, Gemini, and most modern LLMs. It can handle long context in parallel and scales well to huge models.


Check yourself
?

Which word should “it” attend to?

In the sentence “The robot fixed the code because it had a bug,” the word “it” probably refers to “code.” In the sentence “The robot fixed the code because it was broken,” “it” still refers to “code.” How does attention help?

Attention gives “it” a high score with the word it refers to — in this case, “code.” The model compares the query for “it” with the keys for “robot,” “code,” and “bug,” and the strongest match tells it which antecedent is most likely.


Quick quiz

Three questions

1. In self-attention, what does the query vector ask?

2. Why do transformers need positional encoding?

3. What does softmax do to the attention scores?

Keep learning

Attention and transformers are powerful, but they only work because of careful training. The next tutorial covers the tricks that make modern training work: activations, normalization, optimizers, and more.

Part of the AI Explained tutorial series.