Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gather mask of None parameter in TPESampler #3886

Merged
merged 6 commits into from
Nov 16, 2022

Conversation

knshnb
Copy link
Member

@knshnb knshnb commented Aug 12, 2022

Motivation

Previously, in TPESampler, the mask of removing None parameters (= parameters that do not appear in the trial) was taken in two different places, which can easily cause bugs.

Description of the changes

  • Extract the corresponding logic from _build_observation_dict and _calculate_weights_below_for_multi_objective (as a consequence, build_observation_dict was removed).
  • Fix tests.

@github-actions github-actions bot added the optuna.samplers Related to the `optuna.samplers` submodule. This is automatically labeled by github-actions. label Aug 12, 2022
@HideakiImamura HideakiImamura self-assigned this Aug 16, 2022
@knshnb
Copy link
Member Author

knshnb commented Aug 16, 2022

@HideakiImamura Although I haven't modified the test yet, could you briefly check if this strategy is okay when you have time? By this PR, _sample_relative and sample_independent will get a little complex, but I'm thinking of DRYing them in another PR.

@github-actions
Copy link
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Aug 23, 2022
@knshnb knshnb removed the stale Exempt from stale bot labeling. label Aug 30, 2022
@HideakiImamura
Copy link
Member

Thanks for the PR. I think this PR is good to reduce the complexity of TPESampler. Could you fix the CI errors?

@knshnb knshnb changed the title [WIP] Gather mask of None parameter in TPESampler Gather mask of None parameter in TPESampler Aug 30, 2022
@knshnb
Copy link
Member Author

knshnb commented Aug 30, 2022

Thanks! I fixed the error. Could you take a look again and assign another reviewer?

@codecov-commenter
Copy link

codecov-commenter commented Aug 30, 2022

Codecov Report

Merging #3886 (544c0cf) into master (97f90fc) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #3886      +/-   ##
==========================================
+ Coverage   90.09%   90.11%   +0.02%     
==========================================
  Files         162      162              
  Lines       12734    12742       +8     
==========================================
+ Hits        11473    11483      +10     
+ Misses       1261     1259       -2     
Impacted Files Coverage Δ
optuna/samplers/_tpe/sampler.py 96.89% <100.00%> (-0.04%) ⬇️
optuna/integration/chainer.py 89.74% <0.00%> (ø)
optuna/integration/__init__.py 49.15% <0.00%> (ø)
optuna/_hypervolume/__init__.py 100.00% <0.00%> (ø)
optuna/multi_objective/__init__.py 100.00% <0.00%> (ø)
optuna/importance/_fanova/__init__.py 100.00% <0.00%> (ø)
optuna/integration/allennlp/__init__.py 100.00% <0.00%> (ø)
optuna/multi_objective/samplers/__init__.py 100.00% <0.00%> (ø)
optuna/multi_objective/visualization/__init__.py 100.00% <0.00%> (ø)
optuna/study/_dataframe.py 98.38% <0.00%> (+0.02%) ⬆️
... and 6 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@github-actions
Copy link
Contributor

github-actions bot commented Sep 6, 2022

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Sep 6, 2022
@c-bata c-bata assigned c-bata and contramundum53 and unassigned HideakiImamura Sep 7, 2022
@c-bata
Copy link
Member

c-bata commented Sep 7, 2022

Let me reassign reviewers since this PR has no recent activities and Imamura-san will take a vacation.

@contramundum53 Could you review this PR?

@github-actions github-actions bot removed the stale Exempt from stale bot labeling. label Sep 7, 2022
@github-actions
Copy link
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Sep 14, 2022
Copy link
Member

@contramundum53 contramundum53 left a comment

Choose a reason for hiding this comment

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

LGTM!

@contramundum53 contramundum53 removed their assignment Sep 20, 2022
@github-actions github-actions bot removed the stale Exempt from stale bot labeling. label Sep 20, 2022
@github-actions
Copy link
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Sep 28, 2022
@c-bata c-bata removed the stale Exempt from stale bot labeling. label Sep 29, 2022
Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

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

Sorry for the delayed review. I reviewed changes and leave some comments.

Comment on lines 460 to 463
param_mask = ~np.isnan(list(config_values.values())[0])
param_mask_below, param_mask_above = param_mask[indices_below], param_mask[indices_above]
below = {k: v[indices_below[param_mask_below]] for k, v in config_values.items()}
above = {k: v[indices_above[param_mask_above]] for k, v in config_values.items()}
Copy link
Member

Choose a reason for hiding this comment

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

As written in L458, values variable is a dictionary that contains only a single hyperparameter, right?
If so, I'd say we can remove L458 and replace these lines like the following.

        config_value = values["param_name"]
        param_mask = ~np.isnan(config_value)
        param_mask_below, param_mask_above = param_mask[indices_below], param_mask[indices_above]
        below = config_value[indices_below[param_mask_below]]
        above = config_value[indices_above[param_mask_above]]

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I fixed it: 527d5fb.

Comment on lines 399 to 402
# In the call of `sample_relative`, this logic makes sense because we only have the
# intersection search space or group decomposed search space. This means one parameter
# misses the one trial, then the other parameter must miss the trial, in this call of
# `sample_relative`.
Copy link
Member

Choose a reason for hiding this comment

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

This comment seems to have been added in #2688 to indicate that list(config_values.values())[0] is the correct logic.

But honestly, I don't understand what "This means one parameter misses the one trial, then the other parameter must miss the trial" means. Can you please tell me what kind of search space would include True in the param_mask? More specifically, what objective function would cause the this condition to be false?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the comment. I investigated that issue and changed the previous behavior by #4079 to make it consistent with multivariate=False.

Now, that should happen in a dynamic search space when group=True.

@github-actions
Copy link
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Oct 10, 2022
@knshnb
Copy link
Member Author

knshnb commented Oct 17, 2022

I figured out that the mask logic in sample_relative is unnecessary in the current code as pointed out by @c-bata. Let me make this PR draft until the above issue is fixed by #4079.

@knshnb knshnb marked this pull request as draft October 17, 2022 06:15
@github-actions github-actions bot removed the stale Exempt from stale bot labeling. label Oct 17, 2022
@github-actions
Copy link
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Oct 25, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2022

This pull request was closed automatically because it had not seen any recent activity. If you want to discuss it, you can reopen it freely.

@github-actions github-actions bot closed this Nov 8, 2022
@knshnb knshnb removed the stale Exempt from stale bot labeling. label Nov 11, 2022
@knshnb knshnb reopened this Nov 11, 2022
@knshnb
Copy link
Member Author

knshnb commented Nov 11, 2022

Let me reopen this PR because the concern on the current implementation (link) by @c-bata has been resolved by #4079.

@knshnb knshnb marked this pull request as ready for review November 11, 2022 08:48
@c-bata
Copy link
Member

c-bata commented Nov 11, 2022

Thanks! Let me assign @contramundum53 again.

Copy link
Member

@contramundum53 contramundum53 left a comment

Choose a reason for hiding this comment

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

The new code is easier to read for me. LGTM!

@c-bata c-bata added the code-fix Change that does not change the behavior, such as code refactoring. label Nov 14, 2022
Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

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

LGTM:+1:

@c-bata c-bata added this to the v3.1.0 milestone Nov 16, 2022
@c-bata c-bata merged commit dfb8d87 into optuna:master Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-fix Change that does not change the behavior, such as code refactoring. optuna.samplers Related to the `optuna.samplers` submodule. This is automatically labeled by github-actions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants