Skip to content

Introduce the independent sampling warning template#6188

Merged
nabenabe0928 merged 5 commits intooptuna:masterfrom
nabenabe0928:code-fix/bundle-independent-sampling-warning
Jul 3, 2025
Merged

Introduce the independent sampling warning template#6188
nabenabe0928 merged 5 commits intooptuna:masterfrom
nabenabe0928:code-fix/bundle-independent-sampling-warning

Conversation

@nabenabe0928
Copy link
Copy Markdown
Contributor

Motivation

This PR refactors the warnings related to this issue;

Description of the changes

  • Bundle the warning message in a place
  • Use the template

@nabenabe0928 nabenabe0928 changed the title Code fix/bundle independent sampling warning Introduce the independent sampling warning template Jul 1, 2025
@nabenabe0928 nabenabe0928 added the code-fix Change that does not change the behavior, such as code refactoring. label Jul 1, 2025
@nabenabe0928 nabenabe0928 added this to the v4.5.0 milestone Jul 1, 2025
@nabenabe0928
Copy link
Copy Markdown
Contributor Author

@c-bata @fusawa-yugo @sawa3030

Could you review this PR?

@nabenabe0928 nabenabe0928 marked this pull request as ready for review July 1, 2025 05:01
@nabenabe0928
Copy link
Copy Markdown
Contributor Author

import optuna


def objective(trial: optuna.Trial) -> float:
    x = trial.suggest_float("x", -5, 5)
    y = trial.suggest_float("y", -5, 5)
    if trial.number % 2 == 1:
        z = trial.suggest_float("z", -5, 5)
    return x**2 + y**2


samplers = [
    optuna.samplers.TPESampler(multivariate=True, n_startup_trials=1),
    optuna.samplers.QMCSampler(),
    optuna.samplers.CmaEsSampler(popsize=2),
]
for sampler in samplers:
    study = optuna.create_study(sampler=sampler)
    print(sampler.__class__.__name__)
    study.optimize(objective, n_trials=10)

Obrained log

