# What Is a Transformer?

> What a transformer is, how self-attention lets it weigh the words around each word, and where it sits under today's language models, explained for your AI interview.

**In short:** A transformer is a neural network design built on self-attention, which lets each word in a text weigh the other words it is allowed to see while it works out meaning. It was introduced in 2017, and the GPT family of language models is built on it.

AI and LLM interview question Q041, Transformer architecture. Beginner, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-a-transformer

## Say it in 30 seconds

A transformer is a neural network design from 2017. Its main idea is self-attention: each word in a text weighs the other words it can see to work out its meaning. Older models read word by word, so transformers train faster in parallel. The GPT family of language models is built on it. The trade-offs I always mention are that the model needs extra position information, and that long inputs cost more.

## Say it in 2 minutes

A transformer is a kind of neural network, which is software that learns patterns from examples. It came out of a 2017 paper that proposed a design based only on attention, with no step-by-step loops. Attention means each word can look at the words it is allowed to see and decide which ones matter for its meaning. That one idea sits under the GPT family of large language models.

Older models read a text one word after another, so training could not run in parallel across the words. During training, a transformer handles the whole text in one pass, which makes training faster on modern chips. Google's team said in 2017 that the design needs less computation to train and speeds up training by up to an order of magnitude, which means up to about ten times faster. The trade-offs are real. The model has no built-in sense of order, so position must be added, and long inputs cost more, because each word is compared with many others.

Some transformers use both halves, an encoder that reads and a decoder that writes. Others use only the encoder, which suits sorting or searching text, or only the decoder, which writes the next token from the ones before it. When an interviewer asks, I describe the design as a stack of attention and per-word steps, then name the trade-offs. I never claim it understands text the way a person does.

## What is a transformer, in plain words?

A transformer is a kind of neural network. A neural network is software that learns patterns from many examples, instead of following rules a programmer wrote by hand.

The design comes from a 2017 paper called "Attention Is All You Need". The authors proposed a model built only from attention, with no step-by-step loops. The Hugging Face Transformers library now covers text, computer vision, audio and video models.

