Multiclass classification
Use as the final layer for a multiclass classifier when model outputs should be interpretable as class probabilities.
TensorFlow · Class
A Keras layer that converts logits into normalized softmax probabilities. It defaults to the last axis and can optionally ignore masked positions.
The layer applies numerically stable softmax normalization along the configured axis. It subtracts the maximum value before exponentiation, then divides each exponentiated value by the sum along that axis; an optional mask can exclude selected positions.
tf.keras.layers.Softmax(axis=-1, **kwargs)| Name | Type | Description |
|---|---|---|
axis | int or list[int] | Integer or list of integers specifying the axes over which softmax normalization is performed. |
**kwargs | keyword arguments | Additional Keras layer keyword arguments, including options such as name and dtype. |
inputs | Tensor | Input logits to normalize. |
mask | boolean Tensor or None | Boolean mask aligned with inputs; retained positions are marked true or 1. |
Softmaxed output with the same shape as inputs.
import numpy as np
import tensorflow as tf
layer = tf.keras.layers.Softmax(axis=-1)
logits = np.array([[1.0, 2.0, 1.0]], dtype=np.float32)
probabilities = layer(logits)
print(probabilities.numpy())Creates a layer that normalizes each row of logits into class probabilities.
Use as the final layer for a multiclass classifier when model outputs should be interpretable as class probabilities.
Normalize scores across a selected tensor dimension inside a model.
Exclude padding or unavailable positions from the probability distribution.
The layer converts logits into normalized values by exponentiating shifted logits and dividing by their sum along the selected axis. Each normalization group therefore represents a probability distribution.
Follow-up: How would you normalize over a different dimension? Set axis to the desired integer or list of integers when constructing the layer.
By default, axis is -1, so normalization occurs over the last dimension. The layer returns an output with the same shape as its inputs.
Follow-up: What happens when axis is a list? The layer normalizes jointly across all listed axes.
The call accepts inputs, interpreted as logits, and an optional boolean mask with the same shape. Mask values indicate which positions remain active during normalization.
Follow-up: Why is the mask shape requirement useful? It lets each input position be aligned unambiguously with the corresponding normalization element.
tf.keras.activations.softmaxtf.nn.softmaxtf.keras.activations.log_softmax