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

TensorFlow · Class

layers.Softmax

A Keras layer that converts logits into normalized softmax probabilities. It defaults to the last axis and can optionally ignore masked positions.

How it works

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.

Syntax

tf.keras.layers.Softmax(axis=-1, **kwargs)

Parameters

NameTypeDescription
axisint or list[int]Integer or list of integers specifying the axes over which softmax normalization is performed.
**kwargskeyword argumentsAdditional Keras layer keyword arguments, including options such as name and dtype.
inputsTensorInput logits to normalize.
maskboolean Tensor or NoneBoolean mask aligned with inputs; retained positions are marked true or 1.

Returns

Softmaxed output with the same shape as inputs.

Key points

  • Softmax is provided as a Keras activation layer, so it can be inserted directly into a model or called on tensors.
  • The default axis is -1, which applies normalization along the final input dimension.
  • The axis argument accepts either one integer or multiple integers for selecting normalization dimensions.
  • Applying the layer preserves the complete input shape while changing values into normalized outputs.
  • The call supports a boolean mask aligned element-for-element with the input tensor.

Common mistakes

  • When supplying a mask, use the same shape as the inputs and treat true or 1 values as retained positions; masked positions are excluded from the normalization.

Example: Apply softmax to class logits

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.

When to use it

Multiclass classification

Use as the final layer for a multiclass classifier when model outputs should be interpretable as class probabilities.

Axis-specific normalization

Normalize scores across a selected tensor dimension inside a model.

Masked predictions

Exclude padding or unavailable positions from the probability distribution.

Interview questions

QWhat does a Softmax layer compute?

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.

QWhat is the default axis and output shape?

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.

QHow does masking work?

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.softmax
  • tf.nn.softmax
  • tf.keras.activations.log_softmax

Source: tf.keras.layers.Softmax. Checked 2026-10-15.