Why NOT Categorical Cross Entropy?
From the publisher’s feed, collected in the ZeroLabs newsroom.
Source: AI Generated ImageThis is a small article on Categorical Cross Entropy (CCE) in continuation of previous article on Binary Cross Entropy (BCE). In the last post we had come up with a loss function to deal with 2 class problem aka binary classification problem. But what about the case when its more than two classes? This question is answered in this article.The theme of the article is going to be simple. We will go on with a sorta discovering approach as its already known. To brief up what happened in the binary cross entropy is that before concluding the loss, we came up with case wise loss functions which are:Here, while we were designing a constraint to us was the way to represent classes is restricted to just a single variable. i.e. If we have a 2 class problem then y could be 0 or 1. If we have a 3 class problem y could be 0 or 1 or 2. If you observe how things are going then it is hard to perform just switching for a multi class problem and would make things more complicated. So, instead of representing the output class just via simple variable we represent it in terms of one-hot encoded variables (Do not worry if you don’t know what that is, it will be clear with example). Example:Instead of y=0 we represent it as y = [1, 0]Instead of y=1 we represent it as y = [0, 1]Let’s say we have 5 classes then the representation would be as follows:Instead of y=0 we represent it as y = [1, 0, 0, 0, 0]Instead of y=1 we represent it as y = [0, 1, 0, 0, 0]Instead of y=2 we represent it as y = [0, 0, 1, 0, 0]Instead of y=3 we represent it as y = [0, 0, 0, 1, 0]Instead of y=4 we represent it as y = [0, 0, 0, 0, 1]If I were to mathematically generalize this for a n class problem then it had be:Where,Now, as we have seperated variables class wise i.e. each class would have its own variable, we make predictions similarly. So, our model would predict something like this for a 5 class problem then:Class 0 with probability 0.2Class 1 with probability 0.4Class 2 with probability 0.1Class 3 with probability 0.15Class 4 with probability 0.15We will simply represent this as:Note: The probabilities of all classes should always sum up to 1.I think, now to move ahead, a sorta illustration would work better to explain what we would do. Our training would look something like this:Now, if we had think, if class ‘i’ is true, and if we had to improve class ‘i’ chances in prediction then the loss function would beKeeping in context class ‘i’ is true, if model says its true with 100% chance i.e. 1, then we would actually see loss to be 0. If model instead assigns low probability, lets say 0.25, then the loss would be 1.386 which is definetly higher than 0.Lets say we have 3 class classification problem. And we are working with following classes:The loss functions for each case of class are as follows:Notice here that, only at any class ‘i’, yᵢ = 1 rest all are 0. Hold up!!! Spend your time with it.Now, I will teach you one more switching technqiue. Suppose, if we have to choose between 4 things.If we want to make certain kinda function to choose Clubs in this then, I would have to make rest all zero. That would look something like:And that could be also written as follows:Here, we have used factors [0, 1, 0, 0] to choose Club. These factors are working on switching ON or OFF a object. I think by this you already know where I am getting at. And thats good.Similarly, we choose between loss here with class. In above example about 3 classes, if I were to choose Class 1, I would do it as:The implication of this is that, if class ‘i’ predictions need some improvement, then it would start working on its own Loss (Lᵢ) because of this switch.Now if we were to generalize the loss function for n classes it would look like:Thus, this is how we get Categorical Cross Entropy. Making this article for you was interesting, but costed a lot of thought and learning. If something is wrong, please comment it down, or if you want to add input, please comment it down. I think supporting via subscribing or claps or comments really helps a learner and a writer like me.Thank you :)Why NOT Categorical Cross Entropy? was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.