> "A neural network architecture developed at Google that relies on self-attention mechanisms to transform a sequence of input embeddings into a sequence of output embeddings without relying on convolutions or recurrent neural networks." (Google's machine learning glossary)
>
> "We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely." (The 2017 paper)

Think of a claims handler at an insurer working through a long file. When she reads the word it in one note, she checks back through every earlier note to see what it points to. A transformer does a version of that check for every word in the text. It is how a language model works out that a pronoun points to an invoice and not to a garage.

- **Transformer:** A neural network design built on self-attention, with no step-by-step loops.
- **Self-attention:** Each word weighs how much the other words it can see matter to its meaning. An encoder sees every word; a decoder sees only the words before it.
- **Encoder:** The part that reads a text and builds a picture of it.
- **Decoder:** The part that writes the output, one token at a time.

## How does a transformer work, step by step?

Your text is first cut into tokens, the units the model reads and predicts. Each token then goes through the same steps.

1. **Turn each token into numbers:** Each token becomes a list of numbers. The model also adds a position signal, because without it the model would not know which word came first.
2. **Let each word look at the words it can see:** This is self-attention. The model compares each word with the words it can see and mixes in the ones that matter most.
3. **Give each word its own step:** A small network then refines each word on its own, using what attention found.
4. **Stack the steps and repeat:** Those steps form one block, and the model stacks many blocks. Each pass gives every word a richer meaning, and the last layer gives the result.

*Diagram, What a transformer does with your text: Words go in, and each one looks at the words it may see. A small step then refines every word, and the stack repeats this. The last step gives the result, such as a label or the next word.*
1. Each word is turned into a list of numbers and given its position.
2. Each word gathers hints from the words it is allowed to see.
3. After the last round, the refined words give the result.

The 2017 paper describes its design as stacked self-attention and point-wise, fully connected layers for both the encoder and decoder. The point-wise part means one small network applied to each word on its own. Read the design as a stack of repeated blocks, each one a round of attention plus that per-word step.

## Why did it replace the older models, and what does it cost?

> "Experiments on two machine translation tasks show these models to be superior in quality while being more parallelizable and requiring significantly less time to train." (The 2017 paper, on translation tests)
>
> "we must inject some information about the relative or absolute position of the tokens in the sequence" (The 2017 paper, on word order)

Older models had to read word by word. Training could not run in parallel across the words of one text, because each word waited for the one before it. The paper says this sequential nature blocks parallel work.

During training, a transformer handles the whole text in one pass, so training can run in parallel on modern chips. In 2017, Google's team said the design needs less computation to train and fits modern hardware better, speeding up training by up to an order of magnitude, which means up to about ten times faster.

*Diagram, Reading word by word, or in one pass: The older way goes word by word. During training, a transformer reads the whole text together, so training can run in parallel on modern chips.*
1. Word 2 has to wait, because the old model reads word 1 before word 2.
2. During training, every word is handled in one pass, so the work runs in parallel and finishes sooner.

The costs are real. The model has no built-in sense of order, so position has to be added. Each word is also compared with many others, so a long input costs more work for the computer.

## Are all transformers built the same way?

Transformers come in three shapes. The shape depends on the job.

- **Encoder only: Reads and builds a picture.** Good for text classification, such as sorting claim notes into queues. BERT is built on the encoder part of the Transformer.
- **Decoder only: Writes one token at a time.** Predicts the next token from the ones before it. Large language models such as the GPT family work this way.
- **Both halves: Reads, then writes.** The encoder reads the input and the decoder writes the output. The 2017 translation design used both halves.

## How does it read a real claims note?

An insurer's claims team uses a model to read each claim note and sort it into a review queue. One note says: "The garage sent the invoice on 3 March, but it was never matched to the claim." The model must decide what it points to, because a wrong guess sends the claim to the wrong team.

1. **Each word starts with context:** The word it starts as a list of numbers. Self-attention then lets it look at garage, invoice and claim at the same time.
2. **Weights decide the focus:** The model gives more weight to invoice, because the note is about an invoice that was never matched. The weights are learned from training data, not typed in by hand.
3. **Repeat through the stack:** Each block refines the reading, so the meaning of it gets sharper before the queue is chosen.
4. **Test before trusting it:** Run a sample of real notes with known queues. Check the wrong guesses by hand before the model sorts live claims.

> **Tip:** In an interview, tell this story. It shows the job, the failure and the trade-off in one example.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a neural network built on self-attention, from the 2017 paper.
- [ ] Self-attention in one line: each word weighs the other words it can see.
- [ ] Why it beat older models: in training, all words go through one pass, so work runs in parallel.
- [ ] Position: the model needs extra information to know word order.
- [ ] Encoder, decoder or both, and which one fits which job.
- [ ] The cost: each word is compared with many others, so long inputs cost more.

**Common traps**

- Trap: "A transformer is the same as GPT." Better: GPT is one family built on transformers. The transformer is the design underneath it.
- Trap: "It reads text one word at a time." Better: That describes the older models. During training, a transformer handles the whole text together.
- Trap: "It understands text like a person." Better: It learns patterns from large amounts of text. Say what it does, then test it on your own data.

## Can you spot the wrong line?

**Spot the wrong line:** A colleague pasted these lines into the team's onboarding doc. Which one is wrong?

A. An encoder reads all the words of a claim note in one pass.
B. Self-attention lets each word weigh the other words it can see.
C. A transformer is a database, so it keeps a copy of every claim it reads.
D. Position information tells the model which word came first.

**Answer:** C. A transformer is a neural network. It learns patterns from data, and it does not store each claim the way a database does.

**Quick check:** Why does a transformer need position information?

A. It has no built-in sense of word order
B. It makes the model train faster
C. It stores the context window
D. It cuts the number of tokens

**Answer:** A. Attention has no built-in sense of word order, so position is added to each token to fix that.

## Can you answer these from memory?

**Flash cards**

- **What is a transformer?** A neural network built on self-attention, with no step-by-step loops or convolutions, from a 2017 paper.
- **What is self-attention?** Each word weighs how much the other words it can see matter to its meaning.
- **Why do transformers need positions?** Attention has no built-in sense of order, so the model needs extra position information.
- **Why did they beat older models?** Older models read word by word. During training, transformers process the whole text in one pass.
- **Encoder or decoder?** The encoder reads and represents text. The decoder writes output one token at a time.
- **What is the cost to watch?** Each word is compared with many others, so long inputs cost more work.

## Likely follow-up questions

- Q042 Why did transformers replace RNNs and LSTMs? https://designervenkat.online/interview/ai/why-transformers-replaced-rnns-and-lstms
- Q043 What is self-attention? https://designervenkat.online/interview/ai/what-is-self-attention
- Q044 What are queries, keys and values? https://designervenkat.online/interview/ai/what-are-queries-keys-and-values
- Q046 What is multi-head attention? https://designervenkat.online/interview/ai/what-is-multi-head-attention
- Q047 What is positional encoding, and why is it needed? https://designervenkat.online/interview/ai/what-is-positional-encoding
- Q049 Encoder-only vs decoder-only vs encoder-decoder? https://designervenkat.online/interview/ai/encoder-only-vs-decoder-only-vs-encoder-decoder

## Learn it in depth

- [Transformers](https://designervenkat.online/learn/machine-learning/transformers)
