Why does sigmoid crossentropy of Keras/tensorflow have low precision?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with neural networks for binary classification tasks in Keras or TensorFlow, one common approach is to use the sigmoid activation function in conjunction with the binary cross-entropy loss function. While this combination is both popular and effective, users may sometimes encounter issues with numerical precision. This article explores why the sigmoid function and binary cross-entropy might suffer from low precision in Keras and TensorFlow, delving into underlying causes and providing nuanced explanations.
Technical Background
The Sigmoid Activation Function
The sigmoid function, mathematically expressed as:
squashes input values to a tight range between 0 and 1. It's commonly used for binary classification in the final layer of a neural network, converting raw model outputs (logits) into probabilities.
Binary Cross-Entropy `Loss`
The binary cross-entropy loss quantifies how well the predicted probabilities from the sigmoid match the ground truth labels, calculated as:
where is the true label and is the predicted probability.
Sources of Low Precision
Numerically Unstable Computations
Both the sigmoid and its associated cross-entropy calculations can suffer from numerical instability. These issues usually arise from the properties of floating-point arithmetic in computers.
Exponentiation in Sigmoid
• Overflow: For very large negative values of , grows exponentially which may lead to overflow errors in computing . • Underflow: For very large positive values of , approaches zero, which can lead to underflow, causing precision loss.
Logarithm in Cross-Entropy
Logarithms of very small values, as produced by extremely confident predictions (close to 0 or 1) from a sigmoid, can lead to large negative values or even precision errors in floating-point representations.
example problem
Consider a predicted output very close to 0, such as . If the true value , the loss term becomes exceedingly large, leading to precision issues.
Gradient Magnitudes
• Vanishing Gradients: Near the bounds of the sigmoid (0 and 1), the gradient (derivative of the sigmoid) becomes very small. This can hinder learning in deeper networks. • Exploding Gradients: Conversely, extremely confident wrong predictions yield large loss gradients, potentially causing unstable updates during backpropagation.
Practical Implications and Mitigation Strategies
Network Design Adjustments
• Avoid Extreme Values: Careful initialization or narrower weight distributions can prevent the sigmoid from receiving extreme input values. • Use of Batch Normalization: Applying normalization layers can mitigate shift effects, preventing the sigmoid input from becoming too extreme.
Improved `Loss` Functions
• Log-Sum-Exp Trick: This trick can be used in practice for better numerical stability, ensuring that logarithms are computed in a computationally stable manner.
Mixed Precision Training
• Mixed Precision: Utilizing NVIDIA's mixed precision training can substantially increase the robustness against numerical issues by dynamically managing precision levels during training.
Alternative Activations
• SoftPlus Activation: In some cases, replacing the sigmoid with the SoftPlus activation can be viable, as it exhibits smoother gradients near the linearity region.
Summary Table
| Problem Area | Causes | Solutions | |
| Numerical Instability | Exponential and logarithm limits | Use log-sum-exp trick, improve loss function stability | |
| Vanishing Gradients | Sigmoid near bounds | Use batch normalization, Xavier initialization | |
| Overflow/Underflow | Extreme sigmoid inputs | Careful data preprocessing, narrower weight initialization | |
| High Precision Needs | Floating-point precision limits | Mixed precision training, GPU enhancements |
In summary, while sigmoid and cross-entropy provide a straightforward way to train deep learning models for binary classification, they must be used carefully to avoid pitfalls associated with numerical precision. By understanding these intricacies and adopting suitable strategies, we can optimize performance while managing computational efficiency.

