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

Keras · Class

layers.CategoryEncoding

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.

How it works

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.

Syntax

keras.layers.CategoryEncoding(num_tokens=None, output_mode="multi_hot", sparse=False, **kwargs)

Parameters

NameTypeDescription
num_tokensint or NoneTotal number of supported tokens; valid inputs must be integers from 0 through num_tokens minus one.
output_modestrOutput encoding mode: one_hot, multi_hot, or count.
sparseboolWhether to return a sparse tensor when the backend supports sparse tensors.

Returns

A dense or sparse categorical representation of the integer inputs.

Key points

  • Use the layer for integer categorical features when the complete token range is already known.
  • num_tokens determines both the accepted category range and the size of categorical outputs.
  • one_hot produces a vector with a single active position for each encoded element.
  • multi_hot produces one binary vector per sample that records category presence without recording frequency.
  • count preserves how often each token occurs in a sample rather than reducing repeated occurrences to one.
  • The layer accepts integer inputs and can return either a dense representation or a sparse tensor.

Common mistakes

  • Inputs outside the interval from zero through num_tokens minus one cause an error; use IntegerLookup when the input range is not known.
  • The sparse option only provides a sparse result on backends that support sparse tensors.

Example: One-hot encode integer categories

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.

When to use it

Encode fixed-range categorical features

A feature contains integer category IDs within a fixed, known range.

Represent category presence

A model needs one binary vector indicating which categories occur in each sample.

Represent category counts

Repeated category IDs carry meaningful frequency information.

Interview questions

QWhat problem does CategoryEncoding solve?

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.

QHow does one_hot differ from multi_hot?

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.

QWhen should output_mode be count?

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.IntegerLookup
  • Categorical features preprocessing layers

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