Types of Neural Networks / Transformers
Transformers
AdvancedAttention-based architectures that process all tokens in parallel; the foundation of modern LLMs and vision models.
Scaled dot-product attention
Each token creates a Query, Key and Value. Attention weights decide how much each token attends to every other token.
d_k is the query/key feature dimension. Softmax acts across keys in each query row. M is an additive mask: 0 for allowed positions and −∞ for blocked positions; use M = 0 for unmasked attention. Every query must have at least one allowed key.
Transformer block
- Multi-head self-attention
- Position-wise feed-forward network
- Residual connections + Layer Normalization
- Positional encodings (sinusoidal, learned, RoPE)
Families
| Type | Example | Task |
|---|---|---|
| Encoder-only | BERT | Understanding / classification |
| Decoder-only | GPT | Text generation |
| Encoder-decoder | T5 | Translation, summarisation |
| Vision | ViT | Image classification on patches |
Interview questions & answers
Q1. Why is dot-product attention scaled by the square root of the key dimension?
Answer: Under common variance assumptions, query-key dot products grow in variance as the key dimension increases. Scaling counteracts this growth, reducing extremely peaked softmax distributions and helping maintain useful gradients.
Q2. Why does a transformer need positional information?
Answer: Unmasked self-attention alone is permutation equivariant and does not explicitly identify token order. Positional encodings or mechanisms such as rotary embeddings supply position-dependent information so the model can learn sequence order and relative relationships.
Q3. How do encoder-only and decoder-only transformers differ?
Answer: Encoder-only models usually let each token attend to all input positions and are commonly used for representation and understanding tasks. Autoregressive decoder-only models use causal masking to block future tokens. Training can process positions in parallel, but generation typically proceeds token by token.