A deep understanding of AI large language model mechanisms

Build and train LLM NLP transformers and attention mechanisms (PyTorch). Explore with mechanistic interpretability tools

  • 0.0 (0 Reviews)
  • 0 Students
  • 329 Lessons
  • Mike X Cohen

Course Overview

Deep Understanding of Large Language Models (LLMs): Architecture, Training, and Mechanisms


Description

Large Language Models (LLMs) like ChatGPT, GPT-4, , GPT5, Claude, Gemini, and LLaMA are transforming artificial intelligence, natural language processing (NLP), and machine learning. But most courses only teach you how to use LLMs. This 90+ hour intensive course teaches you how they actually work — and how to dissect them using machine-learning and mechanistic interpretability methods.

This is a deep, end-to-end exploration of transformer architectures, self-attention mechanisms, embeddings layers, training pipelines, and inference strategies — with hands-on Python and PyTorch code at every step.

Whether your goal is to build your own transformer from scratch, fine-tune existing models, or understand the mathematics and engineering behind state-of-the-art generative AI, this course will give you the foundation and tools you need.


What You’ll Learn

  • The complete architecture of LLMs — tokenization, embeddings, encoders, decoders, attention heads, feedforward networks, and layer normalization

  • Mathematics of attention mechanisms — dot-product attention, multi-head attention, positional encoding, causal masking, probabilistic token selection

  • Training LLMs — optimization (Adam, AdamW), loss functions, gradient accumulation, batch processing, learning-rate schedulers, regularization (L1, L2, decorrelation), gradient clipping

  • Fine-tuning and prompt engineering for downstream NLP tasks, system-tuning

  • Evaluation metrics — perplexity, accuracy, and benchmark datasets such as MAUVE, HellaSwag, SuperGLUE, and ways to assess bias and fairness

  • Practical PyTorch implementations of transformers, attention layers, and language model training loops, custom classes, custom loss functions

  • Inference techniques — greedy decoding, beam search, top-k sampling, temperature scaling

  • Scaling laws and trade-offs between model size, training data, and performance

  • Limitations and biases in LLMs — interpretability, ethical considerations, and responsible AI

  • Decoder-only transformers

  • Embeddings, including token embeddings and positional embeddings

  • Sampling techniques — methods for generating new text, including top-p, top-k, multinomial, and greedy


Why This Course Is Different

  • 93+ hours of HD video lectures — blending theory, code, and practical application

  • Code challenges in every section — with full, downloadable solutions

  • Builds from first principles — starting from basic Python/Numpy implementations and progressing to full PyTorch LLMs

  • Suitable for researchers, engineers, and advanced learners who want to go beyond “black box” API usage

  • Clear explanations without dumbing down the content — intensive but approachable

Who Is This Course For?

  • Machine learning engineers and data scientists

  • AI researchers and NLP specialists

  • Software developers interested in deep learning and generative AI

  • Graduate students or self-learners with intermediate Python skills and basic ML knowledge

Technologies & Tools Covered

  • Python and PyTorch for deep learning

  • NumPy and Matplotlib for numerical computing and visualization

  • Google Colab for free GPU access

  • Hugging Face Transformers for working with pre-trained models

  • Tokenizers and text preprocessing tools

  • Implement Transformers in PyTorch, fine-tune LLMs, decode with attention mechanisms, and probe model internals

What if you have questions about the material?

This course has a Q&A (question and answer) section where you can post your questions about the course material (about the maths, statistics, coding, or machine learning aspects). I try to answer all questions within a day. You can also see all other questions and answers, which really improves how much you can learn! And you can contribute to the Q&A by posting to ongoing discussions.


By the end of this course, you won’t just know how to work with LLMs — you’ll understand why they work the way they do, and be able to design, train, evaluate, and deploy your own transformer-based language models.

Enroll now and start mastering Large Language Models from the ground up.

See more

FAQ

FAQ area empty

Course curriculum

