Skip to content

Instantly share code, notes, and snippets.

@c-bata
Created December 24, 2025 16:46
Show Gist options
  • Select an option

  • Save c-bata/d310b6b373e03ad03be7fbda99c8bcf7 to your computer and use it in GitHub Desktop.

Select an option

Save c-bata/d310b6b373e03ad03be7fbda99c8bcf7 to your computer and use it in GitHub Desktop.
import optuna
from concurrent.futures import ProcessPoolExecutor, as_completed
from optuna.storages import JournalStorage
from optuna.storages.journal import JournalFileBackend
optuna.logging.set_verbosity(optuna.logging.WARNING)
n_processes = 8
study_name = "foo"
filename = "optuna-journal.log"
def objective(trial: optuna.Trial) -> float:
s = 0.0
for i in range(18):
trial.set_user_attr(f"attr{i}", "attr value")
for i in range(8):
s += trial.suggest_float(f"x{i}", -10, 10) ** 2
return s
def optimize(n_trials: int):
storage = JournalStorage(JournalFileBackend(filename))
study = optuna.create_study(
storage=storage,
study_name=study_name,
sampler=optuna.samplers.RandomSampler(),
load_if_exists=True
)
study.optimize(objective, n_trials=n_trials)
def main():
futures = []
with ProcessPoolExecutor(max_workers=n_processes) as pool:
for i in range(n_processes):
futures.append(pool.submit(optimize, 100))
for future in as_completed(futures):
future.result()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment