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

Implement log argument for suggest_int of pycma integration. #1302

Merged
merged 12 commits into from Jun 30, 2020

Conversation

toshihikoyanase
Copy link
Member

Motivation

This one of the follow-up PRs of #1201.

Description of the changes

This PR adds support for IntLogUniformDistribution to optuna.integration.CmaEsSampler.

I have two concerns about this implementation:

  • Although optuna.samplers.CmaEsSampler quantizes the suggested values by step in the log domain, this implementation quantizes them in the linear domain similarly to the optuna.samplers.RandomSampler. If this change is acceptable, I think I also update the optuna.samplers.CmaEsSampler.
  • When optuna.samplers.RandomSampler suggests values from DiscreteUniformDistribution, it expands low and high by 0.5 * q to make the bins of quantization equivalent to others. On the other hand, it expands 0.5 when it uses IntLogUniformDistribution. I think this is because the low can be negative depending on step. In the most cases, users will use step=1 and the calculation will be correct. So, I follow this definition in optuna.integration.CmaEsSampler, but I'm a bit concerned about performance degradation when users specify step>1.

@toshihikoyanase toshihikoyanase added enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. optuna.integration Related to the `optuna.integration` submodule. This is automatically labeled by github-actions. labels May 29, 2020
@codecov-commenter
Copy link

codecov-commenter commented May 29, 2020

Codecov Report

Merging #1302 into master will increase coverage by 1.27%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1302      +/-   ##
==========================================
+ Coverage   86.50%   87.78%   +1.27%     
==========================================
  Files          94       96       +2     
  Lines        7375     7317      -58     
==========================================
+ Hits         6380     6423      +43     
+ Misses        995      894     -101     
Impacted Files Coverage Δ
optuna/integration/cma.py 93.53% <100.00%> (-0.59%) ⬇️
optuna/samplers/cmaes.py 75.92% <0.00%> (-1.24%) ⬇️
optuna/storages/rdb/storage.py 93.73% <0.00%> (-0.34%) ⬇️
optuna/storages/base.py 67.59% <0.00%> (-0.34%) ⬇️
optuna/visualization/contour.py 98.24% <0.00%> (-0.02%) ⬇️
optuna/_experimental.py 100.00% <0.00%> (ø)
optuna/visualization/__init__.py 100.00% <0.00%> (ø)
optuna/visualization/parallel_coordinate.py 100.00% <0.00%> (ø)
optuna/visualization/param_importances.py 100.00% <0.00%> (ø)
optuna/_imports.py 96.29% <0.00%> (ø)
... and 33 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e23ef65...946cbe3. Read the comment docs.

@HideakiImamura HideakiImamura self-assigned this May 29, 2020
Copy link
Member

@HideakiImamura HideakiImamura left a comment

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 have some comments.

For the first your concern, I totally agree with you. I think we should fix optuna.samplers.CmaEsSampler as done in this PR.

# TODO(toshihikoyanase): Shiting 0.5 is not sufficient if step > 1.
lows.append(self._to_cma_params(search_space, param_name, dist.low - 0.5))
highs.append(self._to_cma_params(search_space, param_name, dist.high + 0.5))
Copy link
Member

Choose a reason for hiding this comment

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

Your second concern is reasonable: instead of expanding 0.5, we should expand 0.5*step. As you say, I think that IntLogUniformDistrbituion uses 0.5 to ensure low positive, but if we just want to ensure it, how about doing the following? I think the optuna.samplers.CmaEsSampler can be fixed as well.

Suggested change
# TODO(toshihikoyanase): Shiting 0.5 is not sufficient if step > 1.
lows.append(self._to_cma_params(search_space, param_name, dist.low - 0.5))
highs.append(self._to_cma_params(search_space, param_name, dist.high + 0.5))
# TODO(toshihikoyanase): Shiting 0.5 is not sufficient if step > 1.
lows.append(self._to_cma_params(search_space, param_name, max(0, dist.low - 0.5 * dist.step)))
highs.append(self._to_cma_params(search_space, param_name, dist.high + 0.5 * dist.step))

