← govind p lal
AI and ML Bootcamp · Lecture 4 · lecture notes

Transformers - Part 1: Tokenization, Embeddings and Positional Embeddings

In short — How does raw text become input a Transformer can use? This lecture goes from tokenization (character, word, subword, BPE), through token embeddings in code, to positional embeddings. Adding position information fixes the Transformer's blindness to word order. The final input is token embedding + positional embedding.

Why we need tokenization

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026

Neural networks only understand numbers. Human language is text. We need a step that converts text into numbers the model can learn from and adjust in training. -> Tokenization.

IDsfinal inputTextTokenizertext -> token IDsToken embeddings768 dims+ PositionalembeddingsTransformer
Full input pipeline built up in this lecture.

Character-level and word-level tokenization

  1. Character level Every character gets its own number. Hello -> H=1, E=2, L=3, L=3, O=4.
  1. Word level Every word is one token. 'the cat sat' -> 1, 2, 3.

Subword tokenization

Like pronouncing a long new word by breaking it into parts. understanding -> under + stand + ing

Byte Pair Encoding (BPE)

Subword tokenization -> the idea. Byte Pair Encoding -> an algorithm that does it, like multiplication is the algorithm for a goal of multiplying numbers.

OpenAI, Anthropic and Google DeepMind each use their own tokenizer. Their current ones are not public. Older ones (GPT-3 era) are public. BPE is the old starting point.

What modern tokenizers do

Why tokenization matters

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026, Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026
Example

Strawberry is tokenized as str + aw + berry. The model never sees the whole word, so counting r's often gave 1, 2 or 4. Reasoning models take time, spell s-t-r-a-w-b-e-r-r-y and count, so they get 3. They can still slip occasionally.

!

Characters are too granular, words are too many. Subwords sit in between. Research on removing tokenizers is ongoing (Meta works on a BPE variant using entropy) but no good answer yet.

Tokenizers in practice (tiktokenizer and tiktoken)

import tiktoken
enc = tiktoken.get_encoding('cl100k_base')
print(enc.n_vocab)
ids = enc.encode('Hello world')
print(ids)  # [9906, 1917] -> 'Hello', ' world'
print(enc.encode('strawberry'))  # 3 tokens: str, aw, berry
Text typeTokens (cl100k_base)
Simple English~4-10
Python code11
JSON19 (most)
Numbers / URLin between
JSON is token-hungry because of braces, keys and values.

Other tokens from the library: emoji split into several byte-like tokens. Many spaces collapse to one space token. Token IDs differ between models even when splits match. English is cheaper than JSON for APIs.

From IDs to meaning: embeddings in code

adds to earlier · Embedding matrix lookup, parameter count, random init, and gensim GloVe demos in code · builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026

The Problem Token IDs are arbitrary. 15496 doesn't tell the model it is a greeting. The Solution Each ID points to a row in an embedding matrix, a vector capturing meaning.

import gensim.downloader as api
m = api.load('glove-wiki-gigaword-100')
m.most_similar('king')  # prince, queen, son, brother, monarch...
m.most_similar(positive=['king','woman'], negative=['man'])  # queen first

Why matrices/vectors: GPUs are fast at vector and matrix multiplication, and vectors let one point carry many dimensions.

The problem of sequence

builds on Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026, Lecture 2: Week 1 - AI and ML | Fast-tracking the course of AI | 18/01/2026

The Problem 'The dog bit the man' and 'The man bit the dog' contain the same tokens. Meaning differs only by order.

Sinusoidal positional embeddings

Idea Take several sine and cosine waves, each with a different frequency.

Combining final embedding = token embedding + positional embedding (same shape) Example: 'the cat sat' -> 3 tokens x 768 = (3, 768) for both, then added. Same word 'the' at position 0 vs position 5 gets different vectors (distance about 14). Meaning plus position in one vector.

TextTokens / IDsToken embeddings(n, 768)Positional encodingsin / cos, no paramsSumTransformer
Building the Transformer input.

Glossary

Tokenization
Converting text into token IDs a model can process.
Subword
A piece of a word used as a token, reusable across many words.
Byte Pair Encoding
Algorithm that merges frequently co-occurring pieces into tokens.
Vocabulary
The set of all tokens a model knows.
Token embedding
A vector (e.g. 768 numbers) representing a token's meaning.
Positional embedding
Vector added to a token embedding to carry its position in the sequence.