40 chương 329 bài học 90 giờ 60 phút 7 bài học thử miễn phí
Chương 1 Introductions
Chương 2 —— ** —— Part 1: Tokenizations and embeddings —— ** ——
Chương 3 Words to tokens to numbers
  • Why text needs to be numbered 24:37
  • Parsing text to numbered tokens 13:35
  • CodeChallenge: Create and visualize tokens (part 1) 16:41
  • CodeChallenge: Create and visualize tokens (part 2) Học thử 9:01
  • Preparing text for tokenization 16:59
  • CodeChallenge: Tokenizing The Time Machine 23:35
  • Tokenizing characters vs. subwords vs. words 4:52
  • Byte-pair encoding algorithm 27:53
  • CodeChallenge: Byte-pair encoding to a desired vocab size 7:24
  • Exploring ChatGPT4's tokenizer 26:11
  • CodeChallenge: Token count by subword length (part 1) 22:05
  • CodeChallenge: Token count by subword length (part 2) 10:13
  • How many "r"s in strawberry? 5:43
  • CodeChallenge: Create your algorithmic rapper name :) 6:31
  • Tokenization in BERT 18:32
  • CodeChallenge: Character counts in BERT tokens 5:41
  • Translating between tokenizers 13:48
  • CodeChallenge: More on token translation 7:25
  • CodeChallenge: Tokenization compression ratios 15:22
  • Tokenization in different languages 8:25
  • CodeChallenge: Zipf's law in characters and tokens 12:34
  • Word variations in Claude tokenizer 13:34
Chương 4 Embeddings spaces
  • Word2Vec vs. GloVe vs. GPT vs. BERT... oh my! 10:28
  • Exploring GloVe pretrained embeddings 26:24
  • CodeChallenge: Wikipedia vs. Twitter embeddings (part 1) 18:35
  • CodeChallenge: Wikipedia vs. Twitter embeddings (part 2) 14:34
  • Exploring GPT2 and BERT embeddings 22:37
  • CodeChallenge: Math with tokens and embeddings 28:59
  • Cosine similarity (and relation to correlation) 26:12
  • CodeChallenge: GPT2 cosine similarities 21:52
  • CodeChallenge: Unembeddings (vectors to tokens) 32:57
  • Position embeddings 29:33
  • CodeChallenge: Exploring position embeddings 22:43
  • Training embeddings from scratch 3:11
  • Create a data loader to train a model 25:41
  • Build a model to learn the embeddings 30:35
  • Loss function to train the embeddings 29:18
  • Train and evaluate the model 15:01
  • CodeChallenge: How the embeddings change 18:29
  • CodeChallenge: How stable are embeddings? 17:40
Chương 5 —— ** —— Part 2: Large language models —— ** ——
  • Large language models
Chương 6 Build a GPT
  • Why build when you can download? 4:19
  • Model 1: Embedding (input) and unembedding (output) 30:16
  • Understanding nn.Embedding and nn.Linear 12:45
  • CodeChallenge: GELU vs. ReLU 26:50
  • Softmax (and temperature): math, numpy, and pytorch 21:42
  • Randomly sampling words with torch.multinomial 17:18
  • Other token sampling methods: greedy, top-k, and top-p 5:35
  • CodeChallenge: More softmax explorations 25:13
  • What, why, when, and how to layernorm 18:36
  • Model 2: Position embedding, layernorm, tied output, temperature 26:27
  • Temporal causality via linear algebra (theory) 13:46
  • Averaging the past while ignoring the future (code) 17:41
  • The "attention" algorithm (theory) 10:39
  • CodeChallenge: Code Attention manually and in Pytorch 21:55
  • Model 3: One attention head 22:52
  • The Transformer block (theory) 8:35
  • The Transformer block (code) 16:34
  • Model 4: Multiple Transformer blocks 20:28
  • Multihead attention: theory and implementation 23:58
  • Working on the GPU 21:00
  • Model 5: Complete GPT2 on the GPU 18:25
  • CodeChallenge: Time model5 on CPU and GPU 12:15
  • Inspecting OpenAI's GPT2 12:07
  • Summarizing GPT using equations 18:32
  • Visualizing nano-GPT 6:17
  • CodeChallenge: How many parameters? (part 1) 19:14
  • CodeChallenge: How many parameters? (part 2) 10:18
  • CodeChallenge: GPT2 trained weights distributions 17:55
  • CodeChallenge: Do we really need Q? 22:03
