optuna / optuna

A hyperparameter optimization framework
https://optuna.org
MIT License
10.87k stars 1.03k forks source link

Multi-objective Optimization with Optuna #5604

Closed krishdotn1 closed 3 months ago

krishdotn1 commented 3 months ago

Expected behavior

I found on doc that multi-object optimization is possible. image

but it show me an error image

below is my actual code.

study = optuna.create_study( storage="sqlite:///./db.sqlite3", study_name=tb_time, direction=["maximize", "minimize"], pruner=optuna.pruners.MedianPruner() ) study.optimize(objective, n_trials=100, n_jobs=8)

Environment

Error messages, stack traces, or logs

ValueError                                Traceback (most recent call last)
Cell In[7], line 1
----> 1 study = optuna.create_study(
      2     storage="sqlite:///./db.sqlite3", 
      3     study_name=tb_time,
      4     direction=["maximize", "minimize"],
      5     pruner=optuna.pruners.MedianPruner()
      6 )
      7 study.optimize(objective, n_trials=1, n_jobs=n_cpu)

File c:\Users\krish\anaconda3\envs\finrl\lib\site-packages\optuna\_convert_positional_args.py:83, in convert_positional_args.<locals>.converter_decorator.<locals>.converter_wrapper(*args, **kwargs)
     77     raise TypeError(
     78         f"{func.__name__}() got multiple values for arguments {duplicated_kwds}."
     79     )
     81 kwargs.update(inferred_kwargs)
---> 83 return func(**kwargs)

File c:\Users\krish\anaconda3\envs\finrl\lib\site-packages\optuna\study\study.py:1240, in create_study(storage, sampler, pruner, study_name, direction, load_if_exists, directions)
   1235     raise ValueError("The number of objectives must be greater than 0.")
   1236 elif any(
   1237     d not in ["minimize", "maximize", StudyDirection.MINIMIZE, StudyDirection.MAXIMIZE]
   1238     for d in directions
   1239 ):
-> 1240     raise ValueError(
...
   1246     d if isinstance(d, StudyDirection) else StudyDirection[d.upper()] for d in directions
   1247 ]
   1249 storage = storages.get_storage(storage)

ValueError: Please set either 'minimize' or 'maximize' to direction. You can also set the corresponding `StudyDirection` member.

Steps to reproduce

1. 2. 3.

# python code

Additional context (optional)

No response

y0z commented 3 months ago

Thank you for your comment.

I found two issues in your code.

study = optuna.create_study(
storage="sqlite:///./db.sqlite3",
study_name=tb_time,
direction=["maximize", "minimize"],
pruner=optuna.pruners.MedianPruner()
)
study.optimize(objective, n_trials=100, n_jobs=8)
  1. direction=["maximize", "minimize"], should be directions=["maximize", "minimize"], (cf. reference)
  2. Pruners cannot be used for multi-objective optimization because it is not supported at the moment (cf. reference, #3450).

Please try again after resolving these issues.

krishdotn1 commented 3 months ago

Thank you! it's work. :)