› 01 · Tokens › Token IDs
⚡ Visual Simulator
AI Journey · Understand Language

Token IDs

Master token IDs as integer vocabulary addresses — the bridge from tokenizer output to embedding lookup and the gateway into a model's numeric world.

🔴 MUST 15 study phases Tokens
🧙‍♂️ The Wise Scholar's Metaphor Dewey Decimal Index & Shelf Numbers
⏮️ 00 · Math & Foundations Hub →

Token IDs: The Librarian's Index Card Numbers

A grand library doesn't search for books by wandering through millions of aisles reading covers. The librarian looks up the exact catalog index number (e.g. #17242 for "apple"). In neural networks, a Token ID is simply that catalog number: it directs the model to pull Row #17242 out of the massive Embedding Matrix to retrieve the rich concept vector stored inside.

01

IntroductionWhat is a Token ID?

Visual lesson: A token ID is only a row index. See how it selects a vector, and what a dimension is. Open "Rows. Dimensions. Vectors." →
#charan #dimension #vector
Six-step diagram of how a question becomes vectors, paired with a librarian analogy
Your question → tokens → embedding lookup → input matrix → Transformer → next token, shown next to a librarian analogy. Click to open full size. Numbers are illustrative.
🤔 Layman Curiosity Hook

"If you were writing a letter to a friend, why would you translate every single word into a dry postal box address before mailing it? Why does an AI secretly turn your warm words into numeric barcodes?"

To humans, words are ideas, feelings, and memories. But to a computer's processor, text is completely un-multipliable. A computer cannot multiply the word "love" by a matrix. It needs a barcode address system: Token IDs.

💡 This is how it happens: Think of the AI's memory as a massive skyscraper with 50,000 apartments. Apartment 12 houses "I", Apartment 47 houses "love", and Apartment 91 houses "AI". When you write "I love AI", the tokenizer translates it into a sequence of mailbox addresses: [12, 47, 91]. To the model, your sentence is just a string of delivery stops. The AI opens those specific apartment doors to pull out the actual floating-point numbers describing what those words mean!

A token ID is a single integer. It is the row index of a token in the model's embedding matrix. The tokenizer converts text to a list of token strings, then maps each string to its integer ID. The model never sees the string — only the number.

"I love AI" ↓ tokenize ["I", " love", " AI"] ↓ vocab lookup [ 12, 47, 91 ] ← these are token IDs ↓ embedding lookup [ vec_12, vec_47, vec_91 ] ← 768-float vectors, one per token

What a token is

A piece of text — a word, subword, character, or special symbol. It is a string like " love" or "##ing". Humans and linguists think in tokens.

What an ID is

A non-negative integer: 47. It is the address of that token in the vocabulary. Models and matrix multiplications think in IDs.

Raw text→ Tokenizer→ Token strings→ Vocab lookup→ Token IDs (ints)→ Embedding matrix→ Float vectors

💡 IDs are just addresses. Token ID = the integer row number of a token inside the embedding matrix. Nothing more, nothing less.

02

WhyWhy do we need IDs at all?

Neural networks operate on floating-point tensors. There is no operation "multiply a string by a matrix." You need a way to turn a discrete category (the token) into a number so the model can retrieve its learned representation.

  • A matrix multiply requires numeric input — strings are meaningless to PyTorch.
  • IDs are the categorical lookup keys that map each token to a row in the embedding matrix.
  • Without IDs, there is no addressable way to retrieve the learned float vector for a token.
  • IDs let you represent a full sentence as a compact integer array — far more efficient than storing strings in memory.

Without IDs — impossible

You have a string "love". You cannot index a matrix with a string. You cannot pass it to nn.Embedding. You cannot batch it into a tensor.

With IDs — everything works

ID 47 is an integer. embedding_matrix[47] returns a row. You can build a batch tensor [[12,47,91],[5,3,47]] and feed it to the model directly.

Insight: Token IDs are the bridge between the symbolic world of text and the numeric world of tensors. Every language model depends on this bridge.
03

IntuitionLibrary call numbers

Token IDs are like library call numbers in the Dewey Decimal System. The number 791.43 tells you the shelf where films are shelved. It does not mean films are 791.43 times anything. It is just an address.

Token ID 500 is not "more meaningful" or "more important" than ID 20. It is not five times anything. It just lives on a different shelf — row 500 versus row 20 in the embedding matrix. The number tells you where to look, never what to think about what you find.

