Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Softmax for Multi-Class Classification

Introduction

In our last lesson, we successfully navigated the forward pass, propagating an input through the network's layers to generate a vector of raw output values, which we call logits. This is a significant step, but it leaves us with a critical question: how do we interpret these raw, unbounded numbers, especially in the context of classification? If our goal is to assign an input to one of several categories (e.g., classifying a handwritten digit as 0 through 9), we need a principled way to convert these logits into a set of probabilities.

This lesson introduces the softmax function, the standard mechanism in neural networks for this very purpose. We will define the function, explore its use in multi-class classification, and derive it from first principles. In line with your goal of bridging classical and modern concepts, we'll see how softmax elegantly generalizes the sigmoid function used in binary logistic regression to the multi-class setting. We will also investigate its key mathematical properties, which are foundational for understanding how classification networks are trained.

From Binary to Multi-Class Classification

Let's begin by revisiting a simpler case: binary classification. Here, we typically have a single output neuron producing a logit . We apply the sigmoid function, , to map this logit to a probability . If is the probability of class 1, the probability of class 0 is simply .

Now, consider a problem with classes. A single output neuron is no longer sufficient.

Question: What is the most direct way to adapt a network's output layer to handle classes? And what two properties must the final output vector satisfy to be considered a valid probability distribution?

Answer The most straightforward approach is to have $K$ output neurons, one for each class. Each neuron $k$ produces its own logit, $z_k$. The final output of the network is a vector of these logits, $\mathbf{z} = [z_1, z_2, ..., z_K]^T$.

For this output vector to represent a valid probability distribution, it must satisfy two conditions:

  1. Each element must be a valid probability: for all .
  2. The probabilities for all classes must sum to one: .

Our challenge, then, is to find a function that takes the vector of arbitrary real-valued logits and transforms it into a probability vector that meets these two conditions. This is precisely the role of the softmax function.

The image below provides a great schematic for this conceptual jump. On the top, you see binary logistic regression using a sigmoid function for one-of-two classes. On the bottom, you see softmax regression, which handles one-of-K classes.

A comparison of the architecture for binary logistic regression (top) which uses a sigmoid function, and multi-class softmax regression (bottom) which uses the softmax function.

Deriving the Softmax Function from First Principles

Let's build the softmax function from the ground up. We start with our vector of logits, , where each can be any real number.

A simple, naive attempt to create probabilities might be to normalize by the sum: . This satisfies the "sum-to-one" property. However, what is the critical flaw in this approach, given that logits can be negative? And what is a common mathematical operation that can map any real number to a strictly positive one?

The flaw is that if any is negative, we would get a negative "probability," which is nonsensical. To solve this, we need to ensure all our scores are positive before normalization. The exponential function, , is a perfect candidate for this. It maps any real number to a positive number and has the added benefit of preserving the order of the original logits (if , then ).

By first exponentiating each logit and then normalizing by the sum of these exponentiated values, we arrive at the softmax function:

Each resulting is guaranteed to be in , and their sum is guaranteed to be 1, fulfilling our requirements for a probability distribution. The name "softmax" comes from the fact that the exponential function strongly amplifies the largest logit, making its corresponding probability close to 1, while suppressing the others. It's a "soft," or differentiable, approximation of the non-differentiable argmax function.

The following article provides a clear, narrative-driven derivation of this exact logic.

Deriving the Softmax from First Principles - will wolf

This article by Will Wolf builds the softmax function from the ground up, starting from the need to convert model scores into a valid probability distribution.

Read from the beginning up to and including the section "This is the softmax function." Pay close attention to the reasoning for why simple normalization fails and why exponentiation is introduced.

Properties and Visual Intuition

To truly understand softmax, it's helpful to visualize its behavior and examine its mathematical properties. The function has several key characteristics that are critical for both its use in modeling and its role in training.

Softmax Function Explained In Depth with 3D Visuals

The video "Softmax Function Explained In Depth" by Elliot Waite provides outstanding visualizations that build a deep intuition for how softmax operates.

Please watch the following segments: Introduction (0:00 - 1:04): This sets up the problem of converting logits to probabilities. Mechanics (1:04 - 2:29): This part visually demonstrates the core "exponentiate and normalize" mechanism. Properties (2:29 - 4:44): This is the most crucial part. Focus on two key properties being discussed: How changing one input logit affects all output probabilities. The shift-invariance property: adding a constant to all inputs does not change the output probabilities.

Let's formalize two of these properties.

1. Shift Invariance

As shown in the video, if we add a constant to every logit, the output of the softmax function remains unchanged. That is, .

Let's prove this algebraically. Let . The new softmax output for class is:

The terms in the numerator and denominator cancel out, leaving us with the original softmax expression.

This property is not just a mathematical curiosity. From a systems engineering perspective, it's vital for numerical stability. If the logits contain very large values, calculating can lead to overflow. By subtracting the maximum logit from all logits before applying the softmax (), we shift the values so the largest is 0, preventing overflow while yielding the exact same result.

2. Equivalence to Sigmoid for K=2

Softmax is a true generalization of the sigmoid. Let's demonstrate that for two classes (), softmax reduces to the sigmoid function. Our logits are .

The softmax probability for class 1 is:

We can divide the numerator and denominator by :

This is equivalent to the sigmoid function applied to the difference in logits, . This perfectly illustrates the "Classical to Modern Bridge": the multi-class softmax gracefully collapses into the familiar binary sigmoid.

For a formal write-up of this property and others, the following resource is excellent.

Logistic Regression: From Binary to Multi-Class

This document formally introduces softmax in the context of multi-class logistic regression.

Please read the following sections: Section 3.1 Softmax for a formal definition. Section 4.2 Equivalence to Sigmoid for the formal proof we just discussed.

The Derivative of Softmax: A Glimpse into Backpropagation

For our next lesson, we will need to compute the gradient of the loss function with respect to the network's parameters. A key step in that process involves finding the derivative of the softmax output with respect to its input logits. This is the Jacobian matrix of the softmax function, where each entry is .

Let's derive this, as it reveals the dynamics of how a change in one logit affects all probabilities. We must consider two cases.

Case 1: (The derivative of a softmax output with respect to its own logit)

We use the quotient rule for where .

Splitting the fraction:

This is identical to the derivative of the sigmoid function, which makes sense given their relationship.

Case 2: (The derivative of a softmax output with respect to another logit)

Rearranging the terms:

This elegant result shows that increasing the logit has a negative effect on all other probabilities , proportional to the magnitude of both probabilities. This interaction is the core of the competitive nature of multi-class classification. The mathematical derivation is also covered clearly in the following video.

Backpropagation for Softmax and Multi-Class Classification | Complete Mathematical Derivation

This video provides a clear, step-by-step mathematical derivation of the softmax derivative.

Watch the segment from 2:12 to 7:34, which derives these two cases in detail. This will be invaluable for our next lesson.

Conclusion

In this lesson, we have formally defined the softmax function and established its critical role in multi-class classification. We have seen that it is not an arbitrary choice, but a principled function that can be derived from the fundamental need to convert unbounded logits into a valid probability distribution.

Key Takeaways:

  • Purpose: The softmax function transforms a vector of real-valued logits into a -dimensional vector of probabilities that sum to 1.
  • Mechanism: It works by exponentiating its inputs to ensure positivity and then normalizing them.
  • Bridge Concept: It is a direct generalization of the sigmoid function; for , softmax is equivalent to the sigmoid.
  • Properties: It is invariant to shifts in the input (crucial for numerical stability) and its derivative has a simple, elegant form ( or ) that captures the competitive relationship between class probabilities.

Preview of the Next Lesson:

Now that our network can produce a vector of probabilities , we can compare this prediction to the true, one-hot encoded label . To do this, we need a loss function. For multi-class classification, the standard is the cross-entropy loss. In our next lesson, we will derive this loss function and then combine its derivative with the softmax derivative we found today to compute the gradient of the loss with respect to the network's logits. This will yield the famous p - y gradient, the starting point for backpropagation in nearly all modern classification architectures.

Can't find a good explanation? Sign up and we'll make it for you

Sign up