Course 0: Why targets 0 and 1 in machine learning ?
Here is my course of deep learning in 5 days only!
First, we’ll begin by the basic concepts in this article, because main deep learning concepts come from datascience. This is a great article for an experienced datascientist also.
I hope you’ll get some feelings about deep learning you cannot get from reading else where.
First, let’s recap the basics of machine learning.
Loss functions
When we fit a model, we use loss functions, or cost functions, or objective functions. The main purpose of machine learning is to be able to predict given data. Let’s say predict the given some observations, let’s say the , through a function:
Usually, f is called a model and is parametrized, let’s say by a list of parameters
and the goal of machine learning is to find the best parameters.
In supervised learning we know the real value we want to predict , for example the class of the object we want to predict, what we call the ground truth or the target.
We want to be able to measure the difference between what we are predicted with the model and what it should predict. For that, there are some cost functions, depending on the problem you want to address, to measure how far our predictions are from their targets.
In order to minimize the cost, we prefer differentiable functions which have simpler optimization algorithms.
The two most important loss functions are:

Mean Squared Error (MSE), usually used for regression:

Cross Entropy for probabilities, in particular for classification where the model predicts the probability of the observed object x for each class or “label”:
Coming from the theory of information, crossentropy measures the distance between two probabilities by :
which can be defined as the expected loglikelihood of the predicted label under the true label distribution, and we want this cost to be the lowest possible (minimization).
Note that a loss function always outputs a scalar value. This scalar value is a measure of fit of the model with the real value. Since a loss function outputs a scalar, the shape of its Jacobian with respect to an input is the same as the shape of the input, for example for an input of rank 4:
In conclusion of this section, the goal of machine learning is to have a function fit with the real world; and to have this function fit well, we use a loss function to measure how to reduce this distance.
Exercise: discover many more loss functions with my full article about loss functions.
Cross entropy in practice
Two problems arise: first, most mathematical functions do not output a probability vector which values sum to 1. Second, how do I evaluate the true distribution ?
Normalizing model outputs
Usually, transforming any model output into a probability that sums to 1 is usually performed thanks to a softmax function set on top of the model outputs :
The softmax normalization function is defined by:
Note that for the softmax to predict probability for C classes, it requires the output to be Cdimensional. are called the logits.
Softmax is the equivalent of the sigmoid()
in binary classification:
in the multiclass case. If you do not remember which one between the softmax and the sigmoid has a negative sign in the exponant, ie or , remember that Softmax and Sigmoid are both monotonic functions.
Estimating the true distribution
Cross entropy is usually mentioned without explanations.
In fact, to understand crossentropy, you need to rewrite its theoretical definition (1):
because is the true label distribution, so cross entropy is the expectation of the negative logprobability predicted by the model under the true distribution.
Then, we use the formula for the empirical estimation of the expectation:
where D is the real sample distribution, N is the number of samples on which the cross entropy is estimated () and is the true class of x.
When we compute the crossentropy, we set an empirical crossentropy for one sample (N=1) to
so that when we average the individual losses over more samples for stability, we find back to the desired empirical estimation:
In the future, we adopt the following formulation for a single sample:
which is equivalent to setting the probability in the theoretical crossentropy definition (1) with:
which we will write . That is why the target for the predicted probability for the true class is 1.
is a vector of zero values except for the true class, where it has a one: we name the onehot encoding.
In conclusion, for the case of classification, we compute the crossentropy with values that are either 0 or 1 at the sample level for the “true” probability: x being the image of a cat, we want the model output to fit the empirical class probability at the sample level, that we set to for the real object class “cat” and for all other classes : then at the dataset level, averaging these values lead to the empirical estimates of the true probabilities we are used to.
The Gradient Descent
To minimize the cost function, the most used technique is the Gradient Descent.
It consists in following the gradient to descend to the minima:
In other words, we follow the negative slope of the mountain to find the bottom/lowest position of the valley. In order to avoid a local minima, initialization (the choice of initial values for the parameters, where to start the descent from) is very important, and multiple runs can also help find the best solution.
It is an iterative process in which the update rule simply consists in:
where our cost is defined as a result of the previous section
We call the target (in fact the target distribution). We usually omit the fact the cost is a function of the input, the target and the model parameters and write it directly as “cost”. We can also write with the composition symbol:
An update rule is how to update the parameters of the model to minimize the cost function.
Since the cost is a scalar, the Jacobian has the same shape as the parameters, and gives a derivative value with respect to every parameter, telling us how to update it.
is the learning rate and has to be set carefully: Effect of various learning rates on convergence (Img Credit: cs231n)
This simple method is named SGD, after Stochastic Gradient Descent. There are many improvements around this simple rule: ADAM, ADADELTA, RMS Prop, … All of them are using the first order only. Some are adaptive, such as ADAM or ADADELTA, where the learning rate is adapted to each parameter automatically.
There exists some second order optimization methods also but they are not very common in practice.
Conclusion: When we have the loss function, the goal is to minimize it. For this, we have a very simple update rule which is the gradient descent.
Backpropagation
Computing the gradients to use in the SGD update rule is known as backpropagation.
The reason for this name is the chaining rule in computing gradients of function compositions.
First, adding the softmax and crossentropy to the model outputs is a composition of 3 functions:
where the composition means
But models are also composed of multiple functions, for example let us consider a model composed of 2 dense layers:
This model is called perceptron and the output of the first Dense layer is a hidden representation of the data, while the second layer is used to reduce the number of outputs to the number of classes, to predict class probabilities with a softmax normalization:
Without a ReLu activation in the middle, the two Dense layers would be mathematically equivalent to only 1 Dense layer.
The model has two sets of parameters:
The chaining rule is a rule for gradient computation of the composition of two functions f and g:
which is a simple matrix multiplication
if
What does that mean for deep learning and gradient descent ? In fact, for each layer, to follow the negative slope, we need to compute
to update each layer’s parameters .
So, let’s consider a two layer network composed of g and f. The functions g and f have to be considered as functions of both inputs and parameters:
And the composition becomes
It is possible to compute the derivatives of g and f with respect to, either the parameters, or the inputs, which we’ll differentiate in notation the following way:
To update the parameters of g, we need to compute the derivative of the cost with respect to the parameters of g, that is :
all other parameters () and inputs (x) being constant.
For example, for the layer ,
the gradient is given by
and for the layer ,
We see that is common to the computation of and , and it is possible to compute them once.
So, to reduce the number of matrix mulplications, it is better to compute the gradients from the top layer to the bottom layer and reuse previous computations of matrix multiplication from earlier layers.
In conclusion, gradient computation with respect to each layer’s parameters is performed via matrix multiplications of gradients of the layers above, so it is more efficient to begin to compute gradients from the top layers, a process we call retropropagation or backpropagation.
Cross Entropy with Softmax
Let’s come back to the global view: an observation X, a model f depending on parameters , a softmax to normalize the model’s output, and last, our cross entropy outputting a final scalar, measuring the distance between the prediction and the expected value:
The cross entropy is working very well with the softmax function and is usually implemented as one layer, for numerical efficiency.
Let us see why and study the combination of softmax and cross entropy
Mathematically,
Let’s take the derivative with respect to the model output (before softmax normalization):
which is very easy to compute and can simply be rewritten:
Note that the gradient is between 1 and 1: for a negative class (), the derivative is positive, and the higher the prediction has been positive, the higher the derivative will be; for a positive class, the derivative will always be negative, and the lower the prediction to be positive, the lower the derivative will be.
Since Softmax is monotonic, Softmax output computation is not required for inference, the highest logit corresponds to the highest probability… except if you need the probability to estimate the confidence of the predicted class.
Conclusion: it is easier to backprogate gradients computed on Softmax+CrossEntropy together rather than backpropagate separately each : the derivative of the Softmax+CrossEntropy with respect to the output of the model for the right class, let’s say the “cat” class, will be 0.8  1 =  0.2, if the model has predicted a probability of 0.8 for this class, and the update will follow the negative slope to encourage to increase the prediction ; the derivative of the Softmax+CrossEntropy with respect to an output for a different class will be 0.4 is it has predicted a probability of 0.4, encouraging the model to decrease this value.
Exercise: Compute the derivative of Sigmoid+BinaryCrossEntropy combined.
Exercise: Compute the derivative of Sigmoid+MSE combined.
Example with a Dense layer
Let’s take, as model, a very simple one with only one Dense layer with 2 filters in one dimension and an input of dimension 2:
This is the smallest model we can ever imagine. Such a layer produces a vector of 2 scalars:
Please keep in mind that it is not possible to descend the gradient directly on this output because it is composed of two scalars. We need a loss function, that returns a scalar, and tells us how to combine these two outputs. For example, the Softmax+CrossEntropy we have seen previously:
Now we can retropropagate the gradients. Since we are in the case of 2 outputs, we call this problem a binary classification problem, let’s say: cats and dogs.
Let us compute the derivatives of the dense layer:
so
A Dense layer with 4 outputs:
Generalize beyond cross entropy
Reweighting probabilities
Crossentropy is built upon the probability of the label:
In our section on practical crossentropy, we have considered that we knew the true label with certainty, that the goal to achieve was maximize the objective under the real distribution of labels, even if they are unbalanced in the dataset, leading to strong bias. In practice, we can go one step further, rebalancing these probability as Bayes rules would suggest, or integrate the notion of incertainty in the groundtruth label, to reduce the influence of noise. Here are a few techniques we can use in practice. It is still possible:
 to rebalance the dataset with the inverse class frequency :
