Different types of Cost Functions in Deep Learning
In machine learning models, especially neural networks, the loss function is a function that informs us about the accuracy of our model predictions. It does so by calculating the difference between the values predicted by the model and the true values of the dataset. Predictive models can be used for different tasks, including binary classification, multi class classification and regression. Similarly different loss functions are used to calculate the losses for each of these prediction tasks. When plotting such loss functions, many local minima and saddle points are observed. Neural network models strive to find the best set of parameters to decrease the loss function.
Regression tasks
1. Mean Squared Error
When dealing with Stochastic Gradient descent or any other optimization methods that update weights after every record, the squared error is given by, SE = . Here, is the real output value whereas is our predicted output. When dealing with batch optimizers we use the mean squared error given by MSE = where t is the batch size. As the mean squared and the squared errors are both quadratic functions, we notice a convex curve when plotting the function. The disadvantage of the MSE error function is it is not robust to outliers, i.e. if the dataset has some outliers the mean squared error will punish the error values of the outliers more because the square of the difference will be more. We can avoid this by using the absolute error loss.
2. Absolute Error Loss
Instead of squaring the difference we take the absolute value so that the error AEL= which is a linear equation. The advantage of this is that mean absolute error is more robust to outliers because the errors are not getting squared so we are not penalizing enough. However the computation of the absolute value is very difficult because we are using a modulus operator.
3. Huber Loss
Here we combine the two previous loss functions. The Huber error is written as, Loss = if otherwise, (Linear eqn) where is a hyperparameter.
Classification tasks
In Neural Networks, the outputs for classification tasks are probabilistic values, i.e. instead of providing the exact values similar to regression tasks, the output values y represent the probability that the input record belongs to a certain class. Hence, for an accurate model, the higher the probability of the output for the correct class, the better the performance and vice versa. Therefore our evaluation model should punish a lower output value for the actual class as this value corresponds to the prediction of the probability of the record belonging to that class being low. In the range of 0 to 1, the log function maps to exponentially high values for small valued inputs. Hence, it is used as the loss function to evaluate the performance of classification tasks.
1. Binary Cross Entropy
The binary cross entropy loss = . Here, if the actual value y=0, Loss = , otherwise if y=1, Loss = . Here, y is found by using the sigmoid activation function where , where z = where X are the input values, w is the weight matrix and b is the bias . We consider that denotes the probability of the particular record belonging to the positive class, i.e. y=1. Hence, if is high, the model is predicting that the record belongs to class y=1. We can see that a high value for y = 0 will correspond to being small and hence, the Loss = will be exponentially high.
2. Categorical Cross Entropy
For multi class classification problems, the output layer consists of c nodes where c= the number of classes in the problem and uses softmax as the activation function. For a particular record, the softmax function is given by:
where i, j denote the nth node of the output layer. Usually, we use sigmoid for binary and softmax for multiclass classification. Hence, our neural network will have a vector output consisting of c items: and each item will represent the probability of the record belonging to the class c. For multi class classification, we have to also remember to do one hot encoding on our target labels. One hot encoding will represent each category as a column and will represent the values of the columns as bits, where 1 denotes the record belongs to that categorical column and 0 denotes it does not. The target label will be a binary vector consisting of c items (the classes): Hence, when dealing with Stochastic Gradient descent or any other optimization methods that update weights after every record,the categorical cross entropy loss is: When dealing with batch optimizers, the loss is given by: , where t is the batch size Here, is a one-hot encoded target vector where i specifies the record or row number, i.e. denotes the target vector of record number 3. Hence, denotes the jth item in the output vector of the ith record .
Auras Khanal