Chương 7 Pretrain LLMs
  • What is "pretraining" and is it necessary? 12:32
  • Introducing huggingface.co 6:29
  • The AdamW optimizer 12:10
  • CodeChallenge: SGD vs. Adam vs. AdamW 26:31
  • Train model 1 22:11
  • CodeChallenge: Add a test set 20:24
  • CodeChallenge: Train model 1 with GPT2's embeddings 10:15
  • CodeChallenge: Train model 5 with modifications 15:41
  • Create a custom loss function 13:33
  • CodeChallenge: Train a model to like "X" 23:56
  • CodeChallenge: Numerical scaling issues in DL models 22:12
  • Weight initializations 10:30
  • CodeChallenge: Train model 5 with weight inits 23:15
  • Dropout in theory and in Pytorch 21:19
  • Should you output logits or log-softmax(logits)? 4:03
  • The FineWeb dataset 6:21
  • CodeChallenge: Fine dropout in model 5 (part 1) 17:21
  • CodeChallenge: Fine dropout in model 5 (part 2) 13:33
  • CodeChallenge: What happens to unused tokens? 25:39
  • Optimization options 4:32
Chương 8 Fine-tune pretrained models
  • What does "fine-tuning" mean? 4:49
  • Fine-tune a pretrained GPT2 22:54
  • CodeChallenge: Gulliver's learning rates 14:56
  • On generating text from pretrained models 14:21
  • CodeChallenge: Maximize the "X" factor 23:00
  • Alice in Wonderland and Edgar Allen Poe (with GPT-neo) 19:53
  • CodeChallenge: Quantify the Alice/Edgar fine-tuning 20:55
  • CodeChallenge: A chat between Alice and Edgar 10:20
  • Partial fine-tuning by freezing attention weights 15:15
  • CodeChallenge: Fine-tuning and targeted freezing (part 1) 21:59
  • CodeChallenge: Fine-tuning and targeted freezing (part 2) 14:03
  • Parameter-efficient fine-tuning (PEFT) 9:51
  • CodeGen for code completion 13:37
  • CodeChallenge: Fine-tune codeGen for calculus 7:00
  • Fine-tuning BERT for classification 31:18
  • CodeChallenge: IMDB sentiment analysis using BERT 16:08
  • Gradient clipping and learning rate scheduler (part 1) 18:55
  • Gradient clipping and learning rate scheduler (part 2) 11:52
  • CodeChallenge: Clip, freeze, and schedule BERT 18:11
  • Saving and loading trained models 13:45
  • BERT decides: Alice or Edgar? 14:38
  • CodeChallenge: Evolution of Alice and Edgar (part 1) 19:38
  • CodeChallenge: Evolution of Alice and Edgar (part 2) 9:20
  • Why fine-tune when you can use AGI? 7:07
Chương 9 Instruction tuning
  • What is instruction tuning? 2:56
  • Some datasets for instruction tuning 12:57
  • Training a chatbot with system-user-assistant 13:46
  • Instruction tuning with GPT2 18:18
  • CodeChallenge: Instruction tuning GPT2-large (part 1) 23:10
  • CodeChallenge: Instruction tuning GPT2-large (part 2) 13:31
  • Reinforcement learning from human feedback (RLHF) 10:13
Chương 10 —— ** —— Part 3: Evaluating LLMs —— ** ——
  • Evaluating LLMs
Chương 11 Quantitative evaluations
  • Promises and challenges of quantitative evaluations 14:40
  • Numerical issues in logits and softmax 14:55
  • Perplexity 19:52
  • CodeChallenge: Perplexing perplexities 24:52
  • Masked word prediction accuracy 11:36
  • HellaSwag 18:03
  • Import large models using bitsandbytes 6:14
  • CodeChallenge: HellaSwag evals in two models (part 1) 18:21
  • CodeChallenge: HellaSwag evals in two models (part 2) 11:19
  • KL (Kullback-Leibler) divergence 16:44
  • MAUVE 18:48
  • CodeChallenge: Large and small MAUVE explorations 19:26
  • SuperGLUE and other amalgamations 4:31
  • Assessing bias and fairness 7:11
  • Non-technical benchmarks 4:33
