Encode fixed-range categorical features
A feature contains integer category IDs within a fixed, known range.
Keras · Class
CategoryEncoding turns integer features into one-hot, multi-hot, or count vectors when the number of supported tokens is known. It can produce dense or sparse outputs.
CategoryEncoding converts integer-valued inputs into dense or sparse categorical representations when the supported token count is known. Choose one_hot for individual values, multi_hot for token presence per sample, or count to record occurrences.
keras.layers.CategoryEncoding(num_tokens=None, output_mode="multi_hot", sparse=False, **kwargs)| Name | Type | Description |
|---|---|---|
num_tokens | int or None | Total number of supported tokens; valid inputs must be integers from 0 through num_tokens minus one. |
output_mode | str | Output encoding mode: one_hot, multi_hot, or count. |
sparse | bool | Whether to return a sparse tensor when the backend supports sparse tensors. |
A dense or sparse categorical representation of the integer inputs.
import keras
layer = keras.layers.CategoryEncoding(
num_tokens=4,
output_mode="one_hot",
)
encoded = layer([0, 2, 1])
print(encoded)Creates a four-category one-hot encoder and applies it to integer token IDs.
A feature contains integer category IDs within a fixed, known range.
A model needs one binary vector indicating which categories occur in each sample.
Repeated category IDs carry meaningful frequency information.
CategoryEncoding accepts integer features and maps them into a representation whose width is num_tokens. Its output can be dense or sparse, depending on sparse.
Follow-up: What should be used when the integer vocabulary is not known in advance? Use IntegerLookup, which is intended to handle an unknown input vocabulary.
one_hot encodes individual input elements, placing a one at the corresponding token index. If the final input dimension has size one, encoding occurs on that dimension; otherwise a new encoded dimension is appended.
Follow-up: How would you encode repeated values within each sample? Use multi_hot for presence, or count when multiplicity must be retained.
The layer requires num_tokens to define the supported token range and output width. With multi_hot, each sample receives a vector marking which vocabulary terms occur; count uses occurrence totals instead of binary indicators.
Follow-up: What does output_mode count return for a token appearing three times? Its corresponding position contains the count three, subject to the input shape being interpreted as samples.
keras.layers.IntegerLookupCategorical features preprocessing layers