Skip to content

Numerical stability issues in log, exp, sigmoid, and softmax #9

Description

@github-actions

File: leanpass/tensor.py

These functions directly call NumPy's log, exp, and np.exp(-x) without guarding against domain errors or overflow:

  • log will produce -inf/nan for non‑positive inputs.
  • exp can overflow for large positive values, yielding inf.
  • sigmoid uses np.exp(-x) which overflows for large negative x.
  • softmax shifts by the max but still calls np.exp on the shifted values; extreme values can still overflow.

Fix:

  • Clamp inputs for log (e.g., np.log(np.clip(self.data, eps, None))).
  • Use np.exp(np.clip(self.data, a_min, a_max)) or np.where to avoid overflow in exp and sigmoid.
  • In softmax, after shifting, also clip the exponentials or use np.exp(np.clip(shifted, a_min, a_max)).
  • Document the expected input ranges.

File: leanpass/tensor.py

Filed automatically by ai-issue-scan.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions