In the previous article, we saw how softmax works and why it is preferred over argmax when it comes to backpropagation.
However, for backpropagation to work correctly with softmax, we need to understand another important concept.
That concept is cross entropy, which helps us measure how well a neural network fits the data.
It may sound a bit technical and might even remind you of chemistry classes, but it is actually quite simple. We will explore it step by step in this article.
Sample Dataset
We will start with a small dataset:
| Petal | Sepal | Species |
|---|---|---|
| 0.04 | 0.42 | Setosa |
| 1.00 | 0.54 | Virginica |
| 0.50 | 0.37 | Versicolor |
Assume these values are already transformed into scores (logits) for each class.
Computing Softmax
Let us compute the softmax values for the first input.
Logits:
- Setosa: 1.04
- Versicolor: 0.00
- Virginica: 0.14
The softmax formula is:
Manual Calculation
Softmax(Setosa):
Softmax(Versicolor):
Softmax(Virginica):
Softmax in Python
import numpy as np
logits = np.array([1.04, 0.0, 0.14])
exp_vals = np.exp(logits)
softmax = exp_vals / np.sum(exp_vals)
softmax
Output:
[0.57, 0.20, 0.23]
Cross Entropy
Now let us compute cross entropy.
For neural networks, cross entropy is usually simplified. If the true class is known, we only compute the negative log of the predicted probability for that class.
If the true label is Setosa, then:
Manual Calculation
Cross Entropy in Python
true_class_index = 0 # Setosa
loss = -np.log(softmax[true_class_index])
loss
Output:
0.56
In the next article, we will compute the total cross entropy loss by including the values from the other two species and see how the loss behaves across the dataset.
Looking for an easier way to install tools, libraries, or entire repositories?
Try Installerpedia: a community-driven, structured installation platform that lets you install almost anything with minimal hassle and clear, reliable guidance.
Just run:
ipm install repo-name
โฆ and youโre done! ๐







Top comments (0)