Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel runs for Optuna #166

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/optimization/conditional_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
train_data, test_data = train_test_split(data, test_size=0.2, stratify=data["TARGET"], random_state=42)


# replacing default _sample function in OptunaTuner class with this function
def sample(optimization_search_space, trial, suggested_params):
trial_values = copy.copy(suggested_params)
trial_values["feature_fraction"] = trial.suggest_uniform("feature_fraction", low=0.5, high=1.0)
Expand Down
7 changes: 3 additions & 4 deletions examples/optimization/custom_search_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
from sklearn.model_selection import train_test_split

from lightautoml.automl.presets.tabular_presets import TabularAutoML
from lightautoml.ml_algo.tuning.base import Distribution
from lightautoml.ml_algo.tuning.base import SearchSpace
from lightautoml.tasks import Task
from lightautoml.ml_algo.tuning.base import Uniform


# load and prepare data
Expand All @@ -22,8 +21,8 @@
task=Task("binary"),
lgb_params={
"optimization_search_space": {
"feature_fraction": SearchSpace(Distribution.UNIFORM, low=0.5, high=1.0),
"min_sum_hessian_in_leaf": SearchSpace(Distribution.LOGUNIFORM, low=1e-3, high=10.0),
"feature_fraction": Uniform(low=0.5, high=1.0),
"min_sum_hessian_in_leaf": Uniform(low=1e-3, high=10.0, log=True),
}
},
)
Expand Down
3 changes: 1 addition & 2 deletions examples/optimization/sequential_parameter_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
from lightautoml.automl.presets.tabular_presets import TabularAutoML
from lightautoml.tasks import Task


# load and prepare data
data = pd.read_csv("./data/sampled_app_train.csv")
train_data, test_data = train_test_split(data, test_size=0.2, stratify=data["TARGET"], random_state=42)


def sample(optimization_search_space, trial, suggested_params):
def sample(trial, suggested_params):
trial_values = copy.copy(suggested_params)

for feature_fraction in range(10):
Expand Down
3 changes: 1 addition & 2 deletions lightautoml/automl/presets/tabular_presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from ...ml_algo.dl_model import TorchModel
from ...ml_algo.linear_sklearn import LinearLBFGS
from ...ml_algo.random_forest import RandomForestSklearn
from ...ml_algo.tuning.optuna import DLOptunaTuner
from ...ml_algo.tuning.optuna import OptunaTuner
from ...pipelines.features.lgb_pipeline import LGBAdvancedPipeline
from ...pipelines.features.lgb_pipeline import LGBSeqSimpleFeatures
Expand Down Expand Up @@ -444,7 +443,7 @@ def get_nn(

if tuned:
nn_model.set_prefix("Tuned")
nn_tuner = DLOptunaTuner(
nn_tuner = OptunaTuner(
n_trials=model_params["tuning_params"]["max_tuning_iter"],
timeout=model_params["tuning_params"]["max_tuning_time"],
fit_on_holdout=model_params["tuning_params"]["fit_on_holdout"],
Expand Down
3 changes: 1 addition & 2 deletions lightautoml/automl/presets/text_presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from ...ml_algo.boost_lgbm import BoostLGBM
from ...ml_algo.dl_model import TorchModel
from ...ml_algo.linear_sklearn import LinearLBFGS
from ...ml_algo.tuning.optuna import DLOptunaTuner
from ...ml_algo.tuning.optuna import OptunaTuner
from ...pipelines.features.base import FeaturesPipeline
from ...pipelines.features.lgb_pipeline import LGBAdvancedPipeline
Expand Down Expand Up @@ -307,7 +306,7 @@ def get_nn(

if tuned:
nn_model.set_prefix("Tuned")
nn_tuner = DLOptunaTuner(
nn_tuner = OptunaTuner(
n_trials=model_params["tuning_params"]["max_tuning_iter"],
timeout=model_params["tuning_params"]["max_tuning_time"],
fit_on_holdout=model_params["tuning_params"]["fit_on_holdout"],
Expand Down
9 changes: 8 additions & 1 deletion lightautoml/ml_algo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Any
from typing import Dict
from typing import List
from typing import Callable
from typing import Optional
from typing import Sequence
from typing import Tuple
Expand Down Expand Up @@ -48,7 +49,13 @@ class MLAlgo(ABC):
"""

_default_params: Dict = {}
optimization_search_space: Dict = {}

# Dict is a default search space representation that is used for simple cases
# Callable is used for complex cases like conditional search space as described in
# LightAutoML/examples/optimization/conditional_parameters.py
# Called in _get_objective function in OptunaTuner class
optimization_search_space: Union[Dict, Callable] = {}

# TODO: add checks here
_fit_checks: Tuple = ()
_transform_checks: Tuple = ()
Expand Down
21 changes: 10 additions & 11 deletions lightautoml/ml_algo/dl_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from typing import Dict

import numpy as np
import optuna
import pandas as pd
import torch
import torch.nn as nn

from torch.optim.lr_scheduler import ReduceLROnPlateau

from .tuning.base import Uniform
from ..dataset.np_pd_dataset import NumpyDataset
from ..tasks.losses.torch import TorchLossWrapper
from ..utils.installation import __validate_extra_deps
Expand Down Expand Up @@ -618,7 +618,7 @@ def predict_single_fold(self, model: any, dataset: TabularDataset) -> np.ndarray

return pred

def _default_sample(self, trial: optuna.trial.Trial, estimated_n_trials: int, suggested_params: Dict) -> Dict:
def _get_default_search_spaces(self, estimated_n_trials: int, suggested_params: Dict) -> Dict:
"""Implements simple tuning sampling strategy.

Args:
Expand All @@ -631,19 +631,18 @@ def _default_sample(self, trial: optuna.trial.Trial, estimated_n_trials: int, su

"""
# optionally
trial_values = copy(suggested_params)
# trial_values = copy(suggested_params) # TODO: check how to use it
trial_values = {}

trial_values["bs"] = trial.suggest_categorical("bs", [2 ** i for i in range(6, 11)])
trial_values["bs"] = Uniform(low=64, high=1024, q=1)

weight_decay_bin = trial.suggest_categorical("weight_decay_bin", [0, 1])
weight_decay_bin = Uniform(low=0, high=1, q=1)
if weight_decay_bin == 0:
weight_decay = 0
else:
weight_decay = trial.suggest_loguniform("weight_decay", low=1e-6, high=1e-2)
weight_decay = Uniform(low=1e-6, high=1e-2, log=True)

trial_values["lr"] = Uniform(low=1e-5, high=1e-1, log=True)
trial_values["weight_decay"] = weight_decay

lr = trial.suggest_loguniform("lr", low=1e-5, high=1e-1)
trial_values["opt_params"] = {
"lr": lr,
"weight_decay": weight_decay,
}
return trial_values
Loading
Loading