Chương 12 Qualitative evaluations
  • Black box evals 12:52
  • Red-teaming 4:43
  • Accuracy, coherence, and relevance 5:55
  • Distributions of hidden-state activations 28:14
  • Heatmaps of tokens for qualitative inspection 8:27
  • CodeChallenge: Visualize single-token predictions 23:50
Chương 13 —— ** —— Part 4: AI safety and mechanistic interpretability —— ** ——
  • Overview of AI safety and mechanistic interpretability
Chương 14 AI safety
  • AI safety and alignment 14:32
  • Why can't AI just be safe and moral? 8:12
  • In-context and few-shot learning 5:54
  • Scaling and AI safety 18:02
  • Hands-on: Hack an AI to steal a password! 1:26
  • How to get involved in AI safety 4:26
Chương 15 Interpretability
  • What is "mech interp" (mechanistic interpretability)? 14:23
  • How does mech interp relate to AI safety? 7:59
  • Concepts and terms in mech interp 12:44
  • Theoretical and empirical approaches in research and teaching 11:58
  • General criticisms of mechanistic interpretability 9:03
Chương 16 —— ** —— Part 5: Observation (non-causal) mech interp —— ** ——
  • Observation (non-causal) mechanistic interpretability
Chương 17 Investigating token embeddings (part 1)
  • CodeChallenge: Cosine similarity (advanced) (part 1) 13:19
  • CodeChallenge: Cosine similarity (advanced) (part 2) 15:22
  • CodeChallenge: Cosine similarity in word sequences 13:08
  • CodeChallenge: Coloring cosine similarity 24:39
  • CodeChallenge: Can random embeddings be interpreted? 12:59
  • T-SNE projection and DBSCAN clustering (theory) 17:25
  • T-SNE projection and DBSCAN clustering (Python) 15:25
  • CodeChallenge: cluster the "x" terms 24:06
  • CodeChallenge: Tokenize, embed, and cluster happy emojis 25:56
  • RSA (representational similarity analysis) 19:12
  • CodeChallenge: Compare embeddings with RSA (part 1) 19:16
  • CodeChallenge: Compare embeddings with RSA (part 2) 11:39
  • CodeChallenge: Word2vec vs. GPT2 20:30
  • CodeChallenge: Graph representation of cosine similarities 23:55
  • Embeddings arithmetic and analogies 16:41
  • CodeChallenge: soft-coded analogies in word2vec 13:55
  • Creating and interpreting linear "semantic axes" 14:38
  • KNN for synonym-searching in BERT 25:31
  • CodeChallenge: BERT v GPT kNN kompetition 17:58
  • Research on translating embeddings spaces 3:33
  • Singular value spectrum of embeddings submatrices 29:04
  • CodeChallenge: SVD projections of related embeddings 23:34
Chương 18 Investigating neurons and dimensions
  • Activation maximization via gradient ascent (theory) 10:19
  • Activation maximization (code) 27:11
  • Activation maximization via data sampling 18:31
  • CodeChallenge: Reproducibility of activation maximization 13:31
  • Extracting activations using "hooks" 25:41
  • Relation between hooks and output.hidden_states 14:46
  • Clarification of final hidden_states output 8:43
  • CodeChallenge: Grammar tuning in MLP neurons? (part 1) 19:06
  • CodeChallenge: Grammar tuning in MLP neurons? (part 2) 15:07
  • CodeChallenge: Context-modulated activation in MLP 19:31
  • CodeChallenge: Activation histograms by token length (part 1) 20:28
  • CodeChallenge: Activation histograms by token length (part 2) 19:29
  • CodeChallenge: Activation histograms by token length (part 3) 18:21
  • Dealing with multitoken word embeddings 13:42
  • CodeChallenge: Category-tuned MLP projections (part 1) 28:36
  • CodeChallenge: Category-tuned MLP projections (part 2) 26:54
  • Classification via logistic regression: theory and code 23:02
  • Logistic regression vs. t-test: assumptions and applications 20:04
  • Proper noun tuning in GPT2-medium 28:06
  • CodeChallenge: Negation tuning in MLP neurons (part 1) 15:16
  • CodeChallenge: Negation tuning in MLP neurons (part 2) 17:47
  • CodeChallenge: Negation tuning in MLP neurons (part 3) 17:45
  • CodeChallenge: Negation tuning in QVK neurons 11:55
