Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

layers.IntegerLookup

IntegerLookup maps arbitrary integer tokens to contiguous vocabulary indices or categorical encodings. It can use a supplied vocabulary or learn one with adapt().

How it works

IntegerLookup builds or accepts a vocabulary of integer tokens, then converts inputs through a table lookup. With the default integer output, known tokens receive contiguous vocabulary indices and unknown tokens use OOV indices; other output modes encode the inputs as categorical vectors or counts.

Syntax

keras.layers.IntegerLookup(max_tokens=None, num_oov_indices=1, mask_token=None, oov_token=-1, vocabulary=None, vocabulary_dtype="int64", idf_weights=None, invert=False, output_mode="int", sparse=False, pad_to_max_tokens=False, oov_method="floormod", salt=None, name=None, **kwargs)

Parameters

NameTypeDescription
max_tokensint or NoneMaximum vocabulary size; relevant when adapting or padding to max_tokens.
num_oov_indicesintNumber of OOV buckets.
mask_tokenint or NoneOptional integer used for masking.
oov_tokenintFallback token used for OOV values when inverting.
vocabularyarray-like, tensor, or stringVocabulary terms, supplied as integer data or a text-file path.
vocabulary_dtypestringInteger dtype of vocabulary terms.
idf_weightsarray-like or NoneOptional inverse-document-frequency weights for tf_idf output.
invertboolWhether to reverse indices into vocabulary tokens.
output_modestringOutput representation: int, one_hot, multi_hot, count, or tf_idf.
sparseboolWhether to return sparse output.
pad_to_max_tokensboolWhether to pad output to max_tokens.
oov_methodstringOOV hashing method.

Returns

A dense or sparse tensor containing integer indices or the selected categorical encoding.

Key points

  • Use the layer to convert arbitrary integer features into vocabulary indices or encoded categorical representations.
  • Initialize with a vocabulary for fixed mappings, or call adapt() on representative integer data to learn one.
  • Even non-contiguous or unbounded input integers are represented by compact index values.
  • Choose integer indices, one-hot-style encodings, counts, or TF-IDF depending on the model input required.
  • max_tokens limits the learned or padded vocabulary capacity.
  • The vocabulary may be provided as integer data or loaded from a text-file path.

Common mistakes

  • Non-TensorFlow compiled graphs cannot contain this layer, although eager execution remains supported across backends.
  • Use max_tokens only while adapting the vocabulary or when padding the output vocabulary to a fixed size.

Example: Map integer tokens

import keras
import numpy as np

lookup = keras.layers.IntegerLookup(vocabulary=[10, 20, 30])
print(lookup(np.array([20, 99, 10])).numpy())

Creates a lookup table from integer vocabulary terms and maps known and unknown values to integer indices.

When to use it

Compact integer indexing

Preparing integer IDs for an embedding layer

Categorical feature encoding

Converting integer features into model-ready categorical vectors

Lookup inversion

Reversing encoded indices to vocabulary values

Interview questions

QWhat problem does IntegerLookup solve?

IntegerLookup maps arbitrary integer tokens to contiguous indices using a table-backed vocabulary. The vocabulary can be supplied during construction or learned by calling adapt(), which analyzes token frequencies.

Follow-up: How are unseen values handled? They are assigned to one of the configured OOV indices; num_oov_indices controls how many such buckets exist.

QHow does output_mode change the result?

The default output_mode is int, so each input becomes its vocabulary index. multi_hot, count, and tf_idf instead produce vectors whose dimensions represent vocabulary entries.

Follow-up: What additional output mode is documented? one_hot is also accepted, and sparse=True can request sparse output where supported.

QHow are masking, OOV values, and inversion related?

mask_token can reserve a masking value, while oov_token specifies the token returned when invert=True maps OOV indices back to integer tokens. The default oov_token is -1.

Follow-up: When would you use invert=True? Use it to reverse index lookup and recover vocabulary tokens, while OOV indices map to the configured fallback token.

  • keras.layers.StringLookup
  • keras.layers.CategoryEncoding
  • keras.layers.Embedding

Source: IntegerLookup layer. Checked 2026-10-15.