softmax function
The softmax function, also known as softargmax or normalized exponential function, converts a tuple of real numbers into a probability distribution over possible outcomes. It is a generalization of the logistic function to multiple dimensions, and is used in multinomial logistic regression. The softmax function is often used as the last activation function of a neural network to normalize the output of a network to a probability distribution over predicted output classes.
The softmax function, also known as softargmax or normalized exponential function, converts a tuple of real numbers into a probability distribution over possible outcomes. It is a generalization of the logistic function to multiple dimensions, and is used in multinomial logistic regression. The softmax function is often used as the last activation function of a neural network to normalize the output of a network to a probability distribution over predicted output classes. Formally, the standard (unit) softmax function \sigma: \R^K \to (0, 1)^K , where , takes a tuple \mathbf{z} = (z_1, \dotsc, z_K) \in \R^K and computes each component of vector \sigma(\mathbf{z}) \in (0, 1)^K with \sigma(\mathbf{z})_i = \frac{e^{z_i}}{\sum_{j=1}^K e^{z_j}}\,. In words, the softmax applies the standard exponential function to each element z_i of the input tuple \mathbf z (consisting of K real numbers), and normalizes these values by dividing by the sum of all these exponentials. The normalization ensures that the sum of the components of the output vector \sigma(\mathbf z) is 1. The term "softmax" derives from the amplifying effects of the exponential on any maxima in the input tuple. For example, the standard softmax of (1,2,8) is approximately (0.001,0.002,0.997) , which amounts to assigning almost all of the total unit weight in the result to the position of the tuple's maximal element (of 8). In general, instead of a different base can be used. As above, if then larger input components will result in larger output probabilities, and increasing the value of will create probability distributions that are more concentrated around the positions of the largest input values. Conversely, if then smaller input components will result in larger output probabilities, and decreasing the value of will create probability distributions that are more concentrated around the positions of the smallest input values. Writing b = e^\beta or b = e^{-\beta} (for real ) yields the expressions: \sigma(\mathbf{z})_i = \frac{e^{\beta z_i}}{\sum_{j=1}^K e^{\beta z_j}} \text{ or } \sigma(\mathbf{z})_i = \frac{e^{-\beta z_i}}{\sum_{j=1}^K e^{-\beta z_j}} \text{ for } i = 1, \dotsc , K . A value proportional to the reciprocal of is sometimes referred to as the temperature: \beta = 1 / kT , where is typically 1 or the Boltzmann constant and is the temperature. A higher temperature results in a more uniform output distribution (i.e. with higher entropy; it is "more random"), while a lower temperature results in a sharper output distribution, usually with one value dominating. In some fields, the base is fixed, corresponding to a fixed scale, while in others the parameter (or ) is varied. The softmax function is a multiple-variable generalization of the logistic function. \operatorname{arg\,max}(z_1,\, \dots,\, z_n) = (y_1,\, \dots,\, y_n) = (0,\, \dots,\, 0,\, 1,\, 0,\, \dots,\, 0), where the output coordinate y_i = 1 if and only if i is the arg max of (z_1, \dots, z_n) , meaning z_i is the unique maximum value of (z_1,\, \dots,\, z_n) . For example, in this encoding \operatorname{arg\,max}(1, 5, 10) = (0, 0, 1), since the third argument is the maximum. This can be generalized to multiple arg max values (multiple equal z_i being the maximum) by dividing the 1 between all max args; formally where is the number of arguments assuming the maximum. For example, \operatorname{arg\,max}(1,\, 5,\, 5) = \bigl(0,\, \tfrac12,\, \tfrac12\bigr), since the second and third argument are both the maximum. In case all arguments are equal, this is simply \operatorname{arg\,max}(z, \dots, z) = \bigl(\tfrac1n, \dots, \tfrac1n\bigr). Points with multiple arg max values are singular points (or singularities, and form the singular set) – these are the points where arg max is discontinuous (with a jump discontinuity) – while points with a single arg max are known as non-singular or regular points. With the last expression given in the introduction, softargmax is now a smooth approximation of arg max: as , softargmax converges to arg max. There are various notions of convergence of a function; softargmax converges to arg max pointwise, meaning for each fixed input as , \sigma_\beta(\mathbf{z}) \to \operatorname{arg\,max}(\mathbf{z}). However, softargmax does not converge uniformly to arg max, meaning intuitively that different points converge at different rates, and may converge arbitrarily slowly. In fact, softargmax is continuous, but arg max is not continuous at the singular set where two coordinates are equal, while the uniform limit of continuous functions is continuous. The reason it fails to converge uniformly is that for inputs where two coordinates are almost equal (and one is the maximum), the arg max is the index of one or the other, so a small change in input yields a large change in output. For example, \sigma_\beta(1,\, 1.0001) \to (0, 1), but \sigma_\beta(1,\, 0.9999) \to (1,\, 0), and \sigma_\beta(1,\, 1) = \tfrac12 for all inputs: the closer the points are to the singular set (x, x) , the slower they converge. However, softargmax does converge compactly on the non-singular set. Conversely, as , softargmax converges to arg min in the same way, where here the singular set is points with two arg min values. In the language of tropical analysis, the softmax is a deformation or "quantization" of arg max and arg min, corresponding to using the log semiring instead of the max-plus semiring (respectively min-plus semiring), and recovering the arg max or arg min by taking the limit is called "tropicalization" or "dequantization". It is also the case that, for any fixed , if one input is much larger than the others relative to the temperature, T = 1/\beta , the output is approximately the arg max. For example, a difference of 10 is large relative to a temperature of 1: \sigma(0,\, 10) := \sigma_1(0,\, 10) = \left(\frac{1}{1 + e^{10}},\, \frac{e^{10}}{1 + e^{10}}\right) \approx (0.00005,\, 0.99995) However, if the difference is small relative to the temperature, the value is not close to the arg max. For example, a difference of 10 is small relative to a temperature of 100: \sigma_{1/100}(0,\, 10) = \left(\frac{1}{1 + e^{1/10}},\, \frac{e^{1/10}}{1 + e^{1/10}}\right) \approx (0.475,\, 0.525). As , temperature goes to zero, T = 1/\beta \to 0 , so eventually all differences become large (relative to a shrinking temperature), which gives another interpretation for the limit behavior. P(y=j\mid \mathbf{x}) = \frac{e^{\mathbf{x}^\mathsf{T}\mathbf{w}_j}}{\sum_{k=1}^K e^{\mathbf{x}^\mathsf{T}\mathbf{w}_k}} This can be seen as the composition of linear functions \mathbf{x} \mapsto \mathbf{x}^\mathsf{T}\mathbf{w}_1, \ldots, \mathbf{x} \mapsto \mathbf{x}^\mathsf{T}\mathbf{w}_K and the softmax function (where \mathbf{x}^\mathsf{T}\mathbf{w} denotes the inner product of \mathbf{x} and \mathbf{w} ). The operation is equivalent to applying a linear operator defined by \mathbf{w} to tuples \mathbf{x} , thus transforming the original, probably highly-dimensional, input to vectors in a -dimensional space \mathbb{R}^K . \frac{\partial}{\partial q_k}\sigma(\textbf{q}, i) = \sigma(\textbf{q}, i)(\delta_{ik} - \sigma(\textbf{q}, k)). This expression is symmetrical in the indexes i, k and thus may also be expressed as \frac{\partial}{\partial q_k}\sigma(\textbf{q}, i) = \sigma(\textbf{q}, k)(\delta_{ik} - \sigma(\textbf{q}, i)). Here, the Kronecker delta is used for simplicity (cf. the derivative of a sigmoid function, being expressed via the function itself). To ensure stable numerical computations subtracting the maximum value from the input tuple is common. This approach, while not altering the output or the derivative theoretically, enhances stability by directly controlling the maximum exponent value computed. If the function is scaled with the parameter \beta , then these expressions must be multiplied by \beta . See multinomial logit for a probability model which uses the softmax activation function. P_t(a) = \frac{\exp(q_t(a)/\tau)}{\sum_{i=1}^n\exp(q_t(i)/\tau)} \text{,} where the action value q_t(a) corresponds to the expected reward of following action a and \tau is called a temperature parameter (in allusion to statistical mechanics). For high temperatures ( \tau \to \infty ), all actions have nearly the same probability and the lower the temperature, the more expected rewards affect the probability. For a low temperature ( \tau \to 0^+ ), the probability of the action with the highest expected reward tends to 1. The standard softmax is numerically unstable because of large exponentiations. The safe softmax method calculates instead \sigma(\mathbf{z})_i = \frac{e^{\beta (z_i-m)}}{\sum_{j=1}^K e^{\beta (z_j-m)}} where m = \max_i z_i is the largest score involved. Subtracting by it guarantees that the exponentiations result in at most 1, see translation invariance in the mathematical properties. The attention mechanism in transformers takes three arguments: a "query vector" q , a list of "key vectors" k_1, \dots, k_N , and a list of "value vectors" v_1, \dots, v_N , and outputs a softmax-weighted sum over value vectors: o = \sum_{i=1}^N \frac{e^{q^T k_i - m}}{\sum_{j=1}^N e^{q^T k_j - m}} v_i The standard softmax method involves several loops over the inputs, which would be bottlenecked by memory bandwidth. It can be efficiently computed on a GPU cluster using the FlashAttention algorithm. More generally, softmax is invariant under translation by the same value in each coordinate: adding \mathbf{c} = (c,\, \dots,\, c) to the inputs \mathbf{z} yields \sigma(\mathbf{z} + \mathbf{c}) = \sigma(\mathbf{z}) , because it multiplies each exponent by the same factor, e^c (because e^{z_i + c} = e^{z_i} \cdot e^c ), so the ratios do not change: \sigma(\mathbf{z} + \mathbf{c})_j = \frac{e^{z_j + c}}{\sum_{k=1}^K e^{z_k + c}} = \frac{e^{z_j} \cdot e^c}{\sum_{k=1}^K e^{z_k} \cdot e^c} = \sigma(\mathbf{z})_j. Geometrically, softmax is constant along diagonals: this is the dimension that is eliminated, and corresponds to the softmax output being independent of a translation in the input scores (a choice of 0 score). One can normalize input scores by assuming that the sum is zero (subtract the average: \mathbf{c} where c = \frac{1}{n} \sum z_i ), and then the softmax takes the hyperplane of points that sum to zero, \sum z_i = 0 , to the open simplex of positive values that sum to 1 \sum \sigma(\mathbf{z})_i = 1 , analogously to how the exponent takes 0 to 1, e^0 = 1 and is positive. By contrast, softmax is not invariant under scaling. For instance, \sigma\bigl((0,\, 1)\bigr) = \bigl(\tfrac{1}{1 + e},\, \tfrac{e}{1 + e}\bigr) but \sigma\bigl((0, 2)\bigr) = \Bigl(\tfrac{1}{1 + e^2},\, \tfrac{e^2}{1 + e^2}\Bigr). The standard logistic function is the special case for a 1-dimensional axis in 2-dimensional space, say the x-axis in the plane. One variable is fixed at 0 (say z_2 = 0 ), so e^0 = 1 , and the other variable can vary, denote it z_1 = x , so the standard logistic function and its complement are \frac{e^{z_1}}{e^{z_1} + e^{z_2}} = \frac{e^x}{e^x + 1}, \quad \frac{e^{z_2}}{e^{z_1} + e^{z_2}} = \frac{1}{e^x + 1}. The 1-dimensional input could alternatively be expressed as the line \bigl(\tfrac12 x,\, -\tfrac12x\bigr) , with outputs \frac{e^{x/2}}{e^{x/2} + e^{-x/2}} = \frac{e^x}{e^x + 1}, \quad \frac{e^{-x/2}}{e^{x/2} + e^{-x/2}} = \frac{1}{e^x + 1}. \frac{\partial}{\partial z_i} \operatorname{LSE}(\mathbf{z}) = \frac{\exp z_i}{\sum_{k=1}^{K} \exp z_k} = \sigma(\mathbf{z})_i, for , and . The gradient of softmax is \frac{\partial}{\partial z_j} \sigma(\mathbf{z})_i = \frac{\partial}{\partial z_j} \frac{\exp z_i}{\sum_{k=1}^{K} \exp z_k} = \sigma_i (\delta_{ij} - \sigma_j), where the Kronecker delta \delta_{ij} equals 1 if i = j and is otherwise equal to 0 . >>> import numpy as np >>> z = np.array([1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0]) >>> beta = 1.0 >>> np.exp(beta * z) / np.sum(np.exp(beta * z)) array([0.02364054, 0.06426166, 0.1746813, 0.474833, 0.02364054, 0.06426166, 0.1746813])
Cite this
"softmax function". 1789452175. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1375000286