Chương 19 Investigating layers
  • Token-related similarities within and across Q, K, V matrices (part 1) 18:23
  • Token-related similarities within and across Q, K, V matrices (part 2) 19:06
  • CodeChallenge: Token-related similarities across layers 28:53
  • Grouping and RSA in Q and K matrices 20:35
  • CodeChallenge: Laminar profile of RSA and category selectivity 10:04
  • "Effective dimensionality" analysis with PCA 30:43
  • CodeChallenge: Dimensionalities in Pythia 2.3B 19:42
  • Mutual information: theory and code 29:21
  • Pairwise mutual information through the LLM 17:30
  • Mutual information vs. covariance 22:08
  • CodeChallenge: Attention to coffee: MI and token distances (part 1) 32:43
  • CodeChallenge: Attention to coffee: MI and token distances (part 2) 23:35
  • CodeChallenge: Clusters in internal vs. terminal punctuation (part 1) 30:14
  • CodeChallenge: Clusters in internal vs. terminal punctuation (part 2) 17:34
  • The Logit Lens 20:42
  • CodeChallenge: Logit Lens in BERT (part 1) 19:16
  • CodeChallenge: Logit Lens in BERT (part 2) 23:12
Chương 20 Investigating token embeddings (part 2)
  • Calculating rotations of embeddings vectors 27:26
  • CodeChallenge: Laminar evolution of sequential angular adjustments 16:45
  • Path length and logit token prediction 20:13
  • CodeChallenge: Residual stream path length decomposition (part 1) 12:38
  • CodeChallenge: Residual stream path length decomposition (part 2) 22:57
  • State-space trajectories through embedding space 30:42
  • Parts of speech with SpaCy library 15:53
  • CodeChallenge: Do nouns or adjectives have longer trajectories? (part 1) 25:20
  • CodeChallenge: Do nouns or adjectives have longer trajectories? (part 2) 22:19
Chương 21 Identifying circuits and components
  • What is a "circuit" in a DL model? 8:26
  • Isolating and investigating attention heads 42:55
  • CodeChallenge: Laminar profile of attention head weights 25:33
  • Are circuits clustered in low-dimensional space? 30:19
  • Sparse probing: theory and code 35:05
  • Challenges with sparse logistic regression in large datasets 23:02
  • "Latent" vs. "manifest" variables 9:59
  • Sparse autoencoders: theory and code 35:06
  • SAE in GPT2 learns about Hungarian Palinka 34:25
  • CodeChallenge: Laminar profile of autoencoder sparsity 33:19
  • Non-orthogonal latent components via eigendecomposition (theory and demo) 35:53
  • Generalized eigendecomposition separates "him" from "her" in MLP 33:41
  • CodeChallenge: GED for category isolation across layers (part 1) 26:19
  • CodeChallenge: GED for category isolation across layers (part 2) 27:40
Chương 22 —— ** —— Part 6: Intervention (causal) mech interp —— ** ——
  • Intervention (causal) mech interp
Chương 23 How to modify activations
  • Introduction to causal mech interp 9:28
  • Activation editing: Code implementations 22:21
  • CodeChallenge: replacing attention, MLP, and hidden states 30:20
Chương 24 Editing hidden states
  • Downstream impact of early layer scaling 18:44
  • CodeChallenge: Hidden-state scaling and token loss 21:35
  • CodeChallenge: Noisy and shuffled BERT predictions 31:09
  • CodeChallenge: Measure and correct BERT's bias 32:24
  • Activation patching with indirect object identification 25:22
  • Skip a layer 4:44
Chương 25 Interfering with attention
  • Head ablation and token prediction 18:33
  • CodeChallenge: Token prediction after head ablations (part 1) 27:57
  • CodeChallenge: Token prediction after head ablations (part 2) 17:14
  • Impact of head-silencing on cosine similarity 23:50
  • CodeChallenge: Does GPT2 like pineapple pizza? 30:05
  • Attention head patching in IOI 22:32
  • CodeChallenge: Head and token patching in IOI 15:13
