activation functions
neural networks
machine learning
sigmoid function
softmax function

softmax and sigmoid function for the output layer

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Softmax and sigmoid functions are two of the most commonly used activation functions for the output layer in neural networks. Understanding their mechanics and appropriate applications is critical for designing effective models in machine learning and artificial intelligence. This article explores the theoretical underpinnings, practical contexts, and nuances of the softmax and sigmoid functions.

Softmax Function

Definition and Formula

The softmax function is used in the output layer of neural networks to convert logits (raw prediction scores) into probabilities that sum to one. It is particularly helpful in multi-class classification problems.

The softmax function is defined as:

σ(z_i)=ez_i_j=1Kez_j\sigma(z\_i) = \frac{e^{z\_i}}{\sum\_{j=1}^{K} e^{z\_j}}

Here, ziz_i is the logit corresponding to the ithi^{th} class, and KK is the total number of classes. This transformation ensures that the sum of the output probabilities equals one, making it suitable for interpreting the output as class probabilities.

Properties

Range: The result of the softmax function is a vector where each element is in the range (0, 1). • Sum to One: The elements of the softmax output vector sum to one, which is a property that allows for interpreting the outputs as probabilities. • Sensitive to Magnitude: The softmax function is sensitive to the magnitude of the input logits, and it accentuates the differences between them, amplifying the highest value.

Example

For a given input vector z=[2.0,1.0,0.1]z = [2.0, 1.0, 0.1], the softmax probabilities are computed as:

σ(z)[0.659,0.242,0.099]\sigma(z) \approx [0.659, 0.242, 0.099]

Which implies that the model is most likely predicting the first class.

Use Cases

Multi-class Classification: Softmax is typically used when the neural network must choose one class from three or more possible classes. • Limitations: For binary classification, using softmax might be overkill and sigmoid is preferred.

Sigmoid Function

Definition and Formula

The sigmoid function, also known as the logistic function, is an S-shaped curve that maps any real-valued number into the range (0, 1). It is expressed as:

σ(x)=11+ex\sigma(x) = \frac{1}{1 + e^{-x}}

Properties

Range: Output is between 0 and 1. • Output Interpretation: Particularly useful for binary classification tasks, where the output can be interpreted as a probability. • Non-linearity: Introduces non-linearity into the model, enabling the learning of complex patterns.

Example

Consider a single output neuron that receives an input z=2.5z = 2.5. The sigmoid function converts this input into a probability:

σ(2.5)0.924\sigma(2.5) \approx 0.924

This output can be interpreted as a 92.4% probability that the instance belongs to the positive class.

Use Cases

Binary Classification: The sigmoid function is ideal for scenarios where each instance can belong to only one of two classes. • Limitations: In cases involving multiple classes, especially using cross-entropy loss, softmax is favored over sigmoid.

Comparative Summary

The table below offers a side-by-side comparison of the softmax and sigmoid functions:

AttributeSoftmaxSigmoid
Output Range(0, 1)(0, 1)
Sum of OutputsEquals 1Does not necessarily sum to 1
Use CaseMulti-class classificationBinary classification
ApplicabilityUse when multiple classes are presentUse for two-class scenarios
Mathematical ComplexityInvolves exponentiation and divisionInvolves exponentiation
InterpretationOutputs as probabilities across classesOutputs can be interpreted as probabilities
SensitivityAmplifies differences in larger logitsHandles individual input ranges

Conclusion

Understanding when to use softmax vs. sigmoid is a critical aspect of effective neural network design. While the softmax function is the natural choice for multi-class problems where probabilities need to sum to one, the sigmoid function is more appropriate for binary classification tasks. Proper use of these functions impacts the interpretability and accuracy of the model's predictions, underscoring their importance in modern AI applications.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.