Library analogy Call number 791.43 → shelf → book about films Call number 020.00 → shelf → book about libraries Token ID 500 → embedding_matrix[500] → vector for " world" Token ID 20 → embedding_matrix[20] → vector for " the" The number is only an address. The meaning lives in the vector at that address.

💡 Critical mental model: Never do arithmetic on IDs. ID_500 − ID_20 = 480 means nothing. The semantic relationship between tokens lives in their embedding vectors, not in the distance between their IDs.

04

TheoryThe vocabulary bijection

The vocabulary is a bijective mapping (one-to-one, onto) between token strings and integers:

vocab: token_string ↔ integer_ID (bijection) "[PAD]" ↔ 0 ← reserved: padding "[UNK]" ↔ 1 ← reserved: unknown token "[BOS]" ↔ 2 ← reserved: beginning of sequence "[EOS]" ↔ 3 ← reserved: end of sequence "I" ↔ 4 "love" ↔ 5 "AI" ↔ 6 ... "the" ↔ 50257 ← GPT-2 vocab size example
  • IDs start from 0. They are contiguous integers: 0, 1, 2, …, vocab_size−1.
  • Special tokens ([PAD], [UNK], [BOS], [EOS], [MASK], etc.) occupy reserved IDs, usually at the low end.
  • The embedding matrix has shape [vocab_size × embed_dim]. Row N is the learned float vector for token ID N.
  • Accessing row N: embedding_matrix[N] returns a vector of embed_dim floats (e.g. 768 for BERT-base).
Bijection matters: Every token string maps to exactly one ID, and every ID maps to exactly one token string. This guarantees lossless encode → decode round-trips for known tokens.
05

How It WorksEncode and decode cycle

There are exactly two operations on the vocabulary mapping:

  • encode (token_string → ID): look up the string in the vocab dict. If missing, return the ID of [UNK].
  • decode (ID → token_string): look up the integer in the id_to_token reverse dict.
Encode cycle Input sentence: "I love AI" After tokenizer: ["I", " love", " AI"] Vocab lookup: "I" → 12 (found in vocab) " love" → 47 (found in vocab) " AI" → 91 (found in vocab) Token IDs: [12, 47, 91] Input sentence: "I love pizza" Vocab lookup: "I" → 12 (found) " love" → 47 (found) " pizza" → 1 ← [UNK] — not in vocabulary! Token IDs: [12, 47, 1] Decode cycle Token IDs: [12, 47, 91] ID lookup: 12 → "I" 47 → " love" 91 → " AI" Decoded: "I love AI" ← exact round-trip ✓
Token strings→ vocab dict→ IDs (encode)↔ id_to_token dict→ Token strings (decode)
06

CalculateManual encode / decode exercise

Build a toy vocabulary with 5 entries, then encode and decode by hand. Follow each step carefully.

Step 1
Build vocab: assign IDs in order of insertion.
{ "[PAD]": 0, "[UNK]": 1, "I": 2, "love": 3, "AI": 4 }
Step 2
Encode "I love AI": tokenize → whitespace split → look up each token.
"I" → 2   "love" → 3   "AI" → 4  → Result: [2, 3, 4] ✓
Step 3
Encode "I love pizza": unknown token gets mapped to UNK.
"I" → 2   "love" → 3   "pizza" → [UNK] = 1  → Result: [2, 3, 1]
Step 4
Decode [2, 3, 1]: reverse lookup each ID.
2 → "I"   3 → "love"   1 → "[UNK]"  → Decoded: "I love [UNK]" ← information lost!
Lesson: UNK is a one-way door. Once a token is mapped to [UNK] during encoding, the original string cannot be recovered during decoding. This is why modern tokenizers (BPE, WordPiece) aim for zero-UNK coverage by building large subword vocabularies.
07

MathematicsID as row index into the embedding matrix

The embedding matrix $\mathbf{E}$ has shape $[V, D]$ where $V$ is the vocabulary size and $D$ is the embedding dimension. A token ID is simply a row selector.

$$\mathbf{E} \in \mathbb{R}^{V \times D}$$

For token ID $i$, the embedding lookup retrieves row $i$:

$$\mathbf{e}_i = \mathbf{E}[i, :] \in \mathbb{R}^{D}$$

For a sequence of $L$ token IDs $[i_1, i_2, \ldots, i_L]$, the model receives:

