Intermediate

Transformers and LLMs

The fundamentals of transformer architectures and how to work with large language models.

GitHub: alpertellioglu/transformers-and-llms


Table of Contents

  1. Module 1 — Fundamentals of Transformers and LLMs
  2. Module 2 — Working with Transformer Models

Module 1 — Fundamentals of Transformers and LLMs

1.1 Introduction to LLMs

Large language models (LLMs) are ubiquitous today. Tools like ChatGPT, Gemini, or Claude are already transforming countless industries. But how exactly do they work?

Real-World LLM Applications

┌─────────────────────────────────────────────────────────────────┐
│               Real-World LLM Applications                       │
├────────────────┬───────────────────┬──────────────┬─────────────┤
│   Chatbots     │ Code Generation   │  Content     │ Brainstorm  │
│                │                   │  Creation    │             │
├────────────────┴───────────────────┴──────────────┴─────────────┤
│  GitHub Copilot · Cursor · ChatGPT · Gemini · Claude            │
└─────────────────────────────────────────────────────────────────┘

What LLMs Do (and Don’t Do)

An LLM is a statistical model: it predicts the next word in a sequence from a vocabulary. It is not (yet) a general intelligence capable of thinking creatively like a human.

However, reasoning models (e.g., OpenAI’s o1) show that iterative next-token prediction can lead to genuine reasoning.

The Debate in the AI Community

ThinkerPosition
Ilya Sutskever (former OpenAI)Predicting the next word is sufficient for real understanding. E.g.: predicting the culprit in a detective novel requires understanding the entire novel.
Yann LeCun (Meta)LLMs cannot reach AGI without real-world understanding (physics, touch, etc.).

1.2 NLP Evolution: From RNNs to Transformers

RNNs (Recurrent Neural Networks)

RNNs process words one at a time, in order, maintaining a hidden state.

┌───────────────────────────────────────────────────┐
│                  RNN Architecture                 │
│                                                   │
│    y1      y2      y3     ...     yn              │
│    ↑       ↑       ↑              ↑               │
│   [H1] → [H2] → [H3] → ... → [HN]               │
│    ↑       ↑       ↑              ↑               │
│    x1      x2      x3     ...     xn              │
└───────────────────────────────────────────────────┘

RNN problems:

  • Sequential bottleneck — impossible to train in parallel
  • Slow and inefficient
  • Difficulty retaining context over long sequences (vanishing gradient problem)

LSTMs (Long Short-Term Memory)

LSTMs introduce memory gates to decide what to retain and what to forget.

LSTM limitations:

  • Still sequential — still slow to train
  • Long-range dependency problems
  • Don’t scale well for large datasets

Transformers — The Paradigm Shift

In 2017, the landmark paper “Attention Is All You Need” introduced the Transformer architecture.

graph LR
    A[RNNs<br/>Read language] --> B[LSTMs<br/>Remember it better]
    B --> C[Transformers<br/>Understand it]
    
    style A fill:#ffcccc
    style B fill:#ffffcc
    style C fill:#ccffcc

Key Transformer advantages:

CharacteristicRNN/LSTMTransformer
ProcessingSequentialParallel
MemoryFixed, can degradeDynamic (attention)
Long dependenciesDifficultDirect
ScalabilityLimitedExcellent
Model typeTask-specificFoundation models

Attention example:

“Alice gave her dog a bath after it rolled in the mud.”
To resolve the reference of “it”, the Transformer assigns a higher attention score to “dog” than to “mud” when processing “it”.

Attention scores for "it":
  Alice   →  0.02
  gave    →  0.05
  her     →  0.08
  dog     →  0.71  ← strong attention
  bath    →  0.03
  rolled  →  0.06
  mud     →  0.05

1.3 Transformer Architecture

Complete Architecture Overview

