
We mostly talk about two broad tasks in machine learning, which are Classification and Regression. Classification forms the basis of many tasks we perform, from recognizing images to being the backbone of large language model training. Yes! Even in LLM training, classification happens at multiple levels, typically optimized using the well-known classification loss: Cross Entropy.
Let’s take one example. How will I classify an image into a particular class? A CNN just gives me the feature vector of the image. What is my next step so that the classification actually works and does not suck?
Binary Cross Entropy
When we have only two classes of data, say images of Ari and Octopus, we can use Binary Cross Entropy Loss.
And the loss is,

To understand this loss, we need to understand -log(p) first!
We want our model to give high probability to the correct class. If the image is Ari, the probabilty should be high for the class of Ari.
In other words, the model should be trained in such a way. If the model says p=0.98, that’s amazing! If it says p=0.1, that’s terrible.
Let’s see what happens when we plug in different p values.
Now, we know what -log(p) does. Let’s just fit this understanding into the Binary Cross Entropy Loss Function.

As we said, we have two classes (1 and 0).
If the true label is 1 (Ari), then only the first term matters: −log(p) and the second term becomes zero. So, the loss punishes low p values, the model must make p → 1.
If the true label is 0 (Octopus), then only the second term matters: −log(1−p) and the first term disappears. Now, the loss punishes high p values, the model must make p → 0.
We add both terms because we don’t know in advance whether the label will be 0 or 1. It is just a smart way to combine both cases into one single equation. Depending on whether y=1 or y=0, one term automatically becomes active, and the other goes to zero.
That’s BCE Loss!
Cross Entropy
Alright, Binary Cross Entropy worked great when we had only two classes: Ari and Octopus. But what if we now have Ari, Octopus, and StarFish?

If we expand this, it’s just y₁log(p₁) + y₂log(p₂) + y₃log(p₃) + … + y꜀log(p꜀). Now, if the class is Ari, how are we going to zero out Octopus and StarFish?
Well! We have something called One-Hot Encoding. For each class, we have a vector filled with zeros except for a 1 at the index of the correct class. That means:
If it’s Ari, the vector is [1 0 0]ᵀ If it’s Octopus, it’s [0 1 0]ᵀ If it’s StarFish, it’s [0 0 1]ᵀ
When we multiply y₁, y₂, y₃ with their respective log(p) values, only the true class’s term survives, the rest all become zero.
For example, if the true class is Ari, the one-hot vector is [1 0 0]ᵀ. Substituting:
Loss = -(1·log(p₁) + 0·log(p₂) + 0·log(p₃)) Loss = -log(p₁)
Only the term corresponding to the true class survives, the rest become zero. That’s how Cross Entropy automatically focuses on the correct class without us manually handling the others.
After training with Cross Entropy Loss, the model learns to assign high probability to the correct class and very low probabilities to all other classes. In other words, for a given input, the output probability distribution becomes sharp and confident, with the true class close to 1 and the rest close to 0. This is the result of the model learning from the loss function over many examples.
Note: The model won’t give probability values itself; we use a softmax over the logits the model gives us.
Comments