concept softmax in category machine learning

appears as: softmax, softmax, The softmax
Machine Learning with TensorFlow, Second Edition

This is an excerpt from Manning's book Machine Learning with TensorFlow, Second Edition.

Softmax regression is named after the traditional max function, which takes a vector and returns the max value; but softmax isn’t exactly the max function, because it has the added benefit of being continuous and differentiable. As a result, it has the helpful properties for stochastic gradient descent to work efficiently.

With softmax regression you have a similar construction wherein you learn weights of size num_features x num_labels. In this case, the weights correspond to the weights to be multiplied by the dependent variables, similar to logistic regression, and you also learn a bias matrix of size num_labels, again similar to standard logistic regression. The key difference with softmax regression is that you apply the softmax function instead of sigmoid to learn the probability distribution over the N classes you are trying to predict. Remind yourself of the equation for softmax:

Now consider the softmax classifier in the context of your sentiment classification problem. You are trying to learn the probability, Y, that a set of text implies one of two labels positive or negative for each of the 25,000 reviews that you will test on. The set of text for each review is converted into a feature vector of 5,000 dimensions, one for each of the 25,000 reviews, X. The weights are associated weights for each of the two classes, positive or negative, to multiply by the 5,000 dependent variables, the associated matrix of which we refer to as W. Finally, B is the bias for each of the two classes, positive and negative, to add forming the linear equation used for regression. The visual depiction of this construction is shown for your reference in figure 6.6.

Human-in-the-Loop Machine Learning

This is an excerpt from Manning's book Human-in-the-Loop Machine Learning.

Figure 3.1: showing how softmax creates probability distributions in two types of architectures. In the top example, softmax is the activation function of the output (final) layer, directly outputting a probability distribution. In the bottom example, a linear activation function is used on the output layer, creating model scores (logits) that are then converted into probability distributions via softmax. The bottom architecture is only slightly more complicated, but is it preferred for Active Learning as it is more informative.

It is common to use softmax in the final layer or to only look at the result of softmax applied to the logits. However, we generally prefer an architecture in Active Learning that allows us to see logits from a linear activation function, as it has more information. Softmax is lossy and loses the distinction between uncertainty due to strongly competing information vs uncertainty due to a lack of information. We will use the second type of architecture in Figure 3.1 for our examples in this section, but the effects would apply either way: whether softmax is an activation function or is applied to the model scores. We will also return to the choice of activation function at the end of this Chapter.

Here’s an example implementation of softmax in python using the PyTorch library[3]:

def softmax(self, scores, base=math.e):
        """Returns softmax array for array of scores
       
        Converts a set of raw scores from a model (logits) into a
        probability distribution via softmax.
           
        The probability distribution will be a set of real numbers
        such that each is in the range 0-1.0 and the sum is 1.0.
   
        Assumes input is a pytorch tensor: tensor([1.0, 4.0, 2.0, 3.0])
            
        Keyword arguments:
            prediction -- a pytorch tensor of any positive/negative real numbers.
            base -- the base for the exponential (default e)
        """
        exps = (base**scores.to(dtype=torch.float)) # exponential for each value in array
        sum_exps = torch.sum(exps) # sum of all exponentials
 
        prob_dist = exps / sum_exps # normalize exponentials
        return prob_dist