Each parcel must go to exactly one of three handling routes, the standard conveyor, cold storage or oversized handling. A binary logistic model gives a single probability, but this task needs a probability for every route, all of them nonnegative and adding to . Softmax turns a vector of class scores into such a probability vector, and multiclass cross-entropy trains it. This lesson also separates tasks whose classes exclude each other from tasks where several labels can hold at once, and compares softmax with one-vs-rest.
Key terms
Multiclass classification
Definition Multiclass classification is a classification task with classes in which every example belongs to exactly one class. The classes must be mutually exclusive and, for the cases the model is meant to handle, together cover every case. The label is coded as an index in or as a one-hot vector in with a single at the true class. A linear multiclass model computes one score per class,
where in holds the features, is a weight matrix and is in . The score of class is , where is row of . With the task is binary classification.
In plain words Each case gets one answer from a fixed list. Whether a task is multiclass depends on what the labels mean, not on the software. If the routing rules send every parcel to exactly one route, with a stated priority when several properties apply, the route is a multiclass label. The scores can be any real numbers and are not yet probabilities.
Example Number the routes standard, cold and oversized as classes , and . A parcel routed to cold storage has label , or one-hot vector . With features the matrix has rows and columns, and the score vector has entries.
Multilabel classification
Definition Multilabel classification is a task with binary labels per example, any number of which can equal at the same time, so the target is a vector in that may contain several ones or none. A common model gives each label its own probability, for , and is trained with the sum or mean of the binary cross-entropies. These probabilities need not add to . Multiplying or across the labels to get the probability of a whole label vector additionally assumes that the labels are conditionally independent given , which may be false.
In plain words A case can have several properties at once, so the model answers separate yes or no questions. A single softmax would make the labels compete for one unit of probability, which is wrong when two of them can both be true.
Example Take the properties fragile, chilled and heavy. A parcel that is fragile and chilled but not heavy has target , while the multiclass route target picks exactly one route. If the three label scores are , and , the sigmoids give about , and , which add to about with no contradiction. The summed binary cross-entropy for the target is about . Only under conditional independence would the probability of exactly this label vector be .
Softmax function
Definition The softmax function maps a score vector in to the vector whose entries, for , are
The result is a class probability vector, a vector in with for every and . In a model is the modeled probability . For finite scores every softmax entry is strictly positive, so with each entry lies strictly between and . Adding the same constant to every score leaves the output unchanged, because the factor cancels. The ratio of two entries is , and for the first entry is . Multiplying all scores by a positive constant other than keeps the order of the entries but changes their values unless all scores are equal. When every wrong class costs the same and a correct class costs nothing, predicting a class with the largest minimizes the modeled probability of error.
Because of the shift invariance, adding the same vector to every row of and the same number to every entry of leaves all the probabilities of a linear softmax model unchanged. Its parameters are therefore determined only up to such a shift unless one class is fixed as a reference with scores .
In plain words Exponentiating makes every score positive, and dividing by the total makes the results add to . Only differences between scores matter, and a larger gap gives the leading class more probability. To check a proposed probability vector, check that no entry is negative and that the entries add to . Passing that check is a mathematical property. Whether cases given probability for cold storage really need it about half the time is calibration, a separate question checked on held-out data.
Example For scores in the order standard, cold, oversized, the exponentials are , and with total , so . Cold storage has the largest score and the largest probability. Adding to every score gives the same vector. Doubling the scores gives exponentials , and and probabilities , still led by cold storage but more concentrated. A sharper vector is not by itself a better or better calibrated prediction.
Log-sum-exp trick
Definition The log-sum-exp of a score vector in is , and the logarithms of the softmax probabilities are . Let be the largest score and
the sum of the shifted exponentials. Since , the trick computes both quantities as
The shifted scores are all at most , with at least one equal to , so every shifted exponential lies in and lies in . It follows that , and the partial derivative of with respect to is .
In plain words Exponentials of large scores overflow, and exponentials of very negative scores round to . Subtracting the largest score first gives the same probabilities, since softmax ignores a common shift, and keeps every exponential at most . Computing log probabilities straight from the shifted scores also avoids taking the logarithm of a probability that has already rounded to . The exponential of a very negative shifted score can still round to inside , which is harmless because is at least .
Example For scores , computing directly overflows, since the largest double-precision number is about , roughly . With the shifted scores are , so and . The probabilities are about , and . If the true class is oversized, its loss is .
Multiclass cross-entropy
Definition For one example with true class , one-hot target and softmax probabilities , the multiclass cross-entropy is
Its gradient with respect to the scores has entries , because the derivative of with respect to is and the derivative of with respect to is for and otherwise. Training minimizes the average over the training examples, often plus a penalty. If the labels are conditionally independent given their features and the parameters, this average is the categorical negative log likelihood divided by , so minimizing it is maximum likelihood. For it is the binary cross-entropy of the previous lesson.
In plain words Only the probability of the true class enters the loss, but softmax ties it to every score, so raising that probability lowers the total of the others. A gradient step raises the true class’s score in proportion to and lowers each other score in proportion to its probability. The loss measures confidence as well as correctness, so two models that predict the same classes can have very different cross-entropy.
Example For and true class cold, the loss is . If the true class were oversized, the same prediction would cost . With true class cold the gradient is , and a step of size taken on the scores themselves changes them by , and . The probability of cold rises to about and the loss falls to about . In a real linear model the step updates the shared and , so it changes the scores of every example. A model that gave cold storage probability would make the same decision with loss .
One-vs-rest
Definition One-vs-rest, also called one-vs-all, reduces a -class problem to binary problems. For each class a separate binary classifier is trained with class as the positive class and all other classes together as the negative class, and it returns a score or an estimated probability . A new case is assigned to a class with the largest of these outputs, which presumes they are on comparable scales. The objectives are fitted separately, so the need not add to . Dividing them by their sum afterward gives a vector that adds to , but it does not make the training a softmax cross-entropy fit.
In plain words Softmax regression fits all classes together, with one shared denominator and one categorical loss. One-vs-rest asks separate questions of the form “this class or not” and compares the answers. Which approach predicts better on new data is a question for validation, and neither one wins on every problem. For multilabel tasks separate sigmoids are the natural model, because there the probabilities should not be forced to add to .
Example To compare only the output functions, apply a sigmoid to each of the scores instead of softmax. The results are , and , which add to , while softmax of the same scores gives . Separately fitted one-vs-rest classifiers would not learn these same scores, so this compares two conversions, not two trained models. Both pick cold storage here.
Check yourself
- For three mutually exclusive classes, decide which of these vectors are valid class probability vectors, and whether a valid one could be the softmax of finite scores. The vectors are , and .
Show the answer
The first vector is invalid, since its entries add to . The second adds to but has a negative entry, and a sum of cannot rescue a negative probability, so it is invalid too. The third has no negative entry and adds to , so it is a valid probability vector. It cannot be the softmax of finite scores, because every softmax entry is strictly positive. A model that output it would also have infinite cross-entropy on any case from the third class.
- Scores in the order standard, cold, oversized are , and the true class is cold. Find the probability vector, the predicted class when all errors cost the same, and the cross-entropy. What changes if is added to every score, and what if every score is doubled?
Show the answer
The exponentials are , and with total , so the probabilities are , which are positive and add to . Standard has the largest probability and is the prediction, so this case is misclassified. The loss uses the probability of the true class,
Adding to every score changes nothing, since softmax depends only on differences. Doubling gives exponentials , and and probabilities . The wrong prediction becomes more confident, and the loss rises to .
- Each support ticket is sent to exactly one team, billing, technical or shipping, and can also carry any of the tags urgent, refund request and repeat contact. Which part is multiclass and which is multilabel, and what output suits each? For the team, someone trains three one-vs-rest classifiers, gets , and for one ticket, and reports these as the probabilities of the three teams. What is wrong?
Show the answer
The team is a multiclass label, provided the routing rules make the teams exclusive and cover every ticket, so a softmax over three scores fits and its probabilities add to . The tags are multilabel, since a ticket can be urgent and a repeat contact at once, so three sigmoids trained with binary cross-entropy fit, and their probabilities may add to more or less than . The one-vs-rest outputs add to , so they are not a probability vector over three exclusive teams. Choosing billing, the largest output, is a reasonable decision if the three classifiers give comparable scores. Dividing by gives about , which adds to , but those numbers were not trained as joint class probabilities, so they need a calibration check on held-out data, or a softmax model can be fitted instead.