Repository navigation
Add skip_if_exists argument to enqueue_trial - #3629
Conversation
With this flag enabled, duplicate trials will not be added to queue and warning message will be displayed when duplicate is found.
Codecov Report
@@ 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
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
|
@c-bata @keisuke-umezawa Could you review this PR if you have time? |
This also correctly handles categorical types.
|
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
left a comment
There was a problem hiding this comment.
Thank you for your pull request! I leave some review comments.
|
|
||
| for trial in self.get_trials(deepcopy=False): | ||
| trial_params = trial.system_attrs.get("fixed_params", trial.params) | ||
| if trial_params == params: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :^)).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Seems good to me apart from the comments from @c-bata ! |
|
CI failures seems to be caused by docker registry outage. |
| # be handled regardless if `skip_if_exists` is `True`. | ||
| continue | ||
|
|
||
| if isinstance(param_value, (float, np.floating)): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I'm afraid mypy is not happy with that change - isclose accepts just about any type, but not Real (neither does math.isclose). 😅
There was a problem hiding this comment.
Yes, you are correct. In #3567, Real values are casted to float to call np.isnan().
optuna/optuna/distributions.py
Lines 464 to 465 in b213753
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
)There was a problem hiding this comment.
Sounds reasonable. Implemented in 87e3c27.
Co-authored-by: Masashi Shibata <[email protected]>
Co-authored-by: Masashi Shibata <[email protected]>
c-bata
left a comment
There was a problem hiding this comment.
Sorry for the late response. Changes looks almost good to me except for #3629 (comment) 👍
keisuke-umezawa
left a comment
There was a problem hiding this comment.
I just left a nits comment. Could you check it?
| # (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 |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
| self, | ||
| params: Dict[str, Any], | ||
| user_attrs: Optional[Dict[str, Any]] = None, | ||
| skip_if_exists: bool = True, |
There was a problem hiding this comment.
I prefer that the default value of skip_if_exists is False because skipping an enqueued trial may not be intuitive for users.
| warnings.warn( | ||
| f"Trial with params {params} already exists. Skipping enqueue.", RuntimeWarning | ||
| ) |
There was a problem hiding this comment.
I think it should be INFO because it is expected behavior.
keisuke-umezawa
left a comment
There was a problem hiding this comment.
LGTM! Thank you for your quick updates.
Motivation
Resolves #3616.
Description of the changes
skip_if_existsargument inenqueue_trial