Keras
Tokenizer
num_words
machine learning
NLP

What does Keras Tokenizer num_words specify?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

In the field of Natural Language Processing (NLP), text data often needs to be transformed into a numerical format before it can be fed into machine learning models. Keras, a popular deep learning library in Python, provides several utilities to preprocess and transform textual data. One such utility is the `Tokenizer` class. Within this class, the `num_words` parameter plays a crucial role in determining how the text is tokenized.

Understanding the Keras `Tokenizer` Class

The `Tokenizer` class in Keras is designed to convert text into a sequence of integers, where each integer represents the index of a word in a dictionary. The class creates a word index based on the frequency of words across the texts provided to it.

The `num_words` Parameter

The `num_words` parameter specifies the maximum number of words to keep based on word frequency. More specifically, it means the `Tokenizer` will create a vocabulary using only the `num_words` most common words.

How `num_words` Works

  1. Frequency-Based Selection: After the `Tokenizer` is fitted on text data, it counts how often each word appears. The `num_words` parameter ensures that only the top `num_words` most common words (based on frequency) are included in the tokenization process.
  2. Impact on Vocabulary: If you specify `num_words=1000`, the tokenizer will build a dictionary of only the 1,000 most frequent words. Any words outside this range are ignored and won't be included in the dictionary.
  3. Efficiency and Model Performance: By limiting the size of the vocabulary through `num_words`, the model can focus on the most relevant parts of the text, potentially improving computational efficiency and model performance. However, setting `num_words` too low might lead to losing important information.

Code Example

Here is a simple example demonstrating how `num_words` is used within Keras `Tokenizer`:

  • Word Index: This dictionary will map the 10 most common words to unique integers, starting from 1.
  • Sequences: These are the transformed text sequences. Each word is replaced by its corresponding integer index. Words not in the top `num_words` list are ignored and thus appear as `0`.
  • Reduced Vocabulary Size: Helps in managing large texts by discarding less frequent words.
  • Efficient Memory Usage: By limiting vocabulary size, memory usage for storing text data is reduced.
  • Loss of Information: If significant words are less frequent, they might not be included, potentially losing nuanced information.
  • Careful Selection: Choosing an appropriate `num_words` value requires careful consideration and domain knowledge to balance information retention and efficiency.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.