$$\mathbf{X} = \mathbf{E}[[i_1, i_2, \ldots, i_L]] \in \mathbb{R}^{L \times D}$$
Embedding matrix shape: [50257, 768] (GPT-2 base) ↑ ↑ vocab_size embed_dim E[0] = 768-float vector for [PAD] E[1] = 768-float vector for [UNK] E[2] = 768-float vector for "I" E[47] = 768-float vector for " love" E[91] = 768-float vector for " AI" For sentence "I love AI" → IDs [2, 47, 91]: Embedding lookup = E[[2, 47, 91]] = stack of 3 rows = tensor of shape [3, 768] ← input to Transformer

ID has no numeric meaning

ID 91 is not "91 units of something". It is an address. The meaning of the token lives entirely in row 91 of E — a learned 768-float vector.

Embedding lookup is O(1)

Matrix row selection is a constant-time operation. No computation is done — the model just reads the pre-learned row. The learning happened during training.

PyTorch: nn.Embedding(vocab_size, embed_dim) is literally a matrix of shape [vocab_size, embed_dim] with a forward pass that does weight[input_ids] — pure row indexing.

Vocabulary row vs embedding row

A token ID can be treated as a row ID in two tables. The numbers below are illustrative.

Vocabulary (tokenizer) Embedding matrix E (model) ID Token Row Vector 0 the 0 [ 0.2, 0.5, -0.1, ...] 1 cat ◄──── same ID ────► 1 [ 0.8, -0.3, 0.4, ...] 2 dog 2 [-0.2, 0.7, 0.1, ...] 3 bank 3 [ 0.1, 0.6, -0.5, ...] 4 river 4 [ 0.0, 0.4, -0.6, ...] "the cat" → [0, 1] → rows E[0], E[1] → two vectors → Transformer
  • The vocabulary maps tokens to IDs (and often back). The embedding matrix maps IDs to learned vectors.
  • The matrix stores no token strings; it lines up with the vocabulary only through the shared ID.
  • The ID is the key. The learned vector is the content, and it is what carries the numerical representation. See Token Embeddings for the full walkthrough.

Does a token ID contain the vector numbers?

No. A token ID identifies which row of the embedding matrix to read. The numbers stored in that row form the token's vector. Using the toy 4-number row for cat from the table above (illustrative values):

Token ID 1 ──► "which row?" (an address, not a vector) Row E[1] = [ 0.80, −0.30, 0.40, 0.15 ] ← the vector for "cat" dim 1 dim 2 dim 3 dim 4

💡 Token ID = which row? Vector = the learned numbers stored in that row. ID 1 is not the vector [0.80, −0.30, 0.40, 0.15]; it only tells the model which vector to retrieve. Where those numbers come from is covered in How Embeddings Are Learned, and what "4 numbers" means is covered under vector dimensions.

08

VisualizeVocabulary table, encode/decode diagram, and live demo

Sample vocabulary entries showing the ID ↔ token mapping:

IDTokenType
0[PAD]Special — padding
1[UNK]Special — unknown
2[BOS]Special — beginning of sequence
3[EOS]Special — end of sequence
4theCommon word
5ofCommon word
6andCommon word
7ingCommon subword suffix

🔄 Encode / Decode Flow

graph LR
    A["String: 'AI'"] -->|Vocabulary Map| B["Token ID: 9821"]
    B -->|Embedding Table Index| C["Row #9821 of E"]
    C -->|Output| D["Vector: [0.15, -0.92, ..., 0.38]"]
    D -->|argmax during generation| B2["Token ID: 9821"]
    B2 -->|Reverse vocab lookup| A2["String: 'AI'"]

    style A fill:#3b82f6,stroke:#1d4ed8,color:#fff
    style B fill:#10b981,stroke:#047857,color:#fff
    style D fill:#8b5cf6,stroke:#6d28d9,color:#fff
    style B2 fill:#10b981,stroke:#047857,color:#fff
    style A2 fill:#3b82f6,stroke:#1d4ed8,color:#fff
          
Fig 1. Bi-directional encode/decode flow — token string → ID → embedding vector (forward), and argmax → ID → string (generation).
🗄️ Live Lab: Token ID → Embedding Matrix Lookup Inspector
See How Token IDs Index Rows in the $[V \times D]$ Weight Matrix
1. Click or Hover Any Token Chip to Highlight its Matrix Row:
Embedding Table $E$ $[V=30{,}000 \times D=768]$ Active Selection: Row #47 ("love")
Row (ID) Token String $d_0$ $d_1$ $d_2$ $d_3$ ... $d_{767}$
🔍 Behind-the-Scenes: PyTorch nn.Embedding Implementation

