Transformer Model in AI: Definition and Examples
Quick answer
What a transformer model is, how self-attention works, a short numpy example, and how a transformer differs from an LLM.
A transformer model in AI is a neural network architecture that processes a sequence, such as a sentence, by letting every element look at every other element through self-attention. It was introduced in 2017 and is the architecture behind most large language models, though the transformer itself is the design, not the finished product.
Key takeaways
- A transformer is an architecture (a way of wiring a neural network), not a specific product like a chatbot.
- Its core idea is self-attention: each token weighs how relevant every other token is.
- The 2017 paper dropped recurrence and convolutions entirely and relied on attention alone.
- LLMs are built on transformers, but transformers are also used for images and other data.
On this page:
- What a transformer model means
- How a transformer works, with a worked trace
- Where transformers are used
- Common mistakes and confusions
- Related terms
- Frequently asked questions
| Term | What it is | Relation to a transformer |
|---|---|---|
| Transformer | An architecture built on self-attention | The design itself |
| LLM | A large trained language system | Usually built using a transformer |
| RNN | A network that reads a sequence one step at a time | The recurrent design transformers moved away from |
| BERT | A specific model | Uses the transformer architecture and relies on self-attention, per the Google Machine Learning Glossary |
What does a transformer model mean?
It means a model whose main building block is attention. According to the original paper on arXiv (Vaswani et al., 2017), the transformer is "based solely on attention mechanisms, dispensing with recurrence and convolutions entirely."
IBM (28 March 2025) describes it as a neural network architecture that excels at processing sequential data, and says its central feature is the self-attention mechanism. Text is a sequence of tokens, so it fits well.
How does a transformer work?
Each token is turned into a vector, then the model scores how much each token should pay attention to every other token. Those scores become weights, and each token's new representation is a weighted mix of the others.
An illustrative trace with made-up numbers: for the phrase "bank by river", suppose the attention row for "bank" is 0.2 for "bank", 0.1 for "by" and 0.7 for "river". The model reads "bank" mostly through "river", so it leans towards the riverside meaning, not the money one.
Here is a runnable numpy sketch of scaled dot-product attention. The vectors are random (seed 0) and only illustrate the shapes; each row of the printed weights sums to 1.
import numpy as np
rng = np.random.default_rng(0)
Q = rng.normal(size=(3, 4)) # 3 tokens, 4 dimensions
K = rng.normal(size=(3, 4))
V = rng.normal(size=(3, 4))
scores = Q @ K.T / np.sqrt(4)
weights = np.exp(scores) / np.exp(scores).sum(axis=1, keepdims=True)
output = weights @ V
print(weights.round(2))
print(output.shape)The three encoder and decoder patterns are worth knowing. The original translation setup used both an encoder and a decoder. BERT-style models use the encoder side, and GPT-style models use the decoder side.
The paper reported 28.4 BLEU on WMT 2014 English-to-German, improving on prior best results by over 2 BLEU. For English-to-French it reported 41.8 BLEU for a single model, after training for 3.5 days on eight GPUs.
Where are transformers used?
They are used for language, and beyond. IBM says the architecture gave rise to large language models and also powers many diffusion models used for image generation.
An illustrative Indian example: a code-mixed chat message such as "kal exam hai, notes bhejo" is split into tokens, and attention lets "notes" and "exam" inform each other across the whole message. This is an illustration, not a benchmark. For the product side, see our LLM explained with Indian examples and the longer large language models guide.
What are the common mistakes?
- Calling a transformer an LLM. The transformer is the architecture. An LLM is a trained system that often uses one.
- Thinking transformers only handle text. IBM notes they also power image-generation models.
- Confusing "attention" with human focus. It is a set of learned weights, nothing more.
Which terms are related?
Self-attention is the mechanism, and neural networks are the wider family. Compare the layers in AI vs machine learning vs deep learning, and see types of machine learning for context.
Next step
Run the numpy snippet, change the seed, and watch how the weights shift. Then read the first page of the paper itself.
Frequently asked questions
What is a transformer in AI?
A transformer is a neural network architecture that processes sequences using self-attention. Each token weighs how relevant every other token is, which lets the model read words in context. It was introduced in a 2017 paper.
Is a transformer the same as an LLM?
No. The transformer is the architecture. An LLM is a trained system, and most are built on a transformer, but the two terms are not interchangeable.
Why is it called attention?
The model assigns a weight to every other token in the sequence, deciding how much each one matters when reading the current token. Those weights are the attention.
Why did transformers replace RNNs?
The 2017 paper dropped recurrence and convolutions and used attention alone, and reported strong translation scores with a training time of 3.5 days on eight GPUs for English-to-French.
Are transformers only used for text?
No. IBM says the architecture also powers many diffusion models used for image generation, alongside its role in large language models.