Understanding different Loss Functions for Neural Networks

 

Understanding different Loss Functions for Neural Networks

Image by KotaFeja

The Loss Function is one of the important components of Neural Networks. Loss is nothing but a prediction error of Neural Net. And the method to calculate the loss is called Loss Function.

In simple words, the Loss is used to calculate the gradients. And gradients are used to update the weights of the Neural Net. This is how a Neural Net is trained.

Keras and Tensorflow have various inbuilt loss functions for different objectives. In this guide, I will be covering the following essential loss functions, which could be used for most of the objectives.

  • Mean Squared Error (MSE)
  • Binary Crossentropy (BCE)
  • Categorical Crossentropy (CC)
  • Sparse Categorical Crossentropy (SCC)

Mean Squared Error

Example

Mean Squared Error

Binary Crossentropy

Example

Binary Crossentropy

While training the network, the target value fed to the network should be 1 if it is raining otherwise 0.

Note 1

Note 2

Categorical Crossentropy

Example

Categorical Crossentropy

For feeding the target value at the time of training, we have to one-hot encode them. If the image is of cat then the target vector would be (1, 0) and if the image is of dog, the target vector would be (0, 1). Basically, the target vector would be of the same size as the number of classes and the index position corresponding to the actual class would be 1 and all others would be zero.

Note

Sparse Categorical Crossentropy

When we are using SCCE loss function, you do not need to one hot encode the target vector. If the target image is of a cat, you simply pass 0, otherwise 1. Basically, whichever the class is you just pass the index of that class.

Sparse Categorical Crossentropy

These were the most important loss functions. And probably you will be using one of these loss functions when training your neural network

Comments

Popular posts from this blog

What is Gini Impurity?

20+ Questions to Test your Skills on Logistic Regression

Artificial Neural Network Interview Questions