graph TB
    subgraph ENCODER["Encoder (e.g., BERT)"]
        I[Inputs] --> IE[Input Embedding]
        IE --> PE[Positional Encoding]
        PE --> MHA[Multi-Head Attention]
        MHA --> AN1[Add & Norm]
        AN1 --> FFN[Feed Forward Network]
        FFN --> AN2[Add & Norm]
        AN2 --> MHA
    end
    
    subgraph DECODER["Decoder (e.g., GPT)"]
        TI[Target Inputs] --> TIE[Target Embedding]
        TIE --> TPE[Positional Encoding]
        TPE --> MMHA[Masked Multi-Head Attention]
        MMHA --> AN3[Add & Norm]
        AN3 --> CA[Cross-Attention]
        CA --> AN4[Add & Norm]
        AN4 --> FFN2[Feed Forward Network]
        FFN2 --> AN5[Add & Norm]
        AN5 --> Lin[Linear]
        Lin --> Smax[Softmax]
        Smax --> OP[Output Probabilities]
    end
    
    AN2 --> CA
    
    style ENCODER fill:#e8f4f8
    style DECODER fill:#f8f0e8

Step 1 — Token Embeddings and Positional Encoding

Transformers don’t process text in order — they receive all tokens simultaneously. So they need to be told the order.

Sentence: "Transformers are amazing"

Token IDs:   [4523, 123, 8901]
             ↓
Embeddings:  [v₁, v₂, v₃]   ← vectors of dimension 512
             +
Pos. Encod.: [p₁, p₂, p₃]   ← position signal

             = [v₁+p₁, v₂+p₂, v₃+p₃]  → input to first layer

Two types of positional encoding:

TypeDescriptionUsed in
Fixed sinusoidalFixed encoding based on sin/cos functionsOriginal Transformer
Learned positional embeddingsLearned during trainingBERT, GPT, most modern models

Step 2 — Multi-Head Self-Attention

This is the heart of the Transformer. Each word can “look at” all other words in the sentence.

Q, K, V Mechanism:

For each token, we project 3 vectors:

  Q (Query): "What am I looking for?"
  K (Key):   "What do I represent?"
  V (Value): "What do I offer?"

Calculation:
  Attention(Q, K, V) = softmax(QKᵀ / √d_k) · V

Mathematical formula:

$$\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V$$

Why “multi-head”?

Transformers perform attention multiple times in parallel. Each “head” focuses on something different:

┌──────────────────────────────────────────────────┐
│              Multi-Head Attention                │
│                                                  │
│   Head 1: syntax                                 │
│   Head 2: thematic flow                          │
│   Head 3: grammar                                │
│   Head 4: semantic relationships                 │
│   ...                                            │
│                                                  │
│   Concatenation → richer understanding           │
└──────────────────────────────────────────────────┘

Encoder vs Decoder

graph LR
    subgraph ENC["Encoder Architecture (BERT)"]
        E1[Bidirectional\nSelf-Attention] --> E2[FFN]
        E2 --> E3[Contextual\nRepresentation]
    end
    
    subgraph DEC["Decoder Architecture (GPT)"]
        D1[Causal Masked\nSelf-Attention] --> D2[FFN]
        D2 --> D3[Text\nGeneration]
    end
    
    subgraph ENCDEC["Encoder-Decoder (T5)"]
        T1[Encoder] --> T2[Cross-Attention]
        T2 --> T3[Decoder]
        T3 --> T4[Translation/\nSummarization]
    end

Key difference: Masked Self-Attention

In GPT (decoder only), the model can only look at tokens to the left (past), never future tokens. This is called causal masking.

Sequence: ["The", "cat", "sat", "on", "the", "mat"]

To predict "sat", GPT can see: ["The", "cat", ...]
                                 ✓       ✓     ✗ (future masked)

Main Models Comparison

