pytensor
pytensor copied to clipboard
Adding equivalent to numpy.nan_to_num functionality
Adding equivalent to numpy.nan_to_num functionality (https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html) addressing issue 479 (https://github.com/pymc-devs/pytensor/issues/479).
Motivation for these changes
Requested in issue #479
Implementation details
Added nan_to_num in pytensor/tensor/math.py
Checklist
- [x] Explain motivation and implementation 👆
- [x] Make sure that the pre-commit linting/style checks pass.
- [x] Link relevant issues, preferably in nice commit messages.
- [x] The commits correspond to relevant logical changes. Note that if they don't, we will rewrite/rebase/squash the git history before merging.
- [ ] Are the changes covered by tests and docstrings?
- [x] Fill out the short summary sections 👇
Major / Breaking Changes
- ...
New features
- added nan_to_num
Bugfixes
- ...
Documentation
Example:
import pytensor
from pytensor import tensor as pt
import numpy as np
# Replace NaN
print("Replace NaN")
x = pt.dvector("x")
y = pt.log(x)
f = pytensor.function([x], y)
t = np.random.normal(size=(10,))
print("x")
print(t)
print("f = log(x)")
print(f (t))
# Test: NaN default replacement
print("\nTest: NaN default replacement")
y1 = pt.math.nan_to_num(y)
f = pytensor.function([x], y1)
print(f (t))
# Test: NaN custom replacement
print("\nTest: NaN custom replacement")
y2 = pt.math.nan_to_num(y, nan=1)
f = pytensor.function([x], y2)
print(f (t))
# Replace +INF/-INF
print("\n\nReplace +INF/-INF")
a = pt.dvector("a")
b = pt.dvector("b")
c = pt.math.true_div(a,b)
t_a = np.random.normal(size=(10,))
t_b = np.zeros((10,))
f = pytensor.function([a, b], c)
print("a")
print(t_a)
print("b")
print(t_b)
print("f = a/b")
print(f(t_a, t_b))
# Test: +INF/-INF default replacement
c1 = pt.math.nan_to_num(c)
f = pytensor.function([a, b], c1)
print("\nTest: +INF/-INF default replacement")
print(f(t_a, t_b))
# Test: +INF/-INF custom replacement
c2 = pt.math.nan_to_num(c, posinf=5, neginf=-5)
f = pytensor.function([a, b], c2)
print("\nTest: +INF/-INF custom replacement")
print(f(t_a, t_b))
Maintenance
- ...
First PR from yesterday's code sprint. Main reasons for marking as in progress included:
- Had some difficulty running pytest locally. Resulted in multiple failures (most involving missing files/config parameters) which I figured the community could help by providing the relevant pytest command to run.
- Could use advice on adding unit tests for the addition.
- Was suggested to use ifelse() by @jessegrabowski instead of traditional if statements. ifelse is currently not imported/used in the math.py file and adding it to the imports list was causing a circular reference. Any thoughts would be welcomed!
ifelse
is not appropriate because it requires a scalar condition, and here we want to work with arbitrary tensors
Hi @aspeers , are you still working on this?