Skip to content

Numerical instability in loss functions (cross_entropy_loss and binary_cross_entropy_loss) #33

Description

@github-actions

File: leanpass/nn.py

Both loss functions compute log of probabilities directly. When the probability is exactly 0 (possible due to underflow in softmax or sigmoid), the log returns -inf, leading to NaNs in the loss and gradients.

Enhancement: add a small epsilon (e.g., 1e-12) to the probabilities before taking the log, or use a more stable formulation such as log_softmax for cross‑entropy.

This aligns the library with typical deep‑learning expectations.

Filed automatically by ai-issue-scan.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions