[Python] My RandomSampler() is not work and always generate same parameters

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I used TPESampler and set it as follows while optimizing with optuna: sampler=optuna.samplers.TPESampler(multivariate=True, n_startup_trials=10, seed=None). But in the 10 startup_trials process, it always takes exactly the same parameters for the trial.

    I conducted additional experiments with RandomSampler and found that it also has the same situation, always collecting exactly the same parameters. Because I used Flax, which does not support multiple processes, I had to encapsulate the processes. If anyone could help me, I would be extremely grateful.

    My code as below.

    def multiprocessing_objective_fn(args, trial):
    queue = multiprocessing.Queue()
    p = multiprocessing.Process(target=train_agent, args=(args, trial, queue))
    p.start()
    p.join()
    result = queue.get()
    return result

    if name == "main":
    from optuna.storages import RDBStorage
    from sqlalchemy import create_engine
    import socket

    args = get_args()
    print(args)

    # Step 2: Create the engine with the specified timeout
    engine = create_engine("sqlite:///optuna_database_250710_test5" + ".db", connect_args={'timeout': 500})

    # Step 3: Use this engine to create the Optuna storage
    storage = RDBStorage("sqlite:///optuna_database_250710_test5" + ".db")

    study = optuna.create_study(
    direction="maximize",
    storage=storage,
    load_if_exists=True,
    study_name=args.exp_name + '__' + args.env_id + '__' + args.run_name + '__' + args.actor,

    pruner=optuna.pruners.HyperbandPruner()
    )
    objective_fn = functools.partial(multiprocessing_objective_fn, args)
    #objective_fn = functools.partial(train_agent, args)
    # wandb only works for n_jobs = 1 ! See README.md for more infos about that
    study.optimize(objective_fn, n_trials=args.n_trials, n_jobs=1)

    Continue reading...

Compartilhe esta Página