Encode string-valued features before embeddings or dense layers.
Categorical model inputs
Keras · Class
StringLookup maps categorical strings to vocabulary indices or encoded vectors. Supply a vocabulary directly or learn one with adapt().
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.
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)| Name | Type | Description |
|---|---|---|
max_tokens | int | None | Maximum vocabulary size; relevant when adapting or padding to a fixed token count. |
num_oov_indices | int | Number of reserved out-of-vocabulary indices. |
mask_token | str | None | Optional value reserved for masking. |
oov_token | str | Token returned for unknown indices when invert is true. |
vocabulary | array of strings | str | None | Vocabulary terms or a path to a vocabulary text file. |
idf_weights | array | None | Optional inverse-document-frequency weights for tf_idf output. |
invert | bool | Whether to map indices back to strings. |
output_mode | str | Encoding mode for the layer output. |
pad_to_max_tokens | bool | Whether to pad the output to max_tokens. |
sparse | bool | Whether to return sparse encoded output. |
encoding | str | String encoding used by the layer. |
name | str | None | Layer name. |
A tensor of integer indices or an encoded representation determined by output_mode; with invert=True, it maps indices back to strings.
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.
Categorical model inputs
Label preprocessing
Feature vector generation
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.
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.
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.
IntegerLookupTextVectorizationFeatureSpace