Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nan_to_num helper #796

Merged
merged 3 commits into from
Jul 7, 2024
Merged

Add nan_to_num helper #796

merged 3 commits into from
Jul 7, 2024

Conversation

Dhruvanshu-Joshi
Copy link
Member

@Dhruvanshu-Joshi Dhruvanshu-Joshi commented Jun 1, 2024

Description

Added pytensor equivalent of numpy's nan_to_num. Also added isposinf and isneginf similar to isinf.

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

@Dhruvanshu-Joshi Dhruvanshu-Joshi changed the base branch from main to upstream_script June 1, 2024 13:15
@Dhruvanshu-Joshi Dhruvanshu-Joshi changed the base branch from upstream_script to main June 1, 2024 13:15
Copy link

codecov bot commented Jun 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.01%. Comparing base (b7b309d) to head (7c68b29).
Report is 80 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #796      +/-   ##
==========================================
+ Coverage   80.76%   81.01%   +0.24%     
==========================================
  Files         163      170       +7     
  Lines       46941    46941              
  Branches    11499    11499              
==========================================
+ Hits        37912    38029     +117     
+ Misses       6786     6705      -81     
+ Partials     2243     2207      -36     
Files Coverage Δ
pytensor/tensor/math.py 90.58% <ø> (+0.91%) ⬆️

... and 66 files with indirect coverage changes

isposinf = IsPosInf()


class IsNegInf(FixedLogicalComparison):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need these custom Ops, or can we just use helper functions like nan_to_num such as pt.eq(x, -np.inf)?

In general we want to have as little Ops as possible, and just reuse what we already have.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just referred to this conversation and implemented the two ops. This can be done even without them. Should I omit these in the next commit?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, no new Ops is always the default and preferred strategy

Comment on lines 3080 to 3081
is_pos_inf = eq(x, np.inf)
is_neg_inf = eq(x, -np.inf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also add the numpy helpers for isneginf, isposinf for users (and use them here)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created Isposinf and Isneginf with the very same purpose here:
Dhruvanshu-Joshi@829309b

How can we use np.isposinf(x) directly in helper functions without creating an op when x is but a container?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't, you use the pt.isponsinf helper which will have this exact code inside pt.eq(pt.as_tensor(x), np.inf).

pytensor/tensor/math.py Outdated Show resolved Hide resolved
pytensor/tensor/math.py Outdated Show resolved Hide resolved
@ricardoV94
Copy link
Member

The new test is failing in the floatX="float32" runs (happens all the time and it's super annoying): https://github.com/pymc-devs/pytensor/actions/runs/9603508611/job/26486909276?pr=796

@Dhruvanshu-Joshi
Copy link
Member Author

The new test is failing in the floatX="float32" runs (happens all the time and it's super annoying): https://github.com/pymc-devs/pytensor/actions/runs/9603508611/job/26486909276?pr=796

Added the allow_input_downcast=True again


f = function([x], out, allow_input_downcast=True)

y = np.array([1, 2, np.nan, np.inf, -np.inf, 3, 4])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would solve the failing float32 test without having to downcast the input

Suggested change
y = np.array([1, 2, np.nan, np.inf, -np.inf, 3, 4])
y = np.array([1, 2, np.nan, np.inf, -np.inf, 3, 4]).astype(x.dtype)

@ricardoV94 ricardoV94 added enhancement New feature or request NumPy compatibility labels Jul 6, 2024
@ricardoV94 ricardoV94 changed the title Convert nan tensors to num Add nan_to_num helper Jul 6, 2024
@jessegrabowski
Copy link
Member

Is this ready to merge?

@ricardoV94 ricardoV94 merged commit ca10298 into pymc-devs:main Jul 7, 2024
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement equivalent to numpy.nan_to_num
3 participants