DVP IBM AI Engineering
Course 7 of 13 · Generative AI / LLMs

Generative AI and LLMs: Architecture and Data Preparation

The pivot point — from training models to adapting foundation models. Tokenization, data quality, and the GPT/BERT/T5 map

Course 7 ≈ 6 hours Generative AI / LLMs Python you need first: LLM-era Python
Motion graphic
Generative AI and LLMs: Architecture and Data Preparation
Text torn into subword pieces, pieces mapped to integers — everything a language model does starts with this step.

6 hours · 2 modules · PyTorch, NLTK, spaCy, Hugging Face tokenizers

The pivot point of the certificate. Courses 1–6 were about training models. From here on you are an AI Engineer: you take enormous pretrained models and adapt them. This course sets the vocabulary for everything that follows.


Module 1 — Generative AI Architecture

Discriminative vs generative

new data from it.

That's the whole distinction, and it's the first quiz question.

The architectures, and what each is for

RNN / LSTM — sequential, hidden state carries memory. Superseded for language, still used for small time-series work. Weakness: no parallelism, weak long-range memory.

Transformer — self-attention, fully parallel, long-range. The architecture behind every current LLM.

VAE (Variational Autoencoder) — encoder produces a distribution (μ, σ) over a latent space; sample it and decode to generate. Loss = reconstruction + KL divergence. Smooth, interpolatable latent space; outputs tend to be blurry.

GAN — generator vs discriminator, adversarial. Sharp outputs, one-shot generation, unstable training, mode collapse.

Diffusion — learn to reverse a gradual noising process. Currently dominant for images (Stable Diffusion, DALL·E, Midjourney). Stable training, excellent quality, slow generation because it's iterative.

Best atWeakness
RNN/LSTMshort sequences, streamingslow, forgets long range
Transformertext, anything sequentialO(n²) attention cost
VAEsmooth latent space, interpolationblurry samples
GANsharp images, fast generationunstable, mode collapse
Diffusionhighest-fidelity imagesslow, many sampling steps

The LLM families — the table to memorise

ModelArchitecturePretraining objectiveGood at
GPTdecoder-onlynext-token prediction (causal LM)generation, chat, completion
BERTencoder-onlymasked LM + next-sentence predictionclassification, NER, understanding
BARTencoder–decoderdenoising (corrupt then reconstruct)summarisation, generation
T5encoder–decoderspan corruption, everything as text-to-texttranslation, summarisation, QA

The mental model:

Ask "does this task need to read, write, or translate?" and the family follows.

Generative AI for NLP

Tasks the videos enumerate: text generation, summarisation, translation, question answering, sentiment analysis, code generation, chatbots/dialogue, named entity recognition.

The shift the course is arguing for: previously each task needed a task-specific model trained on task-specific labelled data. Now one foundation model handles all of them via prompting, with fine-tuning only where you need domain adaptation.

Hallucination

An LLM produces fluent, confident, false output. Not a bug you can patch — a consequence of the objective. The model is trained to predict plausible next tokens, and plausible is not the same as true. It has no internal fact-checking mechanism and no representation of its own uncertainty.

Causes: gaps in training data, outdated training data, ambiguous prompts, over-confident decoding, and the model's tendency to complete a pattern rather than decline.

Mitigations, in rough order of effectiveness:

  1. RAG — ground answers in retrieved documents (Courses 12–13; this is the main answer)
  2. Fine-tuning on domain data (Courses 10–11)
  3. Prompt engineering — ask for citations, permit "I don't know"
  4. Lower temperature for factual tasks
  5. Human review for anything consequential

Why it matters here: this is why the last third of the certificate is about RAG. The architecture answer to hallucination is don't ask the model to recall — give it the document.

watsonx.governance

IBM's product for AI governance: model lifecycle tracking, bias/drift monitoring, explainability, and regulatory compliance documentation. It appears because it's IBM's course. Know the concept — AI governance = monitoring, documentation, risk and compliance for deployed models — and that it's a real interview topic under the EU AI Act.

Libraries you'll use

The fastest possible LLM call, which the lab has you run:

from transformers import pipeline

gen = pipeline('text-generation', model='gpt2')
print(gen("The future of AI is", max_length=50, num_return_sequences=1))

clf = pipeline('sentiment-analysis')
print(clf("This course is dense but useful."))

pipeline() handles tokenisation, model loading, inference and decoding in one call. Good for demos; you'll drop to the lower-level API when you need control.

Self-check — answer without looking
  1. GPT vs BERT — architecture, objective, and which for sentiment classification?
  2. Why can't hallucination be fully fixed by better training?
  3. Which architecture for image generation today, and why did it displace GANs?

Module 2 — Data Preparation for LLMs

Tokenization

Splitting text into the units a model actually processes. Everything downstream depends on it.

Word-based — split on whitespace/punctuation.

entries.

Character-based — tiny vocabulary (~100), no OOV ever.

Subword — the compromise every modern LLM uses. Frequent words stay whole; rare words break into meaningful pieces. unhappiness → un + happi + ness. Fixed vocabulary, no OOV, morphology partly captured.

Subword algorithms:

adjacent pair. Used by GPT-2, RoBERTa.

BERT. Marks continuations with ##.

so it needs no pre-tokenisation and handles languages without spaces. Used by T5, XLNet, ALBERT.

import nltk
nltk.word_tokenize("Don't split this wrongly.")
# ['Do', "n't", 'split', 'this', 'wrongly', '.']

import spacy
nlp = spacy.load('en_core_web_sm')
[t.text for t in nlp("Apple is looking at buying a U.K. startup.")]

from transformers import BertTokenizer, XLNetTokenizer
bt = BertTokenizer.from_pretrained('bert-base-uncased')
bt.tokenize("Tokenization is unbelievably important")
# ['token', '##ization', 'is', 'unbelievable', '##ly', 'important']   ← ## = continuation

xt = XLNetTokenizer.from_pretrained('xlnet-base-cased')
xt.tokenize("Tokenization is important")
# ['▁Token', 'ization', '▁is', '▁important']                          ← ▁ = word start

Two conventions to keep straight: BERT/WordPiece marks continuation with ##; XLNet and SentencePiece mark word start with ▁.

Special tokens

TokenPurpose
[CLS] / <s>start of sequence; its final embedding represents the whole sequence
[SEP] / </s>separator / end of sequence
[PAD]padding to equal length within a batch
[UNK]unknown token
[MASK]the position BERT must predict during masked-LM pretraining

From tokens to tensors

enc = tokenizer("Hello world", padding=True, truncation=True,
                max_length=128, return_tensors='pt')
enc['input_ids']       # token IDs
enc['attention_mask']  # 1 = real token, 0 = padding

attention_mask is not optional. It tells the model to ignore padding positions. Without it, padding tokens participate in attention and corrupt the representation — silently.

Three steps, always in this order: tokenize → numericalize → pad.

Data loaders for NLP

Text samples have different lengths, so batching needs a collate_fn:

from torch.nn.utils.rnn import pad_sequence
from torch.utils.data import DataLoader

def collate_fn(batch):
    texts, labels = zip(*batch)
    ids = [torch.tensor(tokenizer.encode(t)) for t in texts]
    ids = pad_sequence(ids, batch_first=True, padding_value=tokenizer.pad_token_id)
    return ids, torch.tensor(labels)

loader = DataLoader(dataset, batch_size=32, shuffle=True, collate_fn=collate_fn)

collate_fn runs after __getitem__ gathers a batch and before it's handed to your loop — it's where padding belongs. Pad per batch, not to a global maximum: padding everything to the longest document in the dataset wastes enormous compute.

For torchtext-style pipelines the course also shows build_vocab_from_iterator, vocab objects, and <unk> default index handling.

Data quality and diversity

The section with the highest real-world value in this course.

Quality dimensions:

Why it dominates model quality: at fixed compute, data quality beats data quantity. Deduplication alone measurably improves models — duplicated passages get memorised and crowd out learning. Recent small-model results (training on curated/textbook-quality corpora) beat much larger models trained on raw scrapes.

Bias in → bias out. A model trained on internet text reproduces the internet's demographic skews, stereotypes and coverage gaps. There is no debiasing step downstream that fully repairs a skewed corpus — which is exactly why data curation is a job, and why it connects directly to the Human Data Lead work.

The preprocessing pipeline:

  1. Collect
  2. Deduplicate (exact and near-duplicate; MinHash/LSH at scale)
  3. Filter for quality (heuristics, classifiers, perplexity filters)
  4. Remove PII and toxic content
  5. Normalise (encoding, whitespace, unicode)
  6. Tokenize
  7. Shuffle and shard
Gotchas

produces garbage — the ID→token mapping is different. No error, just nonsense output.

tokens; modern LLMs far more) and chunk deliberately rather than letting it clip.

text run much higher. This is what you're billed on with commercial APIs.

Self-check — answer without looking
  1. Why is subword tokenization preferred to word-level and character-level?
  2. What does attention_mask do and what breaks without it?
  3. Where should padding happen, and why per-batch rather than globally?
  4. Give three reasons deduplication improves a trained model.

Course 7 in one page

Supplemental — verified sources
video
Let's build the GPT Tokenizer
Andrej Karpathy · 2h 13m
BPE built from scratch. After this you will never be confused by a tokenizer again — including why token count ≠ word count.
video
Deep Dive into LLMs like ChatGPT
Andrej Karpathy · 3h 31m
The best single overview of the whole LLM stack — pretraining, data, tokenization, alignment. Covers Courses 7 through 11 in one sitting.
docs
Summary of the tokenizers
Hugging Face
BPE vs WordPiece vs SentencePiece, with the ## and ▁ conventions explained side by side.
docs
Transformers documentation
Hugging Face
The library the entire second half of the certificate runs on.