Skip to content

Fix CategoricalDistribution with NaN - #3567

Merged
knshnb merged 15 commits into
optuna:masterfrom
not522:fix-categorical-nan
Jun 6, 2022
Merged

knshnb merged 15 commits into
optuna:masterfrom
not522:fix-categorical-nan

Conversation

@not522

@not522 not522 commented May 17, 2022

Copy link
Copy Markdown
Member

Motivation

Thanks to @contramundum53's investigation, I found that CategoricalDistribution is buggy with NaN. #3556 (comment)

Description of the changes

Fix CategoricalDistribution's __eq__ and check_distribution_compatibility to handle NaN.

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])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We need to create new objects to compare NaNs.

The built-in containers typically assume identical objects are equal to themselves.

https://docs.python.org/3/reference/expressions.html#value-comparisons

>>> x = float("nan")
>>> y = float("nan")
>>> x == x
False
>>> x == y
False
>>> [x] == [x]
True
>>> [x] == [y]
False

Note 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")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note: CategoricalChoiceType is Union[None, bool, int, float, str]. _choices contains all types and their edge cases.

@not522
not522 force-pushed the fix-categorical-nan branch from 17a91ba to 4663499 Compare May 17, 2022 07:16
@not522
not522 marked this pull request as draft May 17, 2022 09:06
@not522

not522 commented May 18, 2022

Copy link
Copy Markdown
Member Author

We should fix this line, too. I'm investigating how to fail the master's implementation in the unit test.

return self.choices.index(param_value_in_external_repr)

@not522

not522 commented May 20, 2022

Copy link
Copy Markdown
Member Author

Fixed. Now, this PR is ready for review.

@not522
not522 marked this pull request as ready for review May 20, 2022 05:29
@c-bata

c-bata commented May 20, 2022

Copy link
Copy Markdown
Member

@knshnb Could you review this PR if you have the time?

@codecov-commenter

codecov-commenter commented May 20, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #3567 (692c352) into master (55ae357) will increase coverage by 0.00%.
The diff coverage is 86.20%.

@@           Coverage Diff           @@
##           master    #3567   +/-   ##
=======================================
  Coverage   90.83%   90.84%           
=======================================
  Files         161      161           
  Lines       12412    12436   +24     
=======================================
+ Hits        11275    11297   +22     
- Misses       1137     1139    +2     
Impacted Files Coverage Δ
optuna/distributions.py 96.30% <86.20%> (-1.27%) ⬇️
optuna/integration/botorch.py 98.68% <0.00%> (+0.87%) ⬆️

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

@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 left some review comments.

Comment thread optuna/distributions.py Resolved
Comment thread optuna/distributions.py Resolved
Comment thread optuna/distributions.py Resolved

@knshnb knshnb 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 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__).

Comment thread optuna/distributions.py Outdated
return False
return True

def __hash__(self) -> int:

@knshnb knshnb May 25, 2022 •

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.

This seems to be the same as BaseDistribution.__hash__()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I changed it to the recommended implementation.

https://docs.python.org/3/reference/datamodel.html#object.__hash__

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__.

@knshnb

knshnb commented May 25, 2022 •

Copy link
Copy Markdown
Member

I'm sorry, I had a misunderstanding, and please ignore the above suggestion.
It is possible to define a new class like _CategoricalChoice with __eq__() and let CategoricalDistribution.choices be Sequence[_CategoricalChoice] while keeping the original input of Sequence[CategoricalChoiceType]. However, CategoricalDistribution.choices doesn't seem to be touched from other places, so I think the current implementation is still fine!

@not522

not522 commented Jun 2, 2022

Copy link
Copy Markdown
Member Author

Thank you for your reviews! I updated this PR. PTAL.

@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! Changes looks almost good to me but I leave two suggestions.

Comment thread optuna/distributions.py Resolved
Comment thread optuna/distributions.py Resolved
@not522

not522 commented Jun 2, 2022

Copy link
Copy Markdown
Member Author

Thank you! I merged your suggestions.

@c-bata

c-bata commented Jun 2, 2022

Copy link
Copy Markdown
Member

Thanks for the update! LGTM after fixed CI issues 🙇

@not522

not522 commented Jun 3, 2022

Copy link
Copy Markdown
Member Author

Now, the CI is back to normal.

@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 6, 2022
@c-bata c-bata added this to the v3.0.0-rc0 milestone Jun 6, 2022
@c-bata c-bata added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Jun 6, 2022

@knshnb knshnb 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 updating. LGTM!

@knshnb
knshnb merged commit 23b1788 into optuna:master Jun 6, 2022
@not522
not522 deleted the fix-categorical-nan branch July 6, 2022 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants