jrbourbeau / dask-optuna

Scale Optuna with Dask
https://jrbourbeau.github.io/dask-optuna/
MIT License
35 stars 6 forks source link

Using direction="maximize" gives incorrect best value #15

Closed Nanthini10 closed 4 years ago

Nanthini10 commented 4 years ago

Using dask_optuna storage for optimization, when I set direction to maximize, it still returns the smallest value.

import optuna
import joblib
from dask.distributed import Client
import dask_optuna

def objective(trial):
    x = trial.suggest_uniform("x", -10, 10)
    return (x - 2) ** 2

client = Client()

dask_storage = dask_optuna.DaskStorage()

study = optuna.create_study(storage=dask_storage, direction="maximize")
with joblib.parallel_backend("dask"):
    study.optimize(objective, n_trials=10, n_jobs=-1)

print(f"best_params = {study.best_params}, best_value= {study.best_value}")

# Output: best_params = {'x': 1.6826689559296248}, best_value= 0.10069899153079438

study.trials_dataframe()['value'].max(), study.trials_dataframe()['value'].min()
# Output: (109.09852105012827, 0.10069899153079438)
jrbourbeau commented 4 years ago

Thanks for raising an issue and providing a nice reproducer @Nanthini10! After running through your snippet a few times, I realized there was an issue with how we're serializing StudyDirections when passing them between the client-side DaskStorage object and the OptunaSchedulerExtension running on the scheduler. I've pushed up a fix over in https://github.com/jrbourbeau/dask-optuna/pull/16 in case you're interested in taking a look

Nanthini10 commented 4 years ago

Thanks for raising an issue and providing a nice reproducer @Nanthini10! After running through your snippet a few times, I realized there was an issue with how we're serializing StudyDirections when passing them between the client-side DaskStorage object and the OptunaSchedulerExtension running on the scheduler. I've pushed up a fix over in #16 in case you're interested in taking a look

Thanks for the quick reply. Yeah, I had a similar conclusion when I tried to debug - the direction wasn't being passed correctly. The PR fixes the issue! 👍

jrbourbeau commented 4 years ago

Awesome, thanks again for reporting the issue. I pushed dask-optuna==0.0.2 to PyPI with the fix