This could also be performed by replacing the current sampling schema (), by sampling uniformly the class first, then a sample belonging to this class.
 to train a model with smoother values than 0 and 1 for negatives and positives, for example 0.1 or 0.9, which will help achieve better performances. This technique of label smoothing or soft labels enables in particular to reintroduce the outputs for the negative classes so that it will preserve a symmetry between the negative and positive labels:
This technique reduces the confidence in the targets, and the network overfitting. It discourages too high differences between the logits for the true class and for the other classes.
It is also possible to regularize with label smoothing, by drawing with probability a class among C classes uniformly:

to use smoother values than 0 and 1 when the labels in the groundtruth are less certain,

to focus more on wrongly classified examples
as in the Focal Loss for object detection where background negatives are too numerous and tend to take over the positives. This technique replaces hard negative mining.
Reinforcement
That is where the magic happens ;)
As you might have understood, the cross entropy comes from the theory of information, but the definition of the probabilities and their weighting scheme can be adapted to the problem we want to solve. That is where we leave theory for practice.
Still, there is a very important theoretical generalization of crossentropy through reinforcement learning which is very easy to understand.
In reinforcement, given an observation at a certain timestep, you’re going to perform an action, for example driving car, going right, left, or straight, or in a game, using some keyboard commands. Then the environment is modified and you’ll need to decide of the next action… and sometimes you get a reward , a feedback from the environment, good news or bad news, gain some points… In the case of reinforcement learning, we do not have any labels as targets or groundtruth. We just want to get the best reward :
over all possible sequence of actions.
The probability of a sequence is
so R can be written
When my actions are parametrized by a model , the whole problem becomes parametrized by
and it is possible to find the that maximizes the reward by following the gradient of the expected reward:
which looks exactly the same as the derivative of the cross entropy:
except that you replace the expected reward in place of the true label probability:
In conclusion, in place of the 1 and 0 of the classification case, reinforcement learning proposes to use the global reward R as target label for each timestep that led to this reward, considering each time step as individual samples.
A fantastic demonstration [Williams, 1992] of reweighting the crossentropy, where the eligibility of each parameter to the gradient is multiplied by the reward or the progress in the goal we want to achieve.
Classical crossentropy definition can be seen as a specific case of this more global theorization.
Well done!
Now let’s go to next course: Course 1: programming deep learning!