Skip to content

Add skip_if_exists argument to enqueue_trial - #3629

Merged
keisuke-umezawa merged 19 commits into
optuna:masterfrom
xadrianzetx:feat-skip-if-exists
Jun 25, 2022
Merged

keisuke-umezawa merged 19 commits into
optuna:masterfrom
xadrianzetx:feat-skip-if-exists

Conversation

@xadrianzetx

Copy link
Copy Markdown
Collaborator

Motivation

Resolves #3616.

Description of the changes

  • Implemented skip_if_exists argument in enqueue_trial
  • Added tests & updated docs

With this flag enabled, duplicate trials will not be added to queue and
warning message will be displayed when duplicate is found.
@codecov-commenter

codecov-commenter commented Jun 4, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #3629 (ba3bfa1) into master (07a8193) will increase coverage by 0.06%.
The diff coverage is 95.00%.

@@            Coverage Diff             @@
##           master    #3629      +/-   ##
==========================================
+ Coverage   90.61%   90.67%   +0.06%     
==========================================
  Files         162      162              
  Lines       12566    12669     +103     
==========================================
+ Hits        11387    11488     +101     
- Misses       1179     1181       +2     
Impacted Files Coverage Δ
optuna/study/study.py 96.10% <94.73%> (-0.13%) ⬇️
optuna/multi_objective/study.py 97.60% <100.00%> (ø)
optuna/samplers/_tpe/sampler.py 95.90% <0.00%> (-1.09%) ⬇️
optuna/study/_optimize.py 98.43% <0.00%> (-0.23%) ⬇️
optuna/study/_tell.py 97.91% <0.00%> (-0.05%) ⬇️
optuna/samplers/_grid.py 100.00% <0.00%> (ø)
optuna/integration/shap.py 100.00% <0.00%> (ø)
optuna/visualization/_pareto_front.py 97.88% <0.00%> (ø)
optuna/testing/integration.py
optuna/testing/pruner.py 100.00% <0.00%> (ø)
... and 7 more

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@HideakiImamura

Copy link
Copy Markdown
Member

@c-bata @keisuke-umezawa Could you review this PR if you have time?

This also correctly handles categorical types.
@xadrianzetx

Copy link
Copy Markdown
Collaborator Author

I had to slightly change the approach to ensure categorical types are handled correctly. Sorry for pushing changes after reviewers have been assigned.

@c-bata c-bata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for your pull request! I leave some review comments.

Comment thread optuna/study/study.py Outdated

for trial in self.get_trials(deepcopy=False):
trial_params = trial.system_attrs.get("fixed_params", trial.params)
if trial_params == params:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm wondering if we should compare float objects with math.isclose() to concider the representation error of floating point numbers. For example, RDBStorage encode/decode system_attr values via json.dumps and json.loads.

What do you think?

@xadrianzetx xadrianzetx Jun 7, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That was my initial approach, but implementation turned out a bit complicated, since we need to check for types, so I figured precision in cpython dict_equal is enough for this API. But I see where json encoding-decoding can introduce float representation related bugs, so if you don't mind slightly complicated type checking, I'm fine with bringing back implementation from 56126a2 (with less bugs this time :^)).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sounds good 👍 I think slightly complicated type checking is acceptable for this purpose.

Furthermore, float('nan') == float('nan') will be false actually. It caused a bug that is reported at #3556. To be honest, I'm not sure we should support the edge case. However, given that we've fixed #3556, it seems that we need to support the float('nan') input here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ah, yes, nan != nan. Was kinda confused when locally testing additional edge cases you've requested. I also think it should be supported as it seems to be a part of IEEE-754 standard.

Thanks for opinion, I'll push the changes after the weekend.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done in cf6810e (somehow awkwardly). PTAL @c-bata.

Comment thread tests/study_tests/test_study.py Resolved
Comment thread optuna/study/study.py Resolved
@keisuke-umezawa

Copy link
Copy Markdown
Member

Seems good to me apart from the comments from @c-bata !

@xadrianzetx

Copy link
Copy Markdown
Collaborator Author

CI failures seems to be caused by docker registry outage.

@c-bata c-bata left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the update! I leave some suggestions.

Comment thread optuna/study/study.py Outdated
# be handled regardless if `skip_if_exists` is `True`.
continue

if isinstance(param_value, (float, np.floating)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In https://github.com/optuna/optuna/pull/3567/files#r879072605, we use isinstance(..., Real) to handle float-like types like np.floating. Can we use numbers.Real here as well?

@xadrianzetx xadrianzetx Jun 14, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm afraid mypy is not happy with that change - isclose accepts just about any type, but not Real (neither does math.isclose). 😅

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, you are correct. In #3567, Real values are casted to float to call np.isnan().

value1_is_nan = isinstance(value1, Real) and np.isnan(float(value1))
value2_is_nan = isinstance(value2, Real) and np.isnan(float(value2))

Do you think it make sense to cast values with float() like:

is_repeated = (
    np.isnan(float(param_value)) or np.isclose(float(param_value), float(existing_param), atol=0.0)
    if isinstance(param_value, Real)
    else param_value == existing_param
)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sounds reasonable. Implemented in 87e3c27.

Comment thread optuna/study/study.py Resolved
Comment thread optuna/study/study.py Resolved
Comment thread optuna/multi_objective/study.py Resolved
@xadrianzetx

Copy link
Copy Markdown
Collaborator Author

While fixing CI failures I found that we were actually exiting function too early. Need to wait untill all params in trial are repeated to skip enqueue. Fixed and tested in ba3bfa1, also implementation becomes a bit cleaner. PTAL @c-bata.

@c-bata c-bata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry for the late response. Changes looks almost good to me except for #3629 (comment) 👍

@c-bata c-bata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@c-bata c-bata removed their assignment Jun 18, 2022

@keisuke-umezawa keisuke-umezawa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I just left a nits comment. Could you check it?

Comment thread optuna/study/study.py
# (e.g. trying to enqueue categorical to float param).
# We are not doing anything about it here, since sanitization should
# be handled regardless if `skip_if_exists` is `True`.
continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nits] If you add repeated_params.append(False) here, you do not need to check len(repeated_params) > 0. And you can just return all(repeated_params) at the end.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I've removed length check in 3b99b84, but otherwise early breakout logic remains unchanged, otherwise we would always return after checking only first trial.

Comment thread optuna/study/study.py Outdated
self,
params: Dict[str, Any],
user_attrs: Optional[Dict[str, Any]] = None,
skip_if_exists: bool = True,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I prefer that the default value of skip_if_exists is False because skipping an enqueued trial may not be intuitive for users.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

a82dbab 👍

Comment thread optuna/study/study.py Outdated
Comment on lines +809 to +811
warnings.warn(
f"Trial with params {params} already exists. Skipping enqueue.", RuntimeWarning
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it should be INFO because it is expected behavior.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Switched to logging.info in 37ea970 and made tests independent from logger messages in 221bd19.

@keisuke-umezawa keisuke-umezawa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Thank you for your quick updates.

@keisuke-umezawa
keisuke-umezawa merged commit efb164d into optuna:master Jun 25, 2022
@keisuke-umezawa keisuke-umezawa added this to the v3.0.0-rc0 milestone Jun 25, 2022
@xadrianzetx
xadrianzetx deleted the feat-skip-if-exists branch June 25, 2022 12:02
@HideakiImamura HideakiImamura added the feature Change that does not break compatibility, but affects the public interfaces. label Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Change that does not break compatibility, but affects the public interfaces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a skip_if_exists option to Study.enqueue_trial

6 participants