What is the problem with my implementation of the cross-entropy function?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When a hand-written cross-entropy function behaves strangely, the issue is usually not the formula itself. The usual problems are feeding it the wrong inputs, missing numerical stability safeguards, or mixing up binary cross-entropy with multiclass cross-entropy.
The fastest way to debug it is to ask three questions: are these probabilities or logits, is the target format correct, and can any log(0) happen? Most broken implementations fail one of those checks.
Start With the Correct Formula
For binary classification, cross-entropy for one example is:
- (y * log(p) + (1 - y) * log(1 - p))
where:
- '
yis0or1' - '
pis the predicted probability of class1'
A simple NumPy implementation looks like this:
If your code differs substantially from that structure, there is a good chance the implementation bug is in the math rather than elsewhere in the training loop.
Logits and Probabilities Are Not the Same Input
One of the most common mistakes is feeding raw logits into a formula that expects probabilities. A logit can be any real number, but log(p) expects a value strictly between 0 and 1.
For example, this is wrong:
If your model outputs logits, convert them first:
- use sigmoid for binary classification
- use softmax for multiclass classification
Or better, use a framework loss function that accepts logits directly and handles the stable transformation internally.
Numerical Stability Matters
Even if you pass probabilities, exact 0 or 1 values cause trouble:
- '
log(0)is undefined' - values extremely close to
0can explode the loss
That is why clipping or an equivalent stable formulation is standard practice. In the example above, np.clip prevents y_pred from reaching invalid boundaries.
This is also why framework losses are often safer than manual formulas. Functions such as TensorFlow's sigmoid cross-entropy with logits or PyTorch's cross-entropy loss combine the nonlinear transformation with the loss in a numerically stable way.
Match the Target Format to the Loss
Another common bug is using the wrong target encoding. Binary cross-entropy expects scalar probabilities for a binary target. Multiclass cross-entropy expects either:
- one-hot targets with class probabilities
- or integer class indices, depending on the framework API
A simple multiclass version with one-hot targets looks like this:
If y_true has shape (batch,) but your formula expects one-hot rows, the result will be wrong even if the code runs.
Trust the Reference Implementation
When debugging, compare your manual function against a well-tested library on the same small input. If the numbers differ, use that minimal example to isolate the bug before touching the model code.
For many projects, the best answer is not "fix the handwritten loss forever" but "use the framework's implementation and move on." Manual loss code is useful for learning and debugging, but production training loops rarely need a custom cross-entropy from scratch.
Common Pitfalls
- Passing logits into a formula that expects probabilities.
- Forgetting the negative sign, which flips the optimization objective.
- Allowing
0or1probabilities and triggering unstable logs. - Mixing binary and multiclass formulas.
- Using target tensors whose shape or encoding does not match the loss function.
Summary
- Most broken cross-entropy implementations fail because of input type, target format, or numerical stability.
- Know whether your model output is logits or probabilities.
- Clip probabilities or use a stable logits-based framework loss.
- Match binary targets to binary cross-entropy and multiclass targets to multiclass cross-entropy.
- When in doubt, compare against a trusted library implementation on a tiny test case.
Related reading
- What is the proper use of Tensorflow dataset prefetch and cache options?
- What is the proper way to benchmark part of tensorflow graph?
- What is the proper way to install TensorFlow on Apple M1 in 2022
- What is the proper way to weight decay for Adam Optimizer
- What is the proper way to check for null values?
- What is the proper way to rethrow an exception in C?
- What is the purpose of graph collections in TensorFlow?
- What is the purpose of tf.compat?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.