┌─────────────────────────────────────────────────────────────────┐
│                    GPT         vs        BERT                   │
├─────────────────────┬───────────────────────────────────────────┤
│ Architecture        │ Decoder only         │ Encoder only       │
│ Attention           │ Causal (→ left)      │ Bidirectional (←→) │
│ Main task           │ Predicts the         │ Predicts randomly  │
│                     │ next token           │ masked tokens      │
│ Masking             │ Masks future         │ Masks input        │
│ Usage               │ Text generation      │ Classification,    │
│                     │                      │ comprehension      │
└─────────────────────┴──────────────────────┴────────────────────┘

1.4 LLM Training Process

Self-Supervised Learning

Training happens without human labels. The model creates its own learning signals from raw data.

flowchart LR
    A["Raw text\nWikipedia, books,\nReddit, articles..."] --> B[Tokenization]
    B --> C[Embeddings + Positional Encoding]
    C --> D[Prediction]
    D --> E[Loss calculation\ncross-entropy]
    E --> F[Backpropagation]
    F --> G[Weight update]
    G --> C

Self-supervised Learning Example

Original sentence: "Transformers are amazing models."

GPT mode (causal):
  Input:  ["Transformers", "are", "amazing"]
  Target: ["are", "amazing", "models."]

BERT mode (masking):
  Input:  ["Transformers", "are", "[MASK]", "models."]
  Target: [_, _, "amazing", _]

→ No human annotations needed!

Complete Training Pipeline

1. DATA COLLECTION
   Billions of tokens: Wikipedia + books + Reddit + web
   
2. TOKENIZATION
   "Transformers" → [4523, 108, ...]
   Each token gets a unique ID in the vocabulary
   
3. EMBEDDINGS + POSITIONAL ENCODING
   Token ID → numerical vector (e.g., 512 dimensions)
   + position signal
   
4. PREDICTION
   GPT  → predicts the next token
   BERT → predicts the masked tokens
   
5. LOSS CALCULATION (Cross-Entropy Loss)
   Good prediction → low loss
   Bad prediction → high loss
   
6. BACKPROPAGATION
   Loss is propagated backward to compute gradients
   All weights are updated in small steps
   
7. REPEAT over billions of tokens

Module 2 — Working with Transformer Models

2.1 Practical Use of Transformer Models

Environment Installation

pip install transformers torch

The Hugging Face Transformers library gives access to thousands of pre-trained models (BERT, GPT-2, T5, etc.) for tasks like:

  • Text generation
  • Classification
  • Translation
  • Question answering
  • Automatic summarization

Example 1 — Sentiment Analysis with BERT

import warnings
warnings.filterwarnings("ignore")

from transformers import pipeline

# Load pre-trained BERT model for sentiment analysis
classifier = pipeline("sentiment-analysis")  # Uses distilbert by default

# Classify a sentence
result = classifier("I love using transformers!")[0]
print("Sentiment:", result)
# Output: {'label': 'POSITIVE', 'score': 0.9994}

Example 2 — Text Generation with GPT-2

from transformers import pipeline

# Load GPT-2 model
generator = pipeline("text-generation", model="gpt2")

# Generate from a prompt
output = generator(
    "Once upon a time",
    truncation=True,
    max_length=30,
    do_sample=True  # adds randomness
)[0]["generated_text"]

print("\nGPT-2 Output:")
print(output)

Example 3 — Automatic Summarization with T5

from transformers import pipeline

# Load T5 model for summarization
summarizer = pipeline("summarization", model="t5-base")

text = """
The T5 model was proposed in the paper "Exploring the Limits of Transfer Learning 
with a Unified Text-to-Text Transformer". The T5 model is an encoder-decoder model 
that converts all NLP problems into a text-to-text format. It was pre-trained on a 
large dataset called the Colossal Clean Crawled Corpus (C4), which contains 750GB 
of text data.
"""

summary = summarizer(text, max_length=20, min_length=10, do_sample=False)
print(summary[0]["summary_text"])
# Output: "the T5 model converts all NLP problems into a text-to-text format"