How a framework actually runs embedding lookup behind the scenes:

  1. Zero Arithmetic: nn.Embedding is NOT a neural network with matrix multiplications; it is a direct lookup table in GPU memory ($O(1)$ memory fetch).
  2. Gather Operation: When given torch.tensor([2, 47, 91]), PyTorch calls torch.index_select(weight, 0, ids).
  3. Resulting Tensor: Stacks 3 float vectors of dimension 768 to output a tensor of shape [3, 768] ready for Self-Attention!
09

CodePython implementation + HuggingFace comparison

Part A — toy vocabulary in pure Python:

Python — Toy Vocabulary
# ── Toy vocabulary ────────────────────────────────────────────────
vocab = {
    "[PAD]": 0, "[UNK]": 1, "[BOS]": 2, "[EOS]": 3,
    "I": 4, "love": 5, "AI": 6, "and": 7, "learning": 8,
    "the": 9, "of": 10, "to": 11, "a": 12, "is": 13,
    "tokens": 14, "model": 15, "text": 16, "word": 17,
}

id_to_token = {v: k for k, v in vocab.items()}   # reverse map

def encode(sentence: str) -> list[int]:
    """Whitespace-tokenize and look up IDs; unknown → UNK."""
    return [vocab.get(tok, vocab["[UNK]"]) for tok in sentence.split()]

def decode(ids: list[int]) -> str:
    """Convert IDs back to token strings."""
    return " ".join(id_to_token.get(i, "[UNK]") for i in ids)

# ── Encode ─────────────────────────────────────────────────────────
ids = encode("I love AI")
print(ids)          # [4, 5, 6]

ids_unk = encode("I love pizza")
print(ids_unk)      # [4, 5, 1]  ← pizza → UNK (ID 1)

# ── Decode ─────────────────────────────────────────────────────────
print(decode([4, 5, 6]))    # "I love AI"
print(decode([4, 5, 1]))    # "I love [UNK]"

Part B — HuggingFace tokenizer (GPT-2):

Python — HuggingFace GPT-2
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("gpt2")

# ── Encode ─────────────────────────────────────────────────────────
ids = tokenizer.encode("I love AI")
print(ids)                      # [40, 1842, 9552]  (real GPT-2 IDs)

# ── Decode ─────────────────────────────────────────────────────────
text = tokenizer.decode(ids)
print(text)                     # "I love AI"

# ── Convert to tokens (strings) ────────────────────────────────────
tokens = tokenizer.convert_ids_to_tokens(ids)
print(tokens)                   # ['I', 'Ġlove', 'ĠAI']
# Note: Ġ is GPT-2's representation of a leading space

# ── Get ID of a single token ────────────────────────────────────────
print(tokenizer.convert_tokens_to_ids("love"))   # a specific integer

# ── Special token IDs ──────────────────────────────────────────────
print(tokenizer.eos_token_id)    # 50256 for GPT-2
print(tokenizer.pad_token_id)    # None for GPT-2 (no PAD in GPT-2)
Key difference: Your toy vocab and GPT-2's vocab are different mappings. The same word "love" will have a different integer ID in each tokenizer. IDs are model-specific — always know which tokenizer you are using.
10

ExperimentIDs change across tokenizers

The same surface form gets completely different integer IDs in different tokenizers. Even within one tokenizer, context like a leading space changes the ID.

Same word, different tokenizers (GPT-2 style):

GPT-2: "apple"
Tokens: ["apple"] → Token IDs: [14208]

GPT-2: " apple" (with leading space)
Tokens: ["Ġapple"] → Token IDs: [17180]

GPT-2: "Hello"
Tokens: ["Hello"] → Token IDs: [15496]

GPT-2: "hello" (lowercase)
Tokens: ["hello"] → Token IDs: [31373]

GPT-2: "heLLo" (typo)
Tokens: ["he", "LL", "o"] → Token IDs: [258, 1492, 85]

Try these experiments yourself with a real tokenizer:

11

CompareToken String vs Token ID vs Embedding Vector

Here is exactly where token IDs sit relative to the other representations in an LLM pipeline:

