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

Keras · Class

layers.Softmax

A Keras layer that converts logits into normalized softmax values along one or more selected axes. It supports an optional broadcastable mask for excluding positions.

How it works

Softmax normalizes logits along the configured axis. It subtracts the maximum value for numerical stability, exponentiates the shifted values, and divides each by their sum; an optional mask can exclude selected positions before normalization.

Syntax

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

Parameters

NameTypeDescription
axisint | list[int]Integer or list of integers specifying the axis or axes used for softmax normalization.
**kwargskeyword argumentsAdditional base-layer keyword arguments, including options such as name and dtype.
inputstensorInput logits to the layer.
maskboolean tensor | NoneOptional boolean mask broadcastable to inputs; 1 keeps a position and 0 masks it.

Returns

Softmaxed output with the same shape as inputs.

Key points

  • keras.layers.Softmax is a layer form of the softmax activation.
  • The implementation first shifts inputs by subtracting the maximum value, which improves numerical stability before exponentiation.
  • Each shifted exponential is divided by the sum of shifted exponentials along the selected axis.
  • The axis setting determines the dimension or dimensions over which normalization occurs.
  • Calling the layer preserves the input tensor's shape while changing its values.

Common mistakes

  • A mask must be broadcastable: every dimension must be either 1 or equal to the corresponding input dimension, never larger.
  • Mask values use 1 to retain an input and 0 to suppress it.

Example: Normalize logits into probabilities

import keras
import numpy as np

layer = keras.layers.Softmax(axis=-1)
logits = np.array([[1.0, 2.0, 1.0]])
print(layer(logits).numpy())

Creates a softmax layer and normalizes each row of the logits into class probabilities.

When to use it

Classification probabilities

Use as the final transformation for single-label multi-class predictions.

Axis-specific score normalization

Normalize scores across a selected feature, spatial, or sequence dimension.

Masked distributions

Exclude padding or invalid positions before computing a distribution.

Interview questions

QHow does the Softmax layer transform its input?

Softmax converts logits into normalized values by exponentiating values after subtracting the maximum, then dividing by their sum. The normalization is performed along the configured axis.

Follow-up: What does the layer return shape-wise? It returns a tensor with the same shape as its input.

QWhat does the axis argument control?

The default axis is -1, so normalization is applied along the final dimension. The axis may also be an integer list when normalization must cover multiple dimensions.

Follow-up: How would you normalize channels rather than the final dimension? Pass the desired channel axis explicitly when constructing the layer.

QHow are masked positions handled?

The call accepts an optional boolean mask that is broadcastable to the inputs. A mask keeps positions marked with 1 and masks positions marked with 0.

Follow-up: What constraint must the mask satisfy? Each mask dimension must be 1 or match the corresponding input dimension, and cannot be larger.

  • keras.activations.softmax
  • keras.layers.Activation
  • keras.layers.Dense

Source: Softmax layer. Checked 2026-10-15.