Artificial intelligence • Original RRTutors lesson
Transformers, Attention, and Embeddings
Learn how attention lets transformer models relate tokens and how embeddings represent items as vectors.
Concept and explanation
An embedding maps a token, image, or other item to a vector. Self-attention computes context-dependent combinations of token representations, allowing a transformer to model relationships across a sequence. Positional information, masking, architecture, and training objective affect what a model can represent.
How to apply it
Track tensor shapes and distinguish tokenization, embedding lookup, attention weights, and the final task head.
Worked example
In a sentence, attention can help a token representation incorporate information from other tokens in its context.
Illustrative Python example
import torch
scores = torch.tensor([[1.0, 2.0, 0.5]])
weights = torch.softmax(scores, dim=-1)
print(weights.sum().item())
Expected result
Softmax converts scores into non-negative weights summing to one.
Limitations and cautions
Attention weights are not a complete explanation of model decisions, and context length has compute and memory costs.
Practice exercise
Explain why an embedding is not a definition and why attention alone does not guarantee reasoning.
Key takeaway: Transformer components are mathematical operations whose behavior comes from training and context.