Property Token String Token ID Embedding Vector
TypeString (text)IntegerArray of floats
Example" love"47[0.12, -0.34, …] (768 values)
Semantic meaningHuman-readable ✓None (just an address)Full learned meaning ✓
Fed to model directlyNo — strings not acceptedYes — integer indexYes — after lookup
Size in memoryVariable (bytes per char)4 bytes (int32)embed_dim × 4 bytes (≈ 3 KB)
Can do arithmetic onConcatenation onlyNo (meaningless)Yes — cosine similarity, addition, etc.
Survives round-tripSource of truthYes (via id_to_token)Not invertible
Used forTokenizer I/O, displayEmbedding lookup, batchingAttention, FFN, generation

🧠 What does the model know right after tokenization?

Not much yet. After tokenization the text has become discrete token IDs. Those IDs identify tokens, but the integers themselves do not encode meaning in a numeric geometry the transformer can use. Compare the three stages (IDs below are illustrative; real IDs depend on the tokenizer):

"The cat is sleeping" ↓ tokenization [464, 3797, 318, 10447] ← token IDs: identifiers only ↓ embedding lookup (row i of the embedding matrix E) 4 vectors, e.g. cat → [0.21, -0.43, 0.87, ...] ← input the transformer can compute with ↓ transformer layers (attention + other computation) 4 contextual representations ← each token's vector updated using the other tokens
  • Why not feed IDs directly? cat → 3797 and dog → 8531 does not mean a dog is "more" of anything than a cat. A larger ID is not more important or more similar.
  • Same ID, different context. bank has one ID in "The bank approved my loan" and in "We sat on the bank of the river". The transformer builds a different contextual representation for each from the surrounding words.
  • Knowledge is not acquired at the embedding step. The embedding matrix and transformer weights were learned in training; the lookup and the layers apply them to your input.
Go deeper: Token Embeddings walks through the lookup with numbers, and Contextual Embeddings shows how the same ID ends up with different vectors.
12

ApplyToken IDs in real model pipelines

  • Model input tensors are integer ID arrays. The first thing any Transformer sees is a torch.LongTensor of shape [batch_size, sequence_length] filled with token IDs.
  • Batching requires padding: sentences in a batch have different lengths. All are padded to the same length using PAD IDs so they form a rectangular tensor.
  • Attention mask: a companion binary tensor (1 = real token, 0 = PAD) tells the model which IDs are real content versus padding. Without it, the model attends to PAD positions.
  • Context length is measured in number of IDs, not words. GPT-4's 128k context means 128,000 token IDs maximum.
  • nn.Embedding in PyTorch takes an integer tensor of IDs and returns the corresponding embedding rows — this is the first layer of every language model.
Python — PyTorch Batching + nn.Embedding
import torch
import torch.nn as nn

# Batch of 2 sentences, padded to length 5 with PAD ID=0
input_ids = torch.LongTensor([
    [4,  5,  6,  0, 0],   # "I love AI [PAD] [PAD]"
    [9, 10,  7,  8, 3],   # "the of ing learning [EOS]"
])

attention_mask = torch.LongTensor([
    [1, 1, 1, 0, 0],   # 1=real token, 0=padding
    [1, 1, 1, 1, 1],
])

# Embedding layer — the first thing the model does with IDs
embedding = nn.Embedding(num_embeddings=50257, embedding_dim=768)
vectors = embedding(input_ids)
print(vectors.shape)   # torch.Size([2, 5, 768])
#   batch=2, seq_len=5, embed_dim=768

💡 The integer tensor explodes in size. Input shape is [B, L] of 4-byte ints. After embedding lookup it becomes [B, L, D] of 4-byte floats — a >1000× memory expansion at the very first layer.

13

LimitationsCommon mistakes and traps

Trap 1 — IDs are model-specific: BERT token ID 1000 and GPT-2 token ID 1000 refer to completely different tokens. Never mix IDs from different tokenizers. Switching the tokenizer invalidates all stored IDs.
Trap 2 — Do not do arithmetic on IDs: ID 10 + ID 10 does not equal ID 20 in any meaningful sense. "token_id_A is close to token_id_B" means nothing. Semantic closeness lives in the embedding vectors, not in the IDs.
Trap 3 — ID 0 is not always PAD: By convention PAD is often 0, but tokenizers differ. Always check tokenizer.pad_token_id. GPT-2 has no PAD token at all by default.