[I 2025-07-01 07:06:55,608] A new study created in memory with name: no-name-edc9a93f-6045-4c4b-b118-91ea98ffced3
TPESampler
[I 2025-07-01 07:06:55,609] Trial 0 finished with value: 0.04633574737331082 and parameters: {'x': 0.12066026736478541, 'y': 0.17826061610117172}. Best is trial 0 with value: 0.04633574737331082.
[I 2025-07-01 07:06:55,613] Trial 1 finished with value: 0.4162103448759381 and parameters: {'x': 0.39543433154491797, 'y': -0.5097470297231382, 'z': -3.982494099322823}. Best is trial 0 with value: 0.04633574737331082.
[I 2025-07-01 07:06:55,615] Trial 2 finished with value: 23.69569333242624 and parameters: {'x': -1.849094064384325, 'y': 4.502948420033801}. Best is trial 0 with value: 0.04633574737331082.
[W 2025-07-01 07:06:55,618] The parameter `z` in Trial#3 is sampled independently using `RandomSampler` instead of `TPESampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space is not supported for `multivariate=True`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `TPESampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,619] Trial 3 finished with value: 23.87845285266918 and parameters: {'x': -4.069955587808934, 'y': -2.7044249603070916, 'z': 4.794344532233806}. Best is trial 0 with value: 0.04633574737331082.
[I 2025-07-01 07:06:55,621] Trial 4 finished with value: 23.39913568479115 and parameters: {'x': 4.83655428444309, 'y': -0.08293574878386697}. Best is trial 0 with value: 0.04633574737331082.
[W 2025-07-01 07:06:55,624] The parameter `z` in Trial#5 is sampled independently using `RandomSampler` instead of `TPESampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space is not supported for `multivariate=True`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `TPESampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,625] Trial 5 finished with value: 26.696393484156438 and parameters: {'x': 3.5391371083640357, 'y': 3.7644258542780586, 'z': 0.1809634932783732}. Best is trial 0 with value: 0.04633574737331082.
[I 2025-07-01 07:06:55,628] Trial 6 finished with value: 33.66496551926208 and parameters: {'x': 3.4037224309192418, 'y': -4.698897650781435}. Best is trial 0 with value: 0.04633574737331082.
[W 2025-07-01 07:06:55,631] The parameter `z` in Trial#7 is sampled independently using `RandomSampler` instead of `TPESampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space is not supported for `multivariate=True`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `TPESampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,632] Trial 7 finished with value: 11.716454641007923 and parameters: {'x': -0.4289044229559577, 'y': -3.3959528319717194, 'z': 0.05912067715973031}. Best is trial 0 with value: 0.04633574737331082.
[I 2025-07-01 07:06:55,635] Trial 8 finished with value: 17.993389573812188 and parameters: {'x': -3.999723171433253, 'y': 1.412658531178752}. Best is trial 0 with value: 0.04633574737331082.
[W 2025-07-01 07:06:55,637] The parameter `z` in Trial#9 is sampled independently using `RandomSampler` instead of `TPESampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space is not supported for `multivariate=True`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `TPESampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,638] Trial 9 finished with value: 1.8433202543326244 and parameters: {'x': -0.04430176046212424, 'y': 1.3569663254305837, 'z': 2.2038637100298333}. Best is trial 0 with value: 0.04633574737331082.
[I 2025-07-01 07:06:55,639] A new study created in memory with name: no-name-d698e1d8-cbe9-4a80-b39c-dc01d006913b
QMCSampler
[I 2025-07-01 07:06:55,639] Trial 0 finished with value: 9.34758610558263 and parameters: {'x': -2.962222565273378, 'y': 0.7568510945805906}. Best is trial 0 with value: 9.34758610558263.
[W 2025-07-01 07:06:55,858] The parameter `z` in Trial#1 is sampled independently using `RandomSampler` instead of `QMCSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `QMCSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `QMCSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,859] Trial 1 finished with value: 50.0 and parameters: {'x': -5.0, 'y': -5.0, 'z': 0.7861537536905647}. Best is trial 0 with value: 9.34758610558263.
[I 2025-07-01 07:06:55,859] Trial 2 finished with value: 0.0 and parameters: {'x': 0.0, 'y': 0.0}. Best is trial 2 with value: 0.0.
[W 2025-07-01 07:06:55,859] The parameter `z` in Trial#3 is sampled independently using `RandomSampler` instead of `QMCSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `QMCSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `QMCSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,859] Trial 3 finished with value: 12.5 and parameters: {'x': 2.5, 'y': -2.5, 'z': -1.3242496257859058}. Best is trial 2 with value: 0.0.
[I 2025-07-01 07:06:55,860] Trial 4 finished with value: 12.5 and parameters: {'x': -2.5, 'y': 2.5}. Best is trial 2 with value: 0.0.
[W 2025-07-01 07:06:55,860] The parameter `z` in Trial#5 is sampled independently using `RandomSampler` instead of `QMCSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `QMCSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `QMCSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,860] Trial 5 finished with value: 3.125 and parameters: {'x': -1.25, 'y': -1.25, 'z': -2.7891677492803337}. Best is trial 2 with value: 0.0.
[I 2025-07-01 07:06:55,860] Trial 6 finished with value: 28.125 and parameters: {'x': 3.75, 'y': 3.75}. Best is trial 2 with value: 0.0.
[W 2025-07-01 07:06:55,860] The parameter `z` in Trial#7 is sampled independently using `RandomSampler` instead of `QMCSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `QMCSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `QMCSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,860] Trial 7 finished with value: 15.625 and parameters: {'x': 1.25, 'y': -3.75, 'z': 0.16477497373272865}. Best is trial 2 with value: 0.0.
[I 2025-07-01 07:06:55,860] Trial 8 finished with value: 15.625 and parameters: {'x': -3.75, 'y': 1.25}. Best is trial 2 with value: 0.0.
[W 2025-07-01 07:06:55,861] The parameter `z` in Trial#9 is sampled independently using `RandomSampler` instead of `QMCSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `QMCSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `QMCSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,861] Trial 9 finished with value: 13.28125 and parameters: {'x': -3.125, 'y': -1.875, 'z': 1.5290615701757346}. Best is trial 2 with value: 0.0.
[I 2025-07-01 07:06:55,861] A new study created in memory with name: no-name-2a16056d-fddc-4d3d-a64d-8854ced1a288
CmaEsSampler
[I 2025-07-01 07:06:55,861] Trial 0 finished with value: 2.7067466219200575 and parameters: {'x': -1.6198644383165108, 'y': -0.2877252567947277}. Best is trial 0 with value: 2.7067466219200575.
[W 2025-07-01 07:06:55,863] The parameter `z` in Trial#1 is sampled independently using `RandomSampler` instead of `CmaEsSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `CmaEsSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `CmaEsSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,863] Trial 1 finished with value: 7.582984484434924 and parameters: {'x': -2.1189011307632457, 'y': -1.7587616332195681, 'z': 2.8452300535487325}. Best is trial 0 with value: 2.7067466219200575.
[I 2025-07-01 07:06:55,864] Trial 2 finished with value: 1.2071233687675391 and parameters: {'x': 1.0949347287065372, 'y': 0.09078165365249191}. Best is trial 2 with value: 1.2071233687675391.
[W 2025-07-01 07:06:55,864] The parameter `z` in Trial#3 is sampled independently using `RandomSampler` instead of `CmaEsSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `CmaEsSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `CmaEsSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,864] Trial 3 finished with value: 2.906472485689223 and parameters: {'x': 1.7008223187059857, 'y': -0.11694411434876706, 'z': -2.4205343506544663}. Best is trial 2 with value: 1.2071233687675391.
[I 2025-07-01 07:06:55,865] Trial 4 finished with value: 0.011137087297300854 and parameters: {'x': 0.10549324170031227, 'y': 0.0028745874243707448}. Best is trial 4 with value: 0.011137087297300854.
[W 2025-07-01 07:06:55,866] The parameter `z` in Trial#5 is sampled independently using `RandomSampler` instead of `CmaEsSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `CmaEsSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `CmaEsSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,866] Trial 5 finished with value: 1.2644498487781717 and parameters: {'x': 0.9154506492620804, 'y': 0.652993076183666, 'z': -2.4852580524551793}. Best is trial 4 with value: 0.011137087297300854.
[I 2025-07-01 07:06:55,866] Trial 6 finished with value: 3.205089277944816 and parameters: {'x': 0.30710046913622246, 'y': -1.7637399410914094}. Best is trial 4 with value: 0.011137087297300854.
[W 2025-07-01 07:06:55,867] The parameter `z` in Trial#7 is sampled independently using `RandomSampler` instead of `CmaEsSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `CmaEsSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `CmaEsSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,867] Trial 7 finished with value: 5.105197431828331 and parameters: {'x': 1.374187557468539, 'y': 1.7935456472381688, 'z': -3.073449792285553}. Best is trial 4 with value: 0.011137087297300854.
[I 2025-07-01 07:06:55,868] Trial 8 finished with value: 0.019021640558474943 and parameters: {'x': -0.03611262490849931, 'y': -0.13310717065843214}. Best is trial 4 with value: 0.011137087297300854.
[W 2025-07-01 07:06:55,868] The parameter `z` in Trial#9 is sampled independently using `RandomSampler` instead of `CmaEsSampler`, potentially degrading the optimization performance. This fallback happend because dynamic search space and `CategoricalDistribution` are not supported by `CmaEsSampler`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `CmaEsSampler` if this independent sampling is intended behavior.
[I 2025-07-01 07:06:55,868] Trial 9 finished with value: 0.8246624426031802 and parameters: {'x': 0.8775346454097672, 'y': -0.23365656145020708, 'z': 1.6923347958401145}. Best is trial 4 with value: 0.011137087297300854.

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Collaborator

@sawa3030 sawa3030 left a comment

Choose a reason for hiding this comment

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

LGTM

@nabenabe0928 nabenabe0928 merged commit 99af8a0 into optuna:master Jul 3, 2025
14 checks passed
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants