Generative AI Lab

Eight generative-AI labs: architectures, text, image, video, 3D, synthetic data, multimodal, and training and evaluation, with simulators that run in your browser, a learning path and a specialised assistant.

13interactive experiments

Text Generation

From tokens and transformers to decoding strategies and alignment: how a language model generates text.

  • 6Lessons
  • 3Experiments
  • 3Models
  • 2Projects

Every simulator in this lab is educational: some train small real models on 2-D data in your browser, others use simplified mathematics or locally generated data. What you see is not the output of a large real generative model and does not represent its performance; each experiment states its limits.

Scientific introduction

Large language models treat text as tokens produced by a tokenisation algorithm such as byte-pair encoding (BPE), and estimate the next-token distribution p(x_t|x_{<t}) with a Transformer. Generation behaviour after training is set by the decoding strategy: greedy, beam search, and sampling with temperature, top-k or top-p (nucleus). Alignment with instructions and human feedback is then applied to improve helpfulness and safety.

The lab explains these stages and lets you try tokenisation and decoding on a tiny educational n-gram model, and attention maps on synthetic data. These simulators do not represent the behaviour of a real language model trained on massive text.

Learning objectives

  • Understand BPE tokenisation and its effect on Arabic and English
  • Explain self-attention and the causal mask
  • Compare decoding strategies and the effect of temperature, top-k and top-p
  • Understand the pipeline: pre-training, instruction tuning, alignment
  • Recognise hallucination and the limits of language models

Core concepts

Token
A text unit that may be a word, a subword or a byte; lengths differ between languages.
Temperature
Dividing logits by T before softmax: T<1 sharpens the distribution, T>1 flattens it.
Top-p (nucleus)
Take the smallest set of tokens whose probability mass exceeds p, renormalise and sample from it.
Hallucination
Plausible-looking text not supported by facts; it arises because the objective is likelihood, not truth.
Alignment
Adjusting model behaviour with instructions and human feedback to match user intent.

Lessons, architectures and algorithms

Algorithm

Byte-Pair Encoding (BPE) Tokenisation

BPE builds a vocabulary of subword units by repeatedly merging the most frequent pair, so rare words are represented by smaller pieces without “unknown” tokens.…

Read the lesson
Algorithm

Decoding Strategies

After computing the next-token distribution we need a rule to pick the token: greedy takes the most likely, beam search tracks several sequences, and sampling with temperature, top-k or top-…

Read the lesson
Practice

Instruction Tuning and Alignment

A pre-trained model predicts the next text and does not necessarily follow instructions. Helpfulness and safety are improved by tuning on instruction examples and then training on human pref…

Read the lesson
Simulator

Attention visualiser

Deterministic embeddings from a token hash with sinusoidal positions and fixed random projections.…

Read the lesson
Simulator

Decoding lab

An n-gram model built from a few sentences; it is not a real language model.…

Read the lesson
Simulator

BPE tokeniser

The original character-level BPE algorithm.…

Read the lesson

Algorithms and models in this lab

AlgorithmLabCovered hereNote
TransformerText GenerationComputed in the browserAttention maps on synthetic data.
BPEText GenerationComputed in the browserTraining and tokenising in the browser.
Decoding (greedy/beam/sampling)Text GenerationComputed in the browserA tiny n-gram model.
RLHFText GenerationExplained onlyExplained only: alignment.

The mathematics

Softmax with temperature

p_i=\frac{\exp(z_i/T)}{\sum_j \exp(z_j/T)}

As T→0 it approaches greedy decoding; as T→∞ it approaches the uniform distribution.

Scaled dot-product attention

\mathrm{Attention}(Q,K,V)=\mathrm{softmax}\!\left(\frac{QK^{\top}}{\sqrt{d_k}}\right)V

Dividing by √d_k prevents softmax saturation at large dimensions (Vaswani et al. 2017).

Language-modelling loss

\mathcal{L}=-\frac1T\sum_{t=1}^{T}\log p_\theta(x_t\mid x_{<t})

Perplexity is exp(L).

Diagrams

Worked examples

  • Train BPE on a short text and see how unseen words are split.
  • Compare greedy and top-p on the same prompt and see the diversity.
  • Switch the causal mask on and watch attention to the future disappear.

Interactive experiments

Suitable datasets

The whole data library

Related models

The full model atlas

Applied projects

Assessment questions

10 questions on this lab, graded on the server. Start the quiz

Papers and references

  1. A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, L. Kaiser, I. Polosukhin (2017). Attention Is All You Need. Advances in Neural Information Processing Systems 30 (NeurIPS 2017). doi:10.48550/arXiv.1706.03762
  2. R. Sennrich, B. Haddow, A. Birch (2016). Neural Machine Translation of Rare Words with Subword Units. Proceedings of the 54th Annual Meeting of the ACL (Volume 1: Long Papers), pp. 1715–1725. doi:10.18653/v1/P16-1162
  3. A. Holtzman, J. Buys, L. Du, M. Forbes, Y. Choi (2020). The Curious Case of Neural Text Degeneration. International Conference on Learning Representations (ICLR 2020). doi:10.48550/arXiv.1904.09751
  4. T. B. Brown, B. Mann, N. Ryder, et al. (2020). Language Models are Few-Shot Learners. Advances in Neural Information Processing Systems 33 (NeurIPS 2020). doi:10.48550/arXiv.2005.14165
  5. L. Ouyang, J. Wu, X. Jiang, et al. (2022). Training language models to follow instructions with human feedback. arXiv:2203.02155 (NeurIPS 2022). doi:10.48550/arXiv.2203.02155

Content-specific assistant

Ask the assistant about any concept or equation in this lab; it answers from the lab’s content and points you to the matching lesson and experiment.

Open the assistant

Quizzes · My progress and saved runs

Related DLArabic sections