Repository navigation
Refactor batched distribution classes to reduce branch duplication - #6689
Merged
Merged
Conversation
Contributor
Author
|
This module can be further refactored like this as well: |
Contributor
|
This pull request has not seen any recent activity. |
Member
|
@not522 @kAIto47802 Could you review this PR? |
kAIto47802
approved these changes
Jun 17, 2026
kAIto47802
left a comment
Collaborator
There was a problem hiding this comment.
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
fiBoth branches produce the same output.
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This PR simplifies the routine of probability_distributions.py for TPE.
This PR relates to:
TPESampler#6258