Copy link
Member

Choose a reason for hiding this comment

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

This change still has the problem of bias in discretization when low - 0.5 * step is negative. A possible solution is to move [low-0.5*step, high+0.5*step] parallel to [1, high - low + step + 1] for sampling when low - 0.5 * step is negative, and then undo it later. However, this is a change that has to be made to other samplers at the same time, and I think it's outside the scope of this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm sorry for the delayed response.
Your new PR (#1329) will prohibit the simultaneous use of step and log, and we can keep the master's implementation. I'll remove the TODO comment about it.

optuna/integration/cma.py Outdated Show resolved Hide resolved
optuna/integration/cma.py Outdated Show resolved Hide resolved
@HideakiImamura
Copy link
Member

Hi @toshihikoyanase! Is this PR ready for the review?

@toshihikoyanase
Copy link
Member Author

@HideakiImamura I'm sorry for the delayed response. Regarding the step of IntLogUniformDistribution, #1329 ensures that the step is always 1, and I set the range to [low-0.5, high+0.5] similar to the other samplers like SkoptSampler.
Please take another look.

Copy link
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

Thanks for the update! Basically, LGTM! I have some minor comments.

optuna/integration/cma.py Outdated Show resolved Hide resolved
optuna/integration/cma.py Outdated Show resolved Hide resolved
Copy link
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

Thanks for the update. LGTM!

@hvy hvy self-assigned this Jun 30, 2020
Copy link
Member

@hvy hvy left a comment

Choose a reason for hiding this comment

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

Thanks, basically LGTM.

if isinstance(dist, IntLogUniformDistribution):
exp_value = math.exp(cma_param_value)
r = numpy.round((exp_value - dist.low) / dist.step)
v = r * dist.step + dist.low
Copy link
Member

Choose a reason for hiding this comment

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

step has been removed (c.f. #1438) so I think we can safely assume it being 1.

optuna/integration/cma.py Outdated Show resolved Hide resolved
elif isinstance(distribution, LogUniformDistribution):
elif isinstance(distribution, LogUniformDistribution) or isinstance(
distribution, IntLogUniformDistribution
):
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

@toshihikoyanase
Copy link
Member Author

I'm sorry but I pushed the wrong branch. I'm reverting the change.

toshihikoyanase and others added 10 commits June 30, 2020 15:55
Co-authored-by: Hideaki Imamura <38826298+HideakiImamura@users.noreply.github.com>
Remove redundant logic.

Co-authored-by: Hideaki Imamura <38826298+HideakiImamura@users.noreply.github.com>
Co-authored-by: Hiroyuki Vincent Yamazaki <hiroyuki.vincent.yamazaki@gmail.com>
lows.append(dist.low - 0.5)
highs.append(dist.high + 0.5)
lows.append(dist.low - 0.5 * dist.step)
highs.append(dist.high + 0.5 * dist.step)
Copy link
Member

Choose a reason for hiding this comment

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

Just wondering but is this change somewhat orthogonal to the other?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right. It's the change for IntUniformDistribution. I reverted the change in 682e8f4. Thank you for your careful review.

Copy link
Member

Choose a reason for hiding this comment

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

Should we create a separate PR for that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for your suggestion.
I created #1456. Please review it after this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I'll have a look at it.

Copy link
Member

@hvy hvy 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 having it dragged out, LGTM!

Let me merge this PR after CI.

@hvy hvy merged commit 04adc0f into optuna:master Jun 30, 2020
@hvy hvy added this to the v2.0.0 milestone Jun 30, 2020
@toshihikoyanase toshihikoyanase deleted the add-int-loguniform-to-pycma branch March 7, 2022 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. optuna.integration Related to the `optuna.integration` 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