# Mean Squared Error vs Cross entropy loss function

In this post, you will be learning the difference between two common types of loss functions: Cross-Entropy Loss and Mean Squared Error (MSE) Loss. These are both used in machine learning for classification & regression tasks, respecitively, to measure how well a model performs on unseen dataset. Both these losses are ways of measuring how well the predictions are made by classification and regression algorithms, and they both provide different information about the performance of models. As a data scientist, it is very important for you to understand the difference between loss functions in a great manner.

## What is cross-entropy loss?

Cross entropy loss is used in classification tasks where we are trying to minimize the probability of a negative class by maximizing an expected value of some function on our training data, also called as “loss function”. Simply speaking, it is used to measure the difference between two probabilities that a model assigns to classes. Cross entropy loss is also called as ‘softmax loss’ after the predefined function in neural networks. It is also used for multi-class classification problems. An example of the usage of cross-entropy loss for multi-class classification problems is training the model using MNIST dataset. The idea of this loss function is to give a high penalty for wrong predictions and a low penalty for correct classifications. It calculates a probability that each sample belongs to one of the classes, then it uses cross-entropy between these probabilities as its cost function. The more confident model is about prediction, the less penalty it incurs.

Cross-entropy loss is very similar to cross entropy. They both measure the difference between an actual probability and predicted probability, but cross entropy uses log probabilities while cross-entropy loss uses negative log probabilities (which are then multiplied by -log(p)) . Log probabilities can be converted into regular numbers for ease of computation using a softmax function.

Cross-entropy loss is also used in time series classification problems such as forecasting weather or stock values. An example would be comparing the forecast of a short term vs long term prediction model making cross-entropy loss incurring large penalty when one class has much higher probability.

A common example used to understand cross-entropy loss is comparing apples and oranges where each fruit has a certain probability of being chosen out of three probabilities (apple, orange, or other). Apple would have an 80% chance while Oranges will only get 20%. In order for our model to make correct predictions in this example, it should assign a high probability to apple and a low for orange. If cross-entropy loss is used, we can compute the cross-entropy loss for each fruit and assign probabilities accordingly. We will then want to choose apple with a higher probability as it has less cross entropy lost than oranges.

### How do you calculate cross-entropy loss?

Cross-entropy loss is calculated by taking the difference between our prediction and actual output. We then multiply that value with -y * ln(y). This means we take a negative number, raise it to the power of the logarithm of y (which will be positive), and then subtract this from our original calculation.

Here is the formula for calculating cross entropy loss:

The cross entropy loss is then calculated by using the cross-entropy formula and adding up all the losses: C = \sum_{i=0}^{m} cross_entropy(X, Y)

## What is mean squared error (MSE) loss?

Mean squared error (MSE) loss is used in regression tasks where we are trying to minimize an expected value of some function on our training data, which is also called as “loss function”.

### How do you calculate mean squared error loss?

Mean squared error (MSE) loss is calculated by taking the difference between y and our prediction, then square those values. We take these new numbers (square them), add all of that together to get a final value, finally divide this number by y again. This will be our final result.

The formula for calculating mean squared error loss is as follows:

This will give us a loss value between 0 and infinity with larger values indicating mean squared error.

Root mean square error (RMSE) is a mean square error loss function that is normalized between 0 and infinity. The root mean squared error (RMSE) can be written as follows:

$$RMSE = \sqrt{\frac{ mean\_squared\_error}{m}}$$