Mistakes to avoid

  • Sorting tokens by ID expecting semantic order
  • Comparing IDs across different models
  • Assuming the same word always gets the same ID
  • Using ID magnitude to infer token importance
  • Feeding IDs from tokenizer A to model B

Correct mental models

  • ID = row address in the embedding matrix
  • IDs are vocabulary-specific; always know which tokenizer
  • Meaning = embedding vector, not the integer
  • Do arithmetic on embedding vectors, not on IDs
  • Store tokenizer name alongside any saved IDs
14

ExplainFeynman Challenge: Library Catalogue Numbers

Feynman Challenge — Explain It Simply

Token IDs are like library catalogue numbers. In a large library, every book has a Dewey Decimal call number like 791.43. That number tells you exactly which shelf and slot to look in. It does not tell you anything about the book's content — it is purely an address.

Token ID 14592 for "apple" is the call number for apple's shelf in the embedding library. When the model "opens that shelf" it retrieves a 768-float vector encoding everything the model learned about apples during training. The number itself carries none of that meaning.

Try explaining this to someone right now: "Why do language models use integer token IDs instead of working directly with strings? And why is it wrong to treat a higher ID as more meaningful than a lower ID?"

🚫 Common Misconceptions to Correct

  • Misconception: Token IDs are learned during backpropagation.
    Reality: No. Token IDs are assigned statically before training begins. They are permanent, deterministic indices. The neural network only trains the embedding values that reside at those indices.
  • Misconception: Reordering the vocabulary list breaks the model's intelligence.
    Reality: It does, but only because the embedding rows become misaligned. The indices are purely symbolic — if you swap both the vocabulary mapping table and the corresponding rows in the embedding matrix together, the model would still work correctly.
  • Misconception: ID 200 is "between" ID 100 and ID 300 in semantic space.
    Reality: No. Semantic space is defined by embedding vectors. The distance between IDs means nothing.
15

BuildMini-Project: Encode/Decode Pipeline + Batch Padder + Vocab Inspector

🎯 Four Things to Build for Mastery

1 — Full encode/decode pipeline

Build a Tokenizer class with a real vocabulary dict. Implement encode(text) and decode(ids). Handle UNK gracefully. Add a batch_encode(texts) that pads to the longest sequence.

2 — Batch padder

Write pad_batch(sequences, pad_id) that takes a list of variable-length integer lists and returns a rectangular 2D list padded with pad_id. Verify the shape is [batch_size, max_len].

3 — Attention mask generator

Given a list of token ID sequences of varying lengths, write make_attention_mask(batch) that pads all to the same length and returns the parallel attention mask tensor (1s for real tokens, 0s for PAD).

4 — Vocabulary inspector

Load a real HuggingFace tokenizer. Write a script that: (a) prints the 20 most common token IDs in a text corpus, (b) shows the string for any given ID, (c) finds whether two tokenizers assign the same ID to the word "the".

Extra credit: Add a JSON serializer/deserializer to your vocabulary class. Write save_to_json(filepath) and load_from_json(filepath). Add a validation step that checks for no duplicate IDs and no index gaps — critical for preventing segmentation faults during model load time.

📌 Summary — Everything in Six Bullets

  • ID = integer address. A token ID is a non-negative integer that identifies a token by its row position in the embedding matrix. It is not a score, not a frequency, not a magnitude — just an address.
  • Embedding matrix lookup. The embedding matrix E has shape [vocab_size, embed_dim]. A token ID N selects row N: E[N] returns the 768-float (or similar) vector that the model actually uses.
  • No semantic numeric meaning. Never do arithmetic on IDs. ID 200 is not more meaningful than ID 20. Semantic similarity lives in the embedding vectors, not in the distance between IDs.
  • Special token IDs. Reserved IDs (PAD=0, UNK=1, BOS=2, EOS=3 by convention) handle padding, unknowns, and sequence boundaries. Always check your specific tokenizer's special token IDs — conventions differ.
  • Padding uses PAD IDs. Batches require rectangular tensors. Shorter sequences are right-padded with PAD IDs. An attention mask (1=real, 0=pad) prevents the model from attending to padding positions.
  • IDs are model-specific. BERT ID 500 ≠ GPT-2 ID 500. Token IDs are only meaningful within the tokenizer and model they were created for. Always store the tokenizer name alongside any saved IDs.
This page 0:00
This session 0:00
Total (all pages) 0:00
Today 0:00
▶ Running
📊 View all study stats →
0:00 studying