How convert output tensor to one-hot tensor?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Turning a model output tensor into a one-hot tensor is a common classification step. The usual approach is simple: find the winning class index, then encode that index as a one-hot vector. The important detail is that this only fits single-label classification, not every prediction problem.
Start With The Prediction Meaning
Before writing code, decide what the tensor represents. If each row means "exactly one class is correct," then argmax followed by one-hot encoding is appropriate. If several classes can be correct at the same time, forcing one winner is wrong.
For a standard multi-class output shaped like [batch_size, num_classes], the class dimension is usually axis=1.
This returns the index of the largest value in each row. That index is the class prediction.
Convert Indices To One-Hot
Once you have class indices, tf.one_hot creates indicator vectors with the correct depth.
The output is:
This is the normal post-processing pattern for classification predictions.
Logits And Probabilities Are Treated The Same
People often ask whether the model output must be softmax probabilities first. It does not. argmax only cares about which class is largest, so raw logits and normalized probabilities produce the same winner.
Both calls return the same class index. So if all you need is one-hot predicted classes, applying softmax first is optional.
Handle Batches And Sequence Outputs Correctly
The main source of bugs is using the wrong axis. For sequence models or token classifiers, the class dimension is often the last one rather than the second one.
Here the input shape is [batch, time, classes]. The result after argmax is [batch, time], and after tf.one_hot it becomes [batch, time, classes].
A practical rule is: run argmax over the class axis, then use the number of classes as the one-hot depth.
Multi-Label Outputs Need Thresholding Instead
If the model can predict several classes as true at once, argmax discards valid information. In that case, apply a threshold per class.
This output is not one-hot in the strict sense because more than one position can be 1. That is correct for multi-label problems.
Use The Same Idea In Keras Prediction Code
In a Keras workflow, you often predict on a batch and convert the result immediately.
This is useful when downstream logic expects hard class assignments instead of floating-point scores.
PyTorch Has The Same Pattern
The same reasoning applies outside TensorFlow.
So the concept is framework-independent: pick class indices first, then encode them.
Common Pitfalls
- Using
argmaxon a multi-label output where several classes may be correct. - Choosing the wrong axis for tensors that have time steps, channels, or other extra dimensions.
- Passing a
depthvalue that does not match the real number of classes. - Expecting softmax to be required before
argmaxfor single-label prediction. - Converting to one-hot too early when later code still needs confidence scores.
Summary
- For single-label classification, use
argmaxto get class indices andtf.one_hotto encode them. - Raw logits and probabilities produce the same predicted class order.
- The class axis matters more than the tensor rank.
- Multi-label outputs should be thresholded, not collapsed with
argmax. - The same pattern works in TensorFlow, Keras, and PyTorch.

