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

Keras · Class

layers.StringLookup

StringLookup maps categorical strings to vocabulary indices or encoded vectors. Supply a vocabulary directly or learn one with adapt().

How it works

StringLookup converts arbitrary string values into integer or encoded representations using a vocabulary table. Provide the vocabulary when constructing the layer or learn it with adapt(); the layer does not tokenize or otherwise transform strings.

Syntax

keras.layers.StringLookup(max_tokens=None, num_oov_indices=1, mask_token=None, oov_token="[UNK]", vocabulary=None, idf_weights=None, invert=False, output_mode="int", pad_to_max_tokens=False, sparse=False, encoding="utf-8", name=None, salt=None, **kwargs)

Parameters

NameTypeDescription
max_tokensint | NoneMaximum vocabulary size; relevant when adapting or padding to a fixed token count.
num_oov_indicesintNumber of reserved out-of-vocabulary indices.
mask_tokenstr | NoneOptional value reserved for masking.
oov_tokenstrToken returned for unknown indices when invert is true.
vocabularyarray of strings | str | NoneVocabulary terms or a path to a vocabulary text file.
idf_weightsarray | NoneOptional inverse-document-frequency weights for tf_idf output.
invertboolWhether to map indices back to strings.
output_modestrEncoding mode for the layer output.
pad_to_max_tokensboolWhether to pad the output to max_tokens.
sparseboolWhether to return sparse encoded output.
encodingstrString encoding used by the layer.
namestr | NoneLayer name.

Returns

A tensor of integer indices or an encoded representation determined by output_mode; with invert=True, it maps indices back to strings.

Key points

  • Use the layer to convert categorical string values into indices or encoded feature vectors.
  • Lookup is table-based, so each recognized string corresponds to an entry in the layer vocabulary.
  • The default integer mode emits one vocabulary index per input string.
  • Choose vector-oriented modes for multi-hot membership, frequency counts, or TF-IDF features.
  • The output_mode argument selects between scalar indices and categorical or statistical encodings.

Common mistakes

  • StringLookup handles categorical or already-tokenized strings; it does not split text into tokens.
  • A vocabulary is required either at construction time or through adapt() before the layer is used.
  • Use max_tokens only while adapting or when padding encoded outputs to a fixed vocabulary size.

Example: Map strings to indices

import keras

lookup = keras.layers.StringLookup(
    vocabulary=["cat", "dog"],
    mask_token=None,
    num_oov_indices=1,
)
print(lookup(["dog", "bird"]).numpy())

Creates a lookup with a fixed vocabulary, then converts known and unknown strings to integer indices.

When to use it

Encode string-valued features before embeddings or dense layers.

Categorical model inputs

Convert string labels into integer targets for training.

Label preprocessing

Create one-hot, multi-hot, count, or TF-IDF representations from categorical strings.

Feature vector generation

Interview questions

QWhat does StringLookup do?

StringLookup uses a table-backed vocabulary to turn strings into indices. With output_mode="int", each input receives its vocabulary index; other modes produce encoded arrays.

Follow-up: How would you handle unseen values? Configure num_oov_indices to reserve one or more out-of-vocabulary indices, or set it to zero when unknown values are not expected.

QHow is the vocabulary established?

The layer can receive vocabulary as a string collection or a text-file path. Alternatively, call adapt() to learn the vocabulary from data.

Follow-up: When is a fixed vocabulary preferable? It makes preprocessing deterministic across training and serving and avoids relearning category order.

QWhich output modes are available?

The supported output modes are int, one_hot, multi_hot, count, and tf_idf. The integer mode returns indices, while the other modes encode vocabulary membership or counts in arrays.

Follow-up: What preprocessing is outside this layer's scope? Token splitting and string transformation must happen before StringLookup.

  • IntegerLookup
  • TextVectorization
  • FeatureSpace

Source: StringLookup layer - Keras. Checked 2026-10-15.