Merged
Conversation
np.isnan with math.isnan
Member
|
@fusawa-yugo @y0z @not522 Could you review this PR? |
Contributor
not522
approved these changes
May 13, 2025
Member
not522
left a comment
There was a problem hiding this comment.
LGTM! I confirmed that it works with Real values such as np.float32.
import numpy as np
import optuna
def objective(trial):
x = trial.suggest_float("x", np.float32(-10.0), np.float32(10.0))
y = trial.suggest_int("y", np.float32(-10.0), np.float32(10.0))
z = trial.suggest_categorical("z", choices=(np.float32(-10.0), np.float32(10.0)))
return x + y + z
study = optuna.create_study()
study.optimize(objective, n_trials=20)
y0z
approved these changes
May 13, 2025
Member
y0z
left a comment
There was a problem hiding this comment.
LGTM.
I confirmed that this PR's implementation is faster than master when n_trials=1000.
yozaki% git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
yozaki% time PYTHONPATH=. python tpe_test.py
PYTHONPATH=. python tpe_test.py 8.60s user 0.41s system 107% cpu 8.410 total
yozaki% gh pr checkout 6080
Switched to branch 'replace-numpy-isnan-with-math-isnan'
yozaki% time PYTHONPATH=. python tpe_test.py
PYTHONPATH=. python tpe_test.py 7.53s user 0.43s system 118% cpu 6.717 total
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Motivation
Currently,$N$ times per trial, resulting in $O(N^2)$ total calls, where $N$ is the total number of trials.
np.isnanis used to detect NaN indistributions.py, but since every value passed through is a pure Python float,math.isnanis semantically identical and much faster.This is especially critical for samplers such as
TPESamplerandBruteForceSampler, which invoketo_internal_reprDescription of the changes
np.isnanindistributions.pywithmath.isnan.Benchmarking results
I benchmarked
TPESamplerandBruteForceSamplerand confirmed a significant speedup with this change:)The result is as follows:
The solid lines denote the mean and the translucent areas denote the standard error, both computed over five independent runs with different random seeds.
The Objective function I used here is as follows:
The benchmarking code and the visualization code I used here is as follows:
Benchmarking code
Visualization code