Three Models Comparison

ModelArchitectureMain TaskMechanism
BERTEncoder onlyClassification, NER, Q&ABidirectional attention
GPT-2Decoder onlyText generationCausal attention (left only)
T5Encoder + DecoderSummarization, translationCross-attention encoder-decoder

2.2 Optimizing Transformers for Specific Tasks

Fine-tuning and Transfer Learning

Transfer learning starts with a powerful general model, then adapts it to a specific task.

graph TB
    A["Foundation Model<br/>(pre-trained on billions of tokens)"] --> B{Strategy}
    B --> C["Full fine-tuning<br/>Train entire model<br/>+ new classification head"]
    B --> D["Feature Extraction<br/>Freeze the Transformer<br/>Train only the head"]
    C --> E["Specialized model<br/>(sentiment analysis,\nsummarization, NER...)"]
    D --> E

Why fine-tuning works:

  • The base model already understands language
  • Few labeled examples are needed
  • Fast convergence with a low learning rate

Optimization Techniques

Distillation:

Creates a smaller “student” model that learns the behaviors of a larger “teacher” model.

graph LR
    A["Teacher Model<br/>(large, slow, accurate)"] -->|"Soft labels\n(probability distributions)"| B["Student Model\n(small, fast)"]
    B --> D["Final model\nlighter and\nalmost as good"]

Quantization:

Reduces the precision of numbers stored in the model.

Float32 (4 bytes)  →  Float16 (2 bytes)  →  Int8 (1 byte)

Advantages:
  • Less memory
  • Faster inference
  • Minimal performance loss if done well
  
Use cases:
  • Mobile deployment
  • Resource-limited hardware

Using Existing Fine-tuned Models

# Sentiment analysis (fine-tuned BERT)
classifier = pipeline("sentiment-analysis", 
                       model="distilbert-base-uncased-finetuned-sst-2-english")

# Summarization (BART fine-tuned on CNN/DailyMail)
summarizer = pipeline("summarization", 
                       model="facebook/bart-large-cnn")

# Code generation
code_gen = pipeline("text-generation", 
                     model="Salesforce/codegen-350M-mono")

2.3 Demo: Fine-tuning Transformers

Fine-tuning T5 on the CNN/DailyMail dataset for the automatic summarization task.

Recommended environment: Google Colab with T4 GPU for fast training.

Step 1 — Load the Dataset

from datasets import load_dataset

dataset = load_dataset("cnn_dailymail", "3.0.0")

print("Article:", dataset["train"][0]["article"])
print("Summary:", dataset["train"][0]["highlights"])

Dataset structure:

  • article: full article text (long)
  • highlights: human summary (short)
  • ~300,000 training examples, ~13,000 validation

Step 2 — Tokenization and Preprocessing

from transformers import AutoTokenizer

# T5 uses a "text-to-text" format:
# must indicate task type as prefix
tokenizer = AutoTokenizer.from_pretrained("t5-small")

def preprocess(example):
    inputs = ["summarize: " + doc for doc in example["article"]]
    
    model_inputs = tokenizer(
        inputs,
        max_length=512,
        truncation=True,
        padding="max_length"
    )
    
    with tokenizer.as_target_tokenizer():
        labels = tokenizer(
            example["highlights"],
            max_length=128,
            truncation=True,
            padding="max_length"
        )
    
    model_inputs["labels"] = labels["input_ids"]
    return model_inputs

tokenized = dataset.map(preprocess, batched=True)

small_train = tokenized["train"].shuffle(seed=42).select(range(20000))
small_eval  = tokenized["validation"].shuffle(seed=42).select(range(1000))

Step 3 — Load the Model

from transformers import AutoModelForSeq2SeqLM

model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")

T5 variant comparison:

ModelParametersRecommendation
t5-small60MDemo, rapid prototyping
t5-base220MGood general balance
t5-large770MHigher quality
t5-3b3BHigh performance
t5-11b11BState of the art

