How do you decode one-hot labels in Tensorflow?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
One-hot encoding converts a categorical label (like class 2 out of 5 classes) into a binary vector ([0, 0, 1, 0, 0]). Decoding reverses this: it takes the one-hot vector and returns the original class index. In TensorFlow, decoding is done with tf.argmax(), which returns the index of the maximum value along a specified axis. For model predictions (probability distributions), tf.argmax() picks the class with the highest predicted probability.
Basic Decoding with tf.argmax
axis=1 means "find the max along columns for each row." Each row represents one sample, and the columns represent classes.
Decoding Model Predictions
Model outputs are probability distributions (from softmax), not clean one-hot vectors:
Mapping Indices to Class Names
Decoding During Training and Evaluation
One-Hot Encoding (The Reverse Operation)
NumPy Equivalent
The NumPy version works identically — np.argmax with axis=1.
Sparse vs Categorical Cross-Entropy
The choice between sparse_categorical_crossentropy and categorical_crossentropy determines whether you need one-hot encoding at all:
For large numbers of classes (e.g., 10,000+ in NLP), sparse labels save significant memory.
Multi-Label Decoding (Multiple Classes Per Sample)
If a sample can belong to multiple classes simultaneously, use thresholding instead of argmax:
argmax only returns one index and is wrong for multi-label classification.
Batch Processing with tf.data
Common Pitfalls
- Wrong axis:
tf.argmax(one_hot, axis=0)returns the max along rows (across samples), not within each sample. Useaxis=1for batch data where each row is a sample. - Using argmax for multi-label classification:
argmaxreturns exactly one index. If samples can belong to multiple classes, use sigmoid activation with thresholding instead. - Ties in one-hot vectors: If two values are equal (e.g.,
[0.5, 0.5, 0]),argmaxreturns the first occurrence. This is nondeterministic for your purposes — consider whether ties matter in your application. - Forgetting to cast types:
tf.argmaxreturnsint64by default. If your labels areint32, cast withtf.cast(decoded, tf.int32)before comparison. - Decoding logits vs probabilities:
argmaxworks the same on raw logits (pre-softmax) and probabilities (post-softmax) because softmax preserves the relative ordering. You do not need to apply softmax beforeargmax.
Summary
- Use
tf.argmax(one_hot, axis=1)to decode one-hot labels to class indices - Works on both clean one-hot vectors and softmax probability distributions
- Use
tf.gather(class_names, indices)to map indices to human-readable names - Use
sparse_categorical_crossentropyto avoid one-hot encoding entirely - For multi-label classification, use sigmoid + thresholding instead of
argmax argmaxon logits gives the same result asargmaxon probabilities — softmax is not needed for decoding
Related reading
- How do you decode one-hot labels in Tensorflow?
- How do you edit an existing Tensorboard Training `Loss` summary?
- How do you edit an existing Tensorboard Training `Loss` summary?
- How do you get the name of the tensorflow output nodes in a Keras Model?
- How do you draw a line using the weight vector in a Linear Perceptron?
- How do you install modules within sagemaker training jobs?
- How do you get the magnitude of a vector in Numpy?
- How do you read Tensorboard files programmatically?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.