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

TensorFlow · Function

tf.one_hot

Returns a one-hot tensor by expanding each integer index into a vector of length depth. The selected position uses on_value and all other positions use off_value.

How it works

tf.one_hot converts each integer in indices into a vector whose length is depth. It inserts a new dimension at axis, using on_value where an index selects a position and off_value everywhere else; with the default axis, that dimension is appended.

Syntax

tf.one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None)

Parameters

NameTypeDescription
indicesTensorTensor containing integer indices to encode.
depthscalarScalar specifying the size of the one-hot dimension.
on_valuescalarValue placed where an index selects a position; defaults to 1.
off_valuescalarValue placed where an index does not select a position; defaults to 0.
axisintDimension at which to insert the one-hot axis; defaults to -1.
dtypedata typeOutput tensor data type.
namestringOptional operation name.

Returns

The one-hot tensor.

Key points

  • The function returns a tensor containing one-hot encodings of the supplied indices.
  • A rank-N indices tensor becomes rank N+1 after encoding.
  • axis determines where the added one-hot dimension is placed.
  • Leaving axis unset appends the encoding dimension to the output.
  • depth specifies the length of each encoded vector.
  • Each selected location receives on_value, while unselected locations receive off_value.

Common mistakes

  • Ensure on_value and off_value use the same data type; when dtype is supplied, both must match it as well.
  • For RaggedTensor inputs, axis must be positive and refer to a valid dimension.
  • For non-numeric outputs such as strings or booleans, explicitly provide both on_value and off_value.

Example: Encode integer indices

import tensorflow as tf

indices = tf.constant([0, 2, 1])
encoded = tf.one_hot(indices, depth=3)
print(encoded)

Creates one length-three vector per index, placing the default on-value at the indexed position and the default off-value elsewhere.

When to use it

Categorical features

Convert class IDs into categorical feature vectors before feeding them to a model.

Vocabulary preprocessing

Transform integerized vocabulary values into fixed-width representations for training.

Axis-specific encoding

Control the encoding dimension when integrating with tensors whose feature axis is not last.

Interview questions

QWhat does tf.one_hot do?

tf.one_hot maps integer indices to vectors of length depth. The selected position receives on_value and every other position receives off_value; these default to 1 and 0.

Follow-up: What shape does it produce for rank-N input? It produces rank N+1, inserting the new dimension at axis, or appending it when axis is left at its default.

QHow do depth and input rank affect the output?

The depth argument defines the size of the one-hot dimension. An input scalar produces a vector of length depth, while a rank-N input gains one additional dimension.

Follow-up: What happens if an index is outside the represented range? The documentation does not specify an out-of-range behavior on this page, so code should ensure indices are valid for the chosen depth.

QWhich arguments control layout and values?

axis chooses where the new one-hot dimension is inserted, with -1 meaning the new inner-most dimension. on_value and off_value control the selected and unselected values, and dtype controls the output type.

Follow-up: How would you create a boolean encoding? Supply boolean on_value and off_value explicitly, because non-numeric output types require both values.

  • tf.keras.ops.one_hot
  • tf.keras.layers.CategoryEncoding
  • tf.sparse.SparseTensor

Source: One Hot. Checked 2026-10-15.