๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #259

Vocabulary

A model's vocabulary is the complete, fixed set of tokens it knows how to represent โ€” every token id from 0 to vocab_size-1 maps to exactly one entry in this set, and this size choice ripples through the entire model's design.

The Vocabulary-Size Tradeoff

Vocabulary SizeEffect
Small (e.g. a few thousand)Small embedding/output layers (cheaper), but longer sequences (since more subword pieces are needed per word)
Large (e.g. 50,000+)Shorter sequences (more whole words get their own token), but larger embedding table and output projection layer โ€” more parameters and compute in exactly those two layers

Modern LLMs commonly use vocabularies in the tens of thousands to over 100,000 tokens, balancing these two costs empirically.

How Vocabulary Size Directly Sizes Two Layers

\[ \text{Embedding layer parameters} = \text{vocab\_size} \times d_{\text{model}} \] \[ \text{Output projection parameters} = d_{\text{model}} \times \text{vocab\_size} \]

Both the input embedding table (converting a token id into a vector) and the final output layer (converting a hidden vector into a probability distribution over every possible next token, as in Softmax Function) scale directly with vocabulary size โ€” for a large vocabulary and modest \(d_{\text{model}}\), these two layers alone can represent a significant fraction of a model's total parameters.

Special Tokens

Beyond ordinary word/subword pieces, most vocabularies reserve a handful of special tokens with dedicated meanings: <pad> (padding shorter sequences to a uniform length within a batch), <unk> (a fallback for anything genuinely unrepresentable), <bos>/<eos> (beginning/end of sequence, used in the autoregressive generation loop from Seq2Seq Model), and task-specific tokens like BERT's [CLS] and [SEP].

Code

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
print(tokenizer.vocab_size)          # 30522
print(tokenizer.cls_token, tokenizer.sep_token, tokenizer.pad_token)
# [CLS] [SEP] [PAD] -- special tokens reserved in the vocabulary

Common Mistakes

  • Using two different pretrained components (e.g. a tokenizer from one model with the embedding layer of another) without checking their vocabularies match exactly โ€” token ids are meaningless numbers without a specific vocabulary mapping them to actual sub-word pieces.
  • Underestimating how much a large vocabulary contributes to total model size โ€” for a small model, the embedding and output layers can dominate the parameter count if the vocabulary is disproportionately large.

Interview Relevance

Q: "How does vocabulary size affect a language model's total parameter count?" Both the input embedding table and the final output projection layer scale directly and proportionally with vocabulary size (each contributing \(\text{vocab\_size}\times d_{\text{model}}\) parameters). For models with modest hidden dimensions but large vocabularies, these two layers can represent a substantial fraction of the model's total parameters.

Practice Question

For a model with \(d_{\text{model}}=512\) and a vocabulary of 30,000 tokens, roughly how many parameters does the embedding layer alone contain?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Vocabulary โ€“ FAQs

Quick answers about learning Vocabulary in Deep Learning.

This free note from CodingNow 2.0 explains Vocabulary in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Vocabulary, is 100% free with no signup required.
With focused practice, most students grasp Vocabulary in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now