Chương 26 Modifying MLP
  • Successive median-replacement of MLP neurons 24:59
  • Statistics-based lesioning MLP neurons 33:23
  • CodeChallenge: Laminar profile of MLP t-lesions 26:07
  • Explorations in subspace removal 37:36
Chương 27 —— ** —— Part 7: Python tutorial —— ** ——
  • Python tutorial
Chương 28 Python intro: Colab and notebooks
  • Creating, working with, and saving Colab notebooks 15:30
Chương 29 Python intro: Data types
  • Arithmetic and comments 9:32
  • Variables 22:55
  • Lists 8:14
  • Booleans 10:06
  • Dictionaries 8:45
Chương 30 Python intro: Indexing and slicing
  • Indexing 13:11
  • Slicing 9:32
Chương 31 Python intro: Functions
  • Inputs and outputs 10:13
  • The numpy library 10:34
  • Getting help on functions 8:05
  • Creating functions 20:07
  • Copying (duplicating) variables 9:17
  • Generating random numbers 9:33
Chương 32 Python intro: Flow control
  • For loops 14:00
  • If-else statements 9:54
  • List comprehension (single-line loops) 12:55
  • Initializing variables 11:18
  • Enumerate iterables 8:21
  • Zip multiple iterables 7:41
Chương 33 Python intro: Data visualization
  • Plotting dots and lines 14:39
  • Subplot geometry 9:56
  • Making graphs look nice 11:52
Chương 34 Python intro: Strings and texts
  • String interpolation and f-strings 9:47
  • Importing text from the web 6:32
  • Processing text 6:24
Chương 35 Python intro: Pytorch
  • Working with classes 5:00
  • Creating custom classes 12:49
  • Datatypes, tensors, and dimensions 12:00
  • Reshaping tensors 14:08
  • Random numbers 8:16
Chương 36 —— ** —— Part 8: Deep learning intro —— ** ——
  • Deep learning intro
Chương 37 Math of deep learning
  • Terms and datatypes in math and computers 6:01
  • Vector and matrix transpose 6:58
  • Linear weighted combinations 18:21
  • The dot product 14:23
  • Matrix multiplication 22:03
  • Softmax 19:26
  • Logarithms 8:26
  • Entropy and cross-entropy 18:18
  • Min/max and argmin/argmax 12:47
  • Mean and variance 15:34
  • Random sampling and sampling variability 11:18
  • The t-test 13:57
  • Derivatives: intuition and polynomials 16:38
  • Derivatives find minima 8:32
  • Derivatives: product and chain rules 10:00
Chương 38 How models learn: gradient descent
  • Overview of gradient descent 14:15
  • What about local minima? 11:45
  • Gradient descent in 1D 17:11
  • Gradient descent in 2D 14:48
  • CodeChallenge: fixed vs. dynamic learning rate 15:33
Chương 39 Essence of deep learning modeling
  • The perceptron and ANN architecture 19:50
  • A geometric view of ANNs 13:38
  • ANN math part 1 (forward prop) 16:22
  • ANN math part 2 (errors, loss, cost) 10:54
  • ANN math part 3 (backprop) 12:10
  • Forward pass in Pytorch 25:24
  • Backprop in Pytorch 17:45
Chương 40 Bonus content
  • Bonus material

Requirment

  • Motivation to learn about large language models and AI

  • Experience with coding is helpful but not necessary

  • Familiarity with machine learning is helpful but not necessary

  • Basic linear algebra is helpful

  • Deep learning, including gradient descent, is helpful but not necessary

Outcomes

  • Large language model (LLM) architectures, including GPT (OpenAI) and BERT

  • Transformer blocks

  • Attention algorithm

  • Pytorch

  • LLM pretraining

  • Explainable AI

  • Mechanistic interpretability

  • Machine learning

  • Deep learning

  • Principal components analysis

  • High-dimensional clustering

  • Dimension reduction

  • Advanced cosine similarity applications

Instructor

...
Mike X Cohen

0.0

  • ... 0 Student
  • ... 1 Course
  • ... 0 review

View Details

Reviews

Rate this course :

Remove all