Repository navigation
Fix CategoricalDistribution with NaN - #3567
Conversation
| for key in EXAMPLE_JSONS: | ||
| distributions.check_distribution_compatibility( | ||
| EXAMPLE_DISTRIBUTIONS[key], EXAMPLE_DISTRIBUTIONS[key] | ||
| EXAMPLE_DISTRIBUTIONS[key], distributions.json_to_distribution(EXAMPLE_JSONS[key]) |
There was a problem hiding this comment.
We need to create new objects to compare NaNs.
The built-in containers typically assume identical objects are equal to themselves.
>>> x = float("nan")
>>> y = float("nan")
>>> x == x
False
>>> x == y
False
>>> [x] == [x]
True
>>> [x] == [y]
FalseNote that copy.deepcopy does not create new object because float("nan") is immutable.
>>> [x] == [copy.deepcopy(x)]
True| from optuna import distributions | ||
|
|
||
|
|
||
| _choices = (None, True, False, 0, 1, 0.0, 1.0, float("nan"), float("inf"), -float("inf"), "", "a") |
There was a problem hiding this comment.
Note: CategoricalChoiceType is Union[None, bool, int, float, str]. _choices contains all types and their edge cases.
17a91ba to
4663499
Compare
|
We should fix this line, too. I'm investigating how to fail the master's implementation in the unit test. optuna/optuna/distributions.py Line 497 in b099d23 |
|
Fixed. Now, this PR is ready for review. |
|
@knshnb Could you review this PR if you have the time? |
Codecov Report
@@ Coverage Diff @@
## master #3567 +/- ##
=======================================
Coverage 90.83% 90.84%
=======================================
Files 161 161
Lines 12412 12436 +24
=======================================
+ Hits 11275 11297 +22
- Misses 1137 1139 +2
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
c-bata
left a comment
There was a problem hiding this comment.
Thank you for your pull request. I left some review comments.
There was a problem hiding this comment.
Thanks for the PR!
I'm not sure I fully understand the background, but how about making CategoricalChoiceType a class and defining __eq__() for that? It is used in other places and calling _nanequal() if we want to compare them might not be clear. I think, this way, we can also make CategoricalDistribution.__eq__() simpler (basically just compare self.__dict__ and other.__dict__).
| return False | ||
| return True | ||
|
|
||
| def __hash__(self) -> int: |
There was a problem hiding this comment.
This seems to be the same as BaseDistribution.__hash__()?
There was a problem hiding this comment.
Yes, I changed it to the recommended implementation.
If a class that overrides
__eq__()needs to retain the implementation of__hash__()from a parent class, the interpreter must be told this explicitly by setting__hash__ = <ParentClass>.__hash__.
|
I'm sorry, I had a misunderstanding, and please ignore the above suggestion. |
Co-authored-by: Masashi Shibata <[email protected]>
|
Thank you for your reviews! I updated this PR. PTAL. |
c-bata
left a comment
There was a problem hiding this comment.
Thanks for the update! Changes looks almost good to me but I leave two suggestions.
Co-authored-by: Masashi Shibata <[email protected]>
|
Thank you! I merged your suggestions. |
|
Thanks for the update! LGTM after fixed CI issues 🙇 |
|
Now, the CI is back to normal. |
Motivation
Thanks to @contramundum53's investigation, I found that
CategoricalDistributionis buggy with NaN. #3556 (comment)Description of the changes
Fix
CategoricalDistribution's__eq__andcheck_distribution_compatibilityto handle NaN.