Skip to content

Refactor batched distribution classes to reduce branch duplication - #6689

Merged
kAIto47802 merged 7 commits into
optuna:masterfrom
nabenabe0928:refactor-tpe-prob-dist
Jun 17, 2026
Merged

kAIto47802 merged 7 commits into
optuna:masterfrom
nabenabe0928:refactor-tpe-prob-dist

Conversation

@nabenabe0928

@nabenabe0928 nabenabe0928 commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

This PR simplifies the routine of probability_distributions.py for TPE.

This PR relates to:

@nabenabe0928

Copy link
Copy Markdown
Contributor Author

@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions Bot added the stale Exempt from stale bot labeling. label Jun 2, 2026
@github-actions github-actions Bot removed the stale Exempt from stale bot labeling. label Jun 7, 2026
@c-bata

c-bata commented Jun 9, 2026

Copy link
Copy Markdown
Member

@not522 @kAIto47802 Could you review this PR?

@not522 not522 added the code-fix Change that does not change the behavior, such as code refactoring. label Jun 15, 2026

@not522 not522 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

@not522 not522 removed their assignment Jun 17, 2026

@kAIto47802 kAIto47802 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for the PR. LGTM!

I’ve confirmed that the changes are logically correct and produce identical values in TPESampler:

from __future__ import annotations

import math

import optuna


def multi_objective(trial: optuna.Trial) -> tuple[float, float]:
    x1 = trial.suggest_int("x1", -5, 10)
    x2 = trial.suggest_int("x2", 1, 1000, log=True)
    x3 = trial.suggest_int("x3", -5, 10, step=2)
    x4 = trial.suggest_float("x4", -5, 10)
    x5 = trial.suggest_float("x5", 1, 1000, log=True)
    x6 = trial.suggest_float("x6", -5, 10, step=2)
    x7 = trial.suggest_categorical("x7", ["a", "b", "c"])
    return x1**2 + math.log(x2) + x3**2, x4**2 + math.log(x5) + x6**2 + ord(x7)

def single_objective(trial: optuna.Trial) -> float:
    return sum(multi_objective(trial))


def print_study_info(study: optuna.Study) -> None:
    for t in study.trials:
        print(t.params)
        print(t.distributions)
        print(t.values)

sampler = optuna.samplers.TPESampler(seed=42)
study = optuna.create_study(sampler=sampler)
study.optimize(single_objective, n_trials=100)
print_study_info(study)

sampler = optuna.samplers.TPESampler(seed=42)
study = optuna.create_study(sampler=sampler, directions=["minimize", "minimize"])
study.optimize(multi_objective, n_trials=100)
print_study_info(study)

sampler = optuna.samplers.TPESampler(seed=42, multivariate=True)
study = optuna.create_study(sampler=sampler, directions=["minimize", "minimize"])
study.optimize(multi_objective, n_trials=100)
print_study_info(study)
#!/bin/bash
set -xeuo pipefail


git switch master
base_log="master-$(git rev-parse --short HEAD).txt"
python check.py >"${base_log}"

git switch nabenabe0928/refactor-tpe-prob-dist
target_log="refactor-tpe-prob-dist-$(git rev-parse --short HEAD).txt"
python check.py >"${target_log}"

if cmp -s "${base_log}" "${target_log}"; then
  echo "Both branches produce the same output."
  exit 0
else
  echo "The outputs differ between the branches."
  diff -u "${base_log}" "${target_log}" | head -100
  exit 1
fi
Both branches produce the same output.

@kAIto47802
kAIto47802 merged commit 70e041f into optuna:master Jun 17, 2026
12 checks passed
@kAIto47802 kAIto47802 added this to the v5.0.0 milestone Jun 24, 2026
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.

4 participants