Sigmoid and softmax functions: how they work and when to use each
Sigmoid and softmax turn the scores of a classifier into probabilities. The sigmoid turns one score into a probability between 0 and 1. The softmax turns several scores, one per class, into probabilities that sum to 1. This article explains both with formulas and examples, and shows when to use each.
Key points
- The sigmoid function turns one score, which can be any number, into a probability between 0 and 1.[1]
- The softmax function turns a list of scores into probabilities: each lies between 0 and 1, and together they sum to 1.[1]
- The sigmoid answers a yes-or-no question: its output is the probability of yes, and 1 minus the output is the probability of no.[1] The softmax gives one probability per class. These sum to 1, so when one rises, at least one other must fall.[2]
- Use one sigmoid to choose between two classes and the softmax to choose one class out of three or more.[1] When several classes can be correct at the same time, the softmax does not fit, and each class gets its own sigmoid.[3]
- The transformer is the standard architecture for building large language models. It uses the softmax in two places. In attention, the softmax gives every word a weight. At the end of the model, it gives every word of the vocabulary a probability of coming next.[4] [5] [6]
A classifier assigns an input to a class. For a movie review, the classes could be positive and negative.1 To do this, the classifier first computes a score. A score can be any number, from far below zero to far above it.1 A probability, however, must lie between 0 and 1.1
Sigmoid and softmax are two functions that turn scores into probabilities. The sigmoid takes one score. The softmax takes several scores at once, one per class.1 Most examples in this article come from the textbook “Speech and Language Processing” by Jurafsky and Martin. Where a step was calculated for this article, the figure says so.
What is the sigmoid function?
The sigmoid function takes one number and turns it into a number between 0 and 1.1 It is named after its S shape and is also called the logistic function.1
Plotted as a curve, the sigmoid is almost a straight line near a score of 0. Far from 0, the curve flattens: large positive scores give outputs close to 1, and large negative scores give outputs close to 0.1 The output never reaches 0 or 1 exactly.7
With two classes, the output is the probability of one class, and the probability of the other class is 1 minus the output.1 The classifier chooses a class when the probability of that class is more than 0.5.1 The value 0.5 is called the decision boundary.1 The score that goes into the sigmoid is often called a logit.1
- The formula, read “sigma of z”, is the output for the score . is Euler's number, about 2.718.
- The exampleThe textbook gives the output 0.70 for the score 0.833;1 the middle step was calculated for this article. z = 0.833→e−z = 0.435→1 / (1 + 0.435) = 0.70
- The decisionThe review is positive with a probability of 0.70 and negative with a probability of 1 − 0.70 = 0.30.1 Because 0.70 is more than the boundary of 0.5, the classifier decides that the review is positive.
What is the softmax function?
The softmax function takes a list of scores, one per class, and turns them into probabilities. Each probability lies between 0 and 1, and together they sum to 1.1 The softmax is a generalization of the sigmoid to more than two classes;1 the next section shows how. The scores that go into a softmax are also called logits.1
The softmax works in three steps. First, it raises the number to the power of each score. The result is called the exponential of the score. Second, it adds up all the exponentials. Third, it divides each exponential by this sum. The results are the probabilities.1
Like the sigmoid, the softmax pushes values toward 0 or 1. When one score is larger than the others, its probability tends to be close to 1, and the probabilities of the smaller scores are small.1
- Exponential, the exponential of the score , is Euler's number raised to the power . The result is always positive, even for a negative score: −1.5 becomes 0.22. The largest score, 3.2, gets by far the largest exponential.
- SumThe six exponentials add up to 33.23.
- DivisionEach exponential is divided by the sum. The formula of the softmax, below this note, says the same.1 For the score 3.2, the result is 24.53 / 33.23 = 0.74. The six results are the probabilities. In the drawing, they fill the column from 0 to 1 exactly.
Sigmoid and softmax: the differences
Both functions divide a total of 1, but among a different number of answers. The sigmoid divides it between two answers: its output is the probability of yes, and 1 minus the output is the probability of no.1 The softmax takes one score per class and gives one probability per class. These probabilities always sum to 1, so when one rises, at least one other must fall.2
A sigmoid is a softmax with two classes in which one score is fixed at 0.2 Here, the score fixed at 0 is the score for “no”. For the movie review, the softmax of the scores 0.833 and 0 gives 0.70 for yes and 0.30 for no, the same values as the sigmoid. This was calculated for this article.
For the softmax, only the differences between the scores matter: adding the same number to every score does not change its outputs.2 The name “softmax” can be misleading: the function is a softened version of the arg max, not of the maximum.2 The arg max of a list says only which entry is the largest. The softmax gives the largest score the largest probability and gives the other scores smaller probabilities.
| Sigmoid | Softmax | |
|---|---|---|
| Input | One score1 | A list of scores, one per class1 |
| Output | One value between 0 and 1, the probability of yes1 | One value between 0 and 1 for each class1 |
| What sums to 1 | Yes and no1 | All classes1 |
| One output depends on | One score | All scores2 |
| Adding the same number to every score | Changes the output | Changes nothing2 |
| The question it answers | Is it this class or not?1 | Which one of the classes is it?1 |
When to use each function
The choice depends on how many classes can be correct for one input. The softmax assumes that each input belongs to exactly one class.3 Its probabilities sum to 1, so two classes can never both get more than 0.5. When an input can belong to several classes at the same time, the softmax does not fit.3 Each class then gets its own sigmoid, which answers yes or no for that class only.3 1
- Several classes can be correct: one sigmoid per classEach class gets its own sigmoid, and each sigmoid gives its own probability of yes. These probabilities do not have to sum to 1.3 Examples: a picture of a bowl that contains both apples and oranges;3 a text that is about religion, politics, finance and education at once.8 This task is called multilabel classification.8
- Exactly one of three or more classes: softmaxOnly one class is correct; an input cannot belong to several classes.1 Examples: a review that is positive, negative or neutral;1 a picture that shows an orange, an apple or a pear.8 This task is called multiclass classification.8
- Exactly one of two classes: one sigmoidThe sigmoid gives the probability of one class, and the probability of the other class is 1 minus this value.1 A softmax with two classes would give the same two values, so one sigmoid is enough. Examples: a review that is positive or negative;1 an email that is spam or not spam. For such an email, an output of 0.932 means that it is spam with a probability of 93.2%.7
Where and why transformers use the softmax
The transformer is the standard architecture for building large language models,4 which predict the next word of a text.1 It uses the softmax inside attention and at the end of the model.6 In both places, the model needs to turn a list of scores into numbers that sum to 1.
In attention, the model combines each word with information from the words around it.4 To do this for one word, it gives every word of the input a score.5 The softmax turns these scores into weights that are all positive and sum to 1.5 4 A weight says how much its word contributes to the output of attention.4
At the end of the model, choosing the next word is a classification in which every word of the vocabulary is a class.1 Exactly one word comes next, so the softmax fits, as in the decision path. The model first calculates one score, a logit, for every word of the vocabulary. It does this with a linear layer, a multiplication by numbers that the model learned during training. The softmax then turns the logits into probabilities.6 4
This formula comes from the paper “Attention Is All You Need”, which proposed the transformer.6 Every word has three vectors (lists of numbers): a query, a key and a value.5 , and are the tables (matrices) that hold these vectors for all words. The score between two words is the dot product of the first word's query and the second word's key. A word also gets a score with itself.5 6 To calculate a dot product, multiply the two vectors entry by entry and add up the products. writes all these dot products at once; the means that the rows and columns of are swapped. The softmax turns the scores into weights, and multiplying by adds up the value vectors, each multiplied by its weight.5 In this formula, means the keys, not the number of scores.
is the number of entries in a query vector or a key vector; it is 64 in the paper.6 The scores are divided by because the authors suspected that, with a large , the dot products grow large and push the softmax into regions where its gradients are extremely small.6 The gradient guides the learning of the model. Where a function is almost flat, its gradient is very small.2
is the list of logits, one for each word of the vocabulary, and is the list of probabilities.4 In the formula of the softmax above, the list of scores was called . The example text is “So long and thanks for”. Of the four words that could follow, “all” has the largest logit and gets the largest probability, 0.44.4
Sources
- Logistic Regression and Text Classification (chapter 4) Jurafsky and Martin, Speech and Language Processing
- Deep Feedforward Networks (chapter 6) Goodfellow, Bengio and Courville, Deep Learning
- Neural networks: Multi-class classification Google, Machine Learning Crash Course
- Transformers and Pretraining (chapter 7) Jurafsky and Martin, Speech and Language Processing
- The Illustrated Transformer Jay Alammar
- Attention Is All You Need Vaswani et al., arXiv
- Logistic regression: Calculating a probability with the sigmoid function Google, Machine Learning Crash Course
- Multiclass and multioutput algorithms scikit-learn documentation
Written by Rahmatullah Musawi with the help of an AI assistant.