Modern AI Systems and Generative AI
understanding Transformers, generative model families, and the systems built around them
Read diagram labels
- Input token embedding (X)
- Query (Q)
- Key (K)
- Value (V)
- Q × Kᵀ
- Softmax
- Attention-weighted output
Core Concepts Covered
- Attention history, scaled dot-product self-attention, and complete Transformer blocks
- Autoregressive models, VAEs, GANs, and diffusion model tradeoffs
- LLM training, RAG, multimodality, tool use, and alignment
Local Setup Recommendation
To execute and experiment with the code cells below on your local machine, ensure you have set up your isolated virtual environments and scientific libraries by following the detailed protocols in Unit 03: Environment Setup or run them in Google Colab.
1. From Recurrent Bottlenecks to Attention and Transformers
Early neural machine translation commonly used an encoder RNN or LSTM to compress a source sentence into a fixed-length context vector, then a decoder RNN to generate the translation. The 2014 sequence-to-sequence result showed that this approach could work well, but asking one vector to preserve every detail created a bottleneck, especially for long sentences. Recurrence also makes the training path serial: hidden state depends on .
Bahdanau additive attention changed the interface between encoder and decoder. At every decoding step, the model scored all encoder states, normalized those scores, and formed a new weighted context. Luong attention explored effective dot-product and multiplicative scoring variants. Attention therefore began as a way for an RNN decoder to retrieve the source information it needed instead of relying on one fixed summary.
The 2017 Transformer removed recurrence from the main sequence path and built the model from attention and position-wise feed-forward layers. In 2018, GPT demonstrated generative pretraining followed by task adaptation, while BERT demonstrated deep bidirectional encoder pretraining. Later systems scaled these architectural families with more data, parameters, compute, and increasingly specialized training stages.
Transformers became dominant in large-scale language systems because tokens can be processed in parallel during training, attention gives short paths between distant positions, retrieval is content-dependent, and the architecture scales efficiently on accelerator hardware. The comparison needs nuance: autoregressive generation is still sequential, standard attention costs in sequence length, position information must be supplied, and RNNs can remain attractive for low-memory streaming or tightly constrained devices.
From Recurrent Compression to Direct Attention
Read diagram labels
- 2014
- Seq2Seq
- one context
- 2014–15
- Attention
- retrieve states
- 2017
- Transformer
- no recurrence
- 2018+
- GPT / BERT
- pretrain + scale
- RECURRENT PATH
- h1
- h2
- h3
- h4
- long serial path through hidden states
- SELF-ATTENTION PATH
- x1
- x2
- x3
- x4
- direct content-dependent connections
2. Scaled Dot-Product Self-Attention, Step by Step
Suppose holds token embeddings. Learned projections produce , , and . With key width and value width , their shapes are and . Row of asks a query; every row of describes what that token offers; the matching weights retrieve rows of .
The scores, row-wise attention weights, and contextual outputs are The scale prevents high-dimensional dot products from routinely pushing softmax into nearly flat-gradient regions. Each row of sums to , so output row is a weighted mixture of the value vectors.
For a complete three-token example, let Then and For example, the first output is .
A decoder must not look at future tokens. A causal mask replaces entries above the score-matrix diagonal with before softmax. Their exponentials become zero, while the permitted entries in each row are renormalized. A padding mask similarly prevents attention to batch padding; it is not the same as a causal mask.
Scores Become Row-Wise Attention Weights, Then Mix Values
Read diagram labels
- SCORES S
- .707
- 0
- 1.414
- softmax
- WEIGHTS A
- .401
- .198
- .248
- .503
- each row sums to 1
- × V
- CONTEXT AV
- 1.604
- .797
- 1.401
- 1.203
- 1.759
- 1.000
- CAUSAL MASK BEFORE SOFTMAX
- future scores → −∞ → future attention weights become 0
import math
import torch
torch.manual_seed(0)
Q = K = torch.tensor([[1., 0.], [0., 1.], [1., 1.]])
V = torch.tensor([[1., 0.], [0., 2.], [3., 1.]])
scores = Q @ K.T / math.sqrt(Q.shape[-1])
attention = torch.softmax(scores, dim=-1)
context = attention @ V
print("S =\n", scores.round(decimals=3))
print("A =\n", attention.round(decimals=3))
print("AV =\n", context.round(decimals=3))
# Decoder-only causal version: future columns become -infinity.
causal = torch.triu(torch.ones(3, 3, dtype=torch.bool), diagonal=1)
causal_attention = torch.softmax(scores.masked_fill(causal, float("-inf")), dim=-1)
print("Causal A =\n", causal_attention.round(decimals=3))Example output (precomputed; this website does not execute code):
S =
tensor([[0.707, 0.000, 0.707],
[0.000, 0.707, 0.707],
[0.707, 0.707, 1.414]])
A =
tensor([[0.401, 0.198, 0.401],
[0.198, 0.401, 0.401],
[0.248, 0.248, 0.503]])
AV =
tensor([[1.604, 0.797],
[1.401, 1.203],
[1.759, 1.000]])
Causal A =
tensor([[1.000, 0.000, 0.000],
[0.330, 0.670, 0.000],
[0.248, 0.248, 0.503]])Reading one row of an attention matrix
What does mean, and why is it not a hard selection?
1.Row describes the query made by token ; column refers to token 's key and value.
2. assigns of the second output's mixture weight to value vector .
The other weights are nonzero and the row sums to , so attention performs differentiable weighted retrieval rather than choosing a single token.
Rows correspond to querying positions, columns to retrieved positions, and mixes values using those row weights.
3. Inside a Complete Transformer Block
One attention head captures one learned similarity space. Multi-head attention runs projected heads, concatenates their outputs, and applies : . Different heads can specialize in different patterns, although a head is not guaranteed to have a simple human-readable role.
A block combines attention with a position-wise feed-forward network such as . Residual paths preserve an identity route around each sublayer, and layer normalization stabilizes activations. Implementations may place normalization before or after a sublayer. Token embeddings also receive learned or fixed positional information because attention alone is permutation-equivariant.
Encoder-only models use bidirectional self-attention and are natural for representation tasks. Decoder-only models use causal self-attention for next-token generation. Encoder-decoder models let a causal decoder cross-attend to a separately encoded input, which suits conditional generation such as translation. Padding masks hide absent tokens; causal masks hide future tokens.
Training a decoder can score all known next-token targets in parallel, but inference must produce the next token before the following one exists. A KV cache reuses earlier keys and values instead of recomputing the full prefix at each generation step. It reduces repeated computation but consumes memory. Standard dense attention still stores or computes an interaction pattern, giving the familiar sequence-length tradeoff.
A Transformer Block Preserves Residual Information
Read diagram labels
- TOKEN + POSITION EMBEDDING
- MULTI-HEAD ATTENTION
- heads → concat → Wₒ
- ADD RESIDUAL + LAYER NORM
- FEED-FORWARD NETWORK
- W₂ σ(W₁x + b₁) + b₂
- MASK CHOICE
- encoder: padding
- decoder: causal + padding
- IDENTITY PATHS
- preserve signal
4. The Generative Modeling Landscape
Generative AI is not one architecture. An autoregressive Transformer factorizes a sequence into next-step conditionals, has no required global latent code, and samples one token at a time. It offers stable likelihood-based training and excels at discrete sequences, but long generation is sequential.
A Variational Autoencoder (VAE) learns a smooth probabilistic latent space with a reconstruction-plus-regularization objective. Sampling is usually one decoder pass and latent interpolation is meaningful, though image outputs can look blurred. A Generative Adversarial Network (GAN) learns through a generator-discriminator game. It can produce sharp samples in one pass but training balance and mode coverage are difficult.
A diffusion model learns to reverse progressive corruption, usually beginning from noise. Its denoising objective is stable and its coverage and visual quality are strong, but conventional sampling requires many iterative network evaluations. Unit 16 provides the variational-inference foundation for VAEs, and Unit 19 develops diffusion and score-based generation in depth. Model choice depends on the data, controllability, latency, likelihood needs, and failure costs, not on a universal winner.
5. Variational Autoencoders: Learning a Generative Latent Space
An ordinary autoencoder maps an input to one code and reconstructs it. A Variational Autoencoder (VAE) instead uses an encoder to predict a distribution over latent codes, usually diagonal Gaussian parameters and . A decoder maps a sampled code back to a distribution over observations.
Sampling directly would obstruct backpropagation through the encoder. The reparameterization trick writes Randomness now enters through , while and remain differentiable transformations.
Training minimizes a reconstruction term plus divergence from a simple prior: For a diagonal Gaussian, . The reconstruction term preserves information about ; KL regularization makes nearby latent samples decode coherently.
A structured latent space supports interpolation and conditional generation. Important failure modes include posterior collapse, where the decoder ignores and the approximate posterior matches the prior, and blurred outputs when a simple pixel-wise likelihood averages multiple plausible reconstructions. Capacity, the KL weight, decoder strength, and likelihood choice all influence this balance.
A VAE Samples a Regularized Latent Distribution
Read diagram labels
- INPUT
- x
- ENCODER
- qφ(z | x)
- μ
- log σ²
- SAMPLE z
- μ + σ⊙ε
- DECODER
- pθ(x | z)
- ELBO TRADEOFF
- reconstruct x + keep qφ(z | x) near N(0, I)
import torch
import torch.nn as nn
torch.manual_seed(7)
class TinyVAE(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Linear(4, 4) # two mus + two log-variances
self.decoder = nn.Linear(2, 4)
def forward(self, x, epsilon):
mu, logvar = self.encoder(x).chunk(2, dim=-1)
z = mu + torch.exp(0.5 * logvar) * epsilon
reconstruction = self.decoder(z)
return reconstruction, mu, logvar, z
# Fix tiny weights so the forward pass has a reproducible result.
model = TinyVAE()
with torch.no_grad():
model.encoder.weight.zero_()
model.encoder.bias.copy_(torch.tensor([0.5, -0.5, 0.0, 0.0]))
model.decoder.weight.copy_(torch.tensor([
[1.0, 0.0], [0.0, 1.0], [1.0, 1.0], [-1.0, 1.0]
]))
model.decoder.bias.zero_()
epsilon = torch.tensor([[0.2, -1.0]])
reconstruction, mu, logvar, z = model(torch.zeros(1, 4), epsilon)
reconstruction_loss = torch.tensor(0.80)
kl_loss = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
total_loss = reconstruction_loss + kl_loss
print("z:", z.tolist())
print("decoder output:", reconstruction.tolist())
print(f"reconstruction: {reconstruction_loss:.2f}")
print(f"KL: {kl_loss:.2f}")
print(f"total: {total_loss:.2f}")Example output (precomputed; this website does not execute code):
z: [[0.699999988079071, -1.5]]
decoder output: [[0.699999988079071, -1.5, -0.800000011920929, -2.200000047683716]]
reconstruction: 0.80
KL: 0.25
total: 1.05One VAE latent sample and ELBO
Use , , and . If reconstruction loss is , compute , KL loss, and total loss.
1.Because , each standard deviation is .
2..
3.With unit variances, the KL term is .
The total loss is .
The reparameterized sample is differentiable with respect to the encoder outputs, while KL keeps the learned distribution near the prior.
6. Generative Adversarial Networks: Learning Through Competition
A Generative Adversarial Network (GAN) contains a generator that maps random noise to synthetic samples and a discriminator that estimates whether an input came from the data distribution. The original minimax game is
Training alternates the players. During a discriminator update, real examples should approach label , generated examples should approach label , and fake samples are detached so that step does not update . During a generator update, gradients flow through into . In practice, the non-saturating generator loss supplies stronger early gradients than minimizing .
GANs can generate sharp samples in one forward pass, but their game is sensitive to learning rates, architecture, and the relative strength of the two players. Mode collapse occurs when many noise inputs map to a narrow set of outputs. A low generator loss alone does not prove diversity or faithful coverage, so sample quality and distributional metrics must be examined together.
GAN Training Alternates Two Competing Updates
Read diagram labels
- NOISE z
- sample prior
- GENERATOR G
- z → synthetic x
- fake
- DISCRIMINATOR D
- probability of real
- REAL DATA x
- target = 1
- D UPDATE
- real → 1, fake → 0
- detach fake
- G UPDATE: −log D(G(z))
- gradient flows through D into G
import torch
import torch.nn as nn
torch.manual_seed(11)
G = nn.Sequential(nn.Linear(2, 4), nn.ReLU(), nn.Linear(4, 2))
D = nn.Sequential(nn.Linear(2, 4), nn.ReLU(), nn.Linear(4, 1))
loss_fn = nn.BCEWithLogitsLoss()
g_opt = torch.optim.Adam(G.parameters(), lr=1e-3)
d_opt = torch.optim.Adam(D.parameters(), lr=1e-3)
real = torch.tensor([[1.0, 0.8], [0.9, 1.1]])
z = torch.tensor([[0.2, -0.4], [-0.7, 0.3]])
# One discriminator update; detach prevents G from receiving gradients.
fake = G(z)
d_loss = loss_fn(D(real), torch.ones(2, 1)) + \
loss_fn(D(fake.detach()), torch.zeros(2, 1))
d_opt.zero_grad(); d_loss.backward(); d_opt.step()
# One non-saturating generator update through the discriminator.
g_loss = loss_fn(D(G(z)), torch.ones(2, 1))
g_opt.zero_grad(); g_loss.backward(); g_opt.step()
# Exact summed probability-space arithmetic from the worked example.
d_example = -(torch.log(torch.tensor(0.9)) +
torch.log(torch.tensor(1.0 - 0.2)))
g_example = -torch.log(torch.tensor(0.2))
print(f"example D loss: {d_example:.3f}")
print(f"example G loss: {g_example:.3f}")Example output (precomputed; this website does not execute code):
example D loss: 0.329
example G loss: 1.609Discriminator and generator losses
For and , calculate the summed discriminator loss and the non-saturating generator loss.
1.The real-data term is .
2.The fake-data term is .
3.Summing them gives .
The generator tries to make fake samples look real: .
A confident discriminator creates a large generator loss; alternating updates are needed because each player's target changes with the other player.
7. The LLM Training Lifecycle
Pretraining teaches a decoder-only language model to predict the next token from preceding tokens. This broad objective learns reusable linguistic and factual patterns, but it does not by itself teach the model to follow user instructions. Encoder models such as BERT instead use masked-token objectives, so not every Transformer is pretrained with next-token prediction.
Supervised fine-tuning (SFT) trains on curated prompt-response examples. LoRA and related parameter-efficient methods freeze most base weights and learn small adapters; they often reduce trainable parameters and memory, but the exact cost reduction depends on model, optimizer, hardware, and deployment choices.
Preference tuning targets behavior beyond imitation. RLHF can fit a reward model to human comparisons and optimize a policy against it, commonly with a reference-model constraint. Direct Preference Optimization (DPO) uses chosen/rejected pairs to optimize a closed-form classification-style objective relative to a reference policy. Neither method guarantees truthfulness or safety; data quality, evaluation, system design, and deployment controls still matter.
8. Application Systems: RAG, Multimodality, and Tool Use
Retrieval-Augmented Generation (RAG) retrieves candidate passages, places selected evidence in the model context, and asks the model to answer from that evidence. It can improve grounding and update knowledge without changing model weights, but it does not guarantee factuality: retrieval can miss, rank irrelevant material, expose conflicting sources, or be ignored or misinterpreted by the generator. Production systems therefore evaluate retrieval and answer attribution separately.
Multimodal systems map text, images, audio, video, or other signals into representations a shared model can process. Designs may use modality-specific encoders, cross-attention, a unified token space, or combinations of these. The durable idea is learned communication between modalities, not any particular product version.
A tool-using agent lets a model choose structured actions such as search, calculation, code execution, or database queries, observe results, and continue. The surrounding application owns permissions, validation, stopping conditions, and audit trails. More autonomous loops create more opportunities for compounding error, so constrained tools, explicit budgets, and human approval for consequential actions are architectural requirements rather than optional polish.
Interactive Practice Quiz
Test your understanding with instant feedback
What problem did Bahdanau attention address in early encoder-decoder RNNs?
Which statement fairly compares Transformers with RNNs?
If , what is the shape of ?
Why is softmax applied row-wise to attention scores?
What does a causal attention mask do before softmax?
What happens after parallel attention heads produce their outputs?
With , , and , what is the VAE sample ?
What are the two main terms in the VAE loss?
Why detach during a GAN discriminator update?
What is GAN mode collapse?
Which family directly learns a smooth probabilistic latent code useful for interpolation?
Which statement about RAG is accurate?
Further Readings
Explore these highly recommended external references to deepen your understanding
Sequence to Sequence Learning with Neural Networks
https://arxiv.org/abs/1409.3215
Neural Machine Translation by Jointly Learning to Align and Translate
https://arxiv.org/abs/1409.0473
Effective Approaches to Attention-based Neural Machine Translation
https://arxiv.org/abs/1508.04025
Attention Is All You Need
https://arxiv.org/abs/1706.03762
Improving Language Understanding by Generative Pre-Training
https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf
BERT: Pre-training of Deep Bidirectional Transformers
https://arxiv.org/abs/1810.04805
Auto-Encoding Variational Bayes
https://arxiv.org/abs/1312.6114
Generative Adversarial Nets
https://arxiv.org/abs/1406.2661
Retrieval-Augmented Generation for Knowledge-Intensive NLP
https://arxiv.org/abs/2005.11401
LoRA: Low-Rank Adaptation of Large Language Models
https://arxiv.org/abs/2106.09685
Training Language Models to Follow Instructions with Human Feedback
https://arxiv.org/abs/2203.02155
Direct Preference Optimization
https://arxiv.org/abs/2305.18290