Step 4 — Training Configuration

from transformers import TrainingArguments

training_args = TrainingArguments(
    output_dir="./t5_summarization",
    per_device_train_batch_size=8,
    per_device_eval_batch_size=8,
    gradient_accumulation_steps=2,
    learning_rate=3e-4,
    num_train_epochs=4,
    lr_scheduler_type="linear",
    eval_strategy="epoch",
    save_strategy="epoch",
    save_total_limit=2,
    load_best_model_at_end=True,
    logging_steps=100,
    report_to="none"
)

Step 5 — Early Stopping

from transformers import EarlyStoppingCallback

callbacks = [EarlyStoppingCallback(early_stopping_patience=2)]

Step 6 — Training

from transformers import Trainer

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=small_train,
    eval_dataset=small_eval,
    tokenizer=tokenizer,
    callbacks=callbacks
)

trainer.train()
# Observe decreasing loss across epochs
# (e.g.: 1.8 → 1.4 → 1.2 → 1.1)
# Duration: ~1h on Colab T4 GPU with these parameters

2.4 Evaluating LLMs

BLEU Score (Bilingual Evaluation Understudy)

import evaluate

bleu = evaluate.load("bleu")
result = bleu.compute(
    predictions=["the cat sat on the mat"],
    references=[["the cat sat on the mat"]]
)
print("BLEU score:", result["bleu"])

BLEU measures n-gram overlap between generated and reference text. Originally designed for translation.

ROUGE (Recall-Oriented Understudy for Gisting Evaluation)

import evaluate

rouge = evaluate.load("rouge")

predictions = ["The cat sat on the mat near the window."]
references  = ["The cat sat on the mat."]

result = rouge.compute(predictions=predictions, references=references)
print("ROUGE-1:", result["rouge1"])   # Unigram overlap
print("ROUGE-2:", result["rouge2"])   # Bigram overlap
print("ROUGE-L:", result["rougeL"])   # Longest Common Subsequence

ROUGE metric types:

MetricMeasuresUse Case
ROUGE-1Unigram overlapIndividual word coverage
ROUGE-2Bigram overlapPhrase-level coherence
ROUGE-LLongest Common SubsequenceSequence-level structure

Perplexity

Measures how well the model predicts the next token. Lower perplexity = better model.

$$\text{Perplexity}(W) = P(w_1, w_2, …, w_n)^{-\frac{1}{n}}$$

BERTScore

Uses BERT embeddings to measure semantic similarity between generated and reference text.


Quick Reference

Key Transformer Concepts

mindmap
  root((Transformers\nand LLMs))
    Architecture
      Token Embeddings
      Positional Encoding
      Multi-Head Self-Attention QKV
      Add & Norm
      Feed Forward Network
      BERT Encoder
      GPT Decoder
      T5 Encoder-Decoder
    Training
      Self-supervised learning
      Masked Language Model BERT
      Causal Language Model GPT
      Cross-Entropy Loss
      Backpropagation
    Practice
      Hugging Face Transformers
      Pipeline API
      Fine-tuning
      Transfer Learning
      Distillation
      Quantization
    Evaluation
      BLEU
      ROUGE-1/2/L
      Perplexity
      BERTScore

Resources

ResourceLink
Hugging Face Transformershuggingface.co/docs/transformers
”Attention Is All You Need” paperarxiv.org/abs/1706.03762
CNN/DailyMail Datasethuggingface.co/datasets/cnn_dailymail
evaluate library (ROUGE)huggingface.co/docs/evaluate

Search Terms

transformers · llms · ai · genai · foundations · artificial · intelligence · generative · models · transformer · architecture · comparison · evaluation · fine-tuning · llm · load · rnns · self-supervised · understudy

Interested in this course?

Contact us to book it or get a custom training plan for your team.