Learn with me: argmax and softmax

This post could be a short one or a long one. I like to see this little blog of mine as an exercise in emergence itself. A lot of tiny ideas, some abandoned projects, a few series that i thought i would be consistent with. This one is about a mathematical method I wish to learn for a paper I am working on. Let us learn what a softmax function is and what does it mean to take an argmax or an argmin of an array.

argmin/max function

For people who work with scientific computation are probably aware of this function as a standard function in scientific programming libraries like numpy/scipy in Python or in Julia. But what does it mean mathematicaly?

We call an arg min function or an argmax function to get the position of element where it is maximum or minimum in an array. Let us say I have a list of numbers defined as \(X=[4,2,7,3,1,9]\). And I want to know which element in this array holds the maximum value in the array, I can call argmax function and I will get \(6\) as the answer, we can see that \(\max{X}=X[6]=9\). Thus an argmax function gives you the argument of a function where the function is maximum.

Similarly, argmin stands for argument of the minimum, to understand argmin, let us consider a continuous case. Say, we have a function \(f(x)=(x-2)^2\), we can plot the function and see what it looks like. For the mathematically inclined people, you can tell that this will be a parabola with the minimum point shifted at \(x=2\). Thus, we know \(f(x=2)=0\), or in other words, \(x=2\) is the argument of the function \(f(x)\) at which \(f(x)\) becomes minimum. \(x=2\) is thus the argmin of \(f(x)\).

Note, python do not provide all the arguments at which a function or array becomes minimum/maximum. It only provides the argument or position of the first minimum/maximum.

>>> import numpy as np
>>> a=np.arange(4)+10
>>> a
array([10, 11, 12, 13])
>>> np.argmin(a)
np.int64(0)
>>> a[np.argmin(a)]
np.int64(10)
>>> a[3]=10
>>> a
array([10, 11, 12, 10])
>>> np.argmin(a)
np.int64(0)

Note, the difference between np.min() and np.argmin(), the former provides the minimum value in the array and the latter provides the argument at which that minimum value occurs in the array. Mathematically, argmin is part of the domain of the function whereas, min is part of the codomain of the function.

Argmax to softmax

The previous section was a very simple and mostly obvious points about a function that we all know about. In this section, we develop on the argmax function to describe and define a very interesting function known as softmax function. Before we formally define it, let me briefly discuss why I had a need for it.

Let us say, I want a function that gives me the argument of the least object in an array, I can simply use argmin and be happy with it, but what if I want the arguments of the two least elements, the three least and so on? What if instead of being in a discrete space, I am in a continuous domain. Like that of the parabola described above? What should I do then?

To answer this, mathematicians have already defined a smooth approximation of the argmax/min function known as softmax/min function. This function is used a lot in machine learning applications as an activation function (much like the ReLU function and Tanh function) and is also used in regression.

In essense, softmax function takes a set of numbers and transforms them so that all of the numbers are present inside a simplex. i.e. after applying the softmax function to a tuple \(\vec{X}=(x_1,x_2,\cdots x_N)\), we obtain \(\vec{\sigma(X)}\) where each \(\sigma_i\) belongs to \([0,1]\) and all the elements add upto \(1\). i.e. it creates a probability distribution assigned to each element of the vector/tuple \(\vec{X}\). Mathematically it is defined as:

\[ \sigma_i \equiv \sigma(\mathbf{X})_i = \frac{e^{\beta X_i}}{\sum_{j=1}^N e^{\beta X_j}} \ \text{for} \ i = 1, \dots, N. \]

We can show that similar to argmax to softmax, we can write softmin for argmin with \(\beta \to -\beta\):

\[ \sigma_i \equiv \sigma(\mathbf{X})_i = \frac{e^{-\beta X_i}}{\sum_{j=1}^N e^{-\beta X_j}} \ \text{for} \ i = 1, \dots, N. \]

The plot below shows this transformation for three values. Move the sliders to change the inputs \(x_1,x_2,x_3\) and \(\beta\); the point shows the resulting probability vector \((\sigma_1,\sigma_2,\sigma_3)\) inside the simplex. This post uses the negative-exponent convention above, so increasing \(\beta\) puts more weight on the smallest \(x_i\), thus showing the softmin function at work.

The simplex visualisation is inspired by Joseph Salmon's interactive softmax visualisation.

Note, this mathematical form is exactly same as the boltzman probability distribution! with \(\beta= 1/ k_b T\), where \(T\) is the effective temperature and \(k_b\) is the boltzman constant. Denominator represents the canonical partition function used in the statistical mechanics and \(X_i\) are the energy states of particles.

I love when two separate worlds collide and merge so well. The beauty of mathematics as the universal language of the world shines through bright here. A distribution used in statistical mechanics makes a wild card appearance in our continuous approximation of the argmax/min function.

To understand this function, let us see what \(\sigma_i\) looks like in the limit \(\beta \to 0\). Every exponential tends to \(1\), so \(\sigma_i\) tends to \(1/N\): all entries receive equal probability. (Put \(\beta \to 0\) in the visualisation above) As \(\beta\) grows, the distribution concentrates on the smallest \(X_i\), because our definition has a negative exponent.

If \(x_m\) is the unique minimum, then \(\Delta_j=x_j-x_m>0\) for every \(j \ne m\), and

\[ \sigma_m = \frac{e^{-\beta x_m}}{e^{-\beta x_m} + \sum_{j \ne m} e^{-\beta x_j}} = \frac{1}{1 + \sum_{j \ne m} e^{-\beta \Delta_j}} \xrightarrow{\beta \to \infty} 1. \]

If \(k\) values share the minimum, their probabilities each tend to \(1/k\). With the negative exponent used here, this is a soft approximation to argmin; using \(e^{+\beta x_i}\) instead gives the corresponding soft approximation to argmax.

Interestingly, geometrically the softmax or softmin function maps the values to boundaries of the simplex for sufficiently low temperature or large (\(\beta\)). See the visualisation above. And this function has applications in wide range of fields like Game theory, decision theory, machine learning, and neural networks.