Skip to content

plkit.optuna

module

plkit.optuna

Optuna wrapper for plkit

Classes
  • OptunaSuggest Optuna suggests for configuration items</>
  • Optuna The class uses optuna to automate hyperparameter tuning</>
class

plkit.optuna.OptunaSuggest(default, suggtype, *args, **kwargs)

Optuna suggests for configuration items

Parameters
  • default (any) The default value, which the value will be collapsed to when optuna is opted out. So that you don't have to change your code if you don't run optuna.
  • suggtype (str) The type of suggestion For example, cat refers to trial.suggest_categorical The mappings are: cat -> 'suggest_categorical', categorical -> 'suggest_categorical', distuni -> 'suggest_discrete_uniform', dist_uni -> 'suggest_discrete_uniform', discrete_uniform -> 'suggest_discrete_uniform', float -> 'suggest_float', int -> 'suggest_int', loguni -> 'suggest_loguniform', log_uni -> 'suggest_loguniform', uni -> 'suggest_uniform'
  • *args The args used in trial.suggest_xxx(name, *args, **kwargs)
  • **kwargs The kwargs used in trial.suggest_xxx(name, *args, **kwargs)
Attributes
  • args *args from Args
  • default (any) The default from Args
  • kwargs **kwargs from Args
  • suggfunc (str) The transformed suggestion name according to suggtype
Methods
  • suggest(name, trial) (Any) Get the suggested value</>
method

suggest(name, trial)

Get the suggested value

This is used in Optuna class, you don't have to call this

Parameters
  • name (str) The name of the parameter
  • trial (optuna.Trial) The trial to get the suggested value from
Returns (Any)

The suggested value

class

plkit.optuna.Optuna(on, n_trials, **kwargs)

The class uses optuna to automate hyperparameter tuning

Example
>>> from plkit import Data, Module, Optuna
>>> class MyData(Data):
>>>     ...
>>> class MyModel(Module):
>>>     ...
>>> class MyOptuna(Optuna):
>>>     def suggests(self, config):
>>>         ...
>>>         return new_config
>>> optuna = MyOptuna('val_loss', 100)
>>> optuna.run(config, model_class, data_class)
Parameters
  • on (str) On which value to optimize. Should be one of the keys of dictionary that is returned from validation_epoch_end. val_acc or val_loss for example.
  • n_trials (int) Number of trials
  • **kwargs Other keyword arguments for optuna.create_study
Attributes
  • best_model Get the model from best trainer</>
  • best_params The best parameters from the study</>
  • best_trainer Get the best trainer</>
  • best_trial The best trial from the study</>
  • n_trials (int) n_trials from Args
  • on (str) on from Args
  • study (optuna.Study) study object created from kwargs
  • trainers (list) list of trainers to keep track of the best one
  • trials The trials</>
Methods
  • run(config, data_class, model_class, **kwargs) Run the optimization</>
  • run(config, data_class, model_class, **kwargs) Run the optimization</>
  • suggests(trial, conf) (dict) Collect the hyperparameters from the trial suggestions if any configuration item is an OptunaSuggest object</>
method

suggests(trial, conf)

Collect the hyperparameters from the trial suggestions if any configuration item is an OptunaSuggest object

Parameters
  • trial (optuna.Trial) the trial object
  • conf (dict) The configuration dictionary
Returns (dict)

A dictionary of suggested parameters

method

run(config, data_class, model_class, **kwargs)

Run the optimization

The optimization is running on fit of the trainer. If test data is provided. Test will be performed as well.

Parameters
  • config (dict) The configuation dictionary
  • data_class (class) The data class subclassed from plkit.Data Note that this is the class itself, not the instantized object.
  • model_class (class) The data class subclassed from plkit.Module Note that this is the class itself, not the instantized object.
  • **kwargs Other arguments for study.optimize other than func and n_trials. See: https://optuna.readthedocs.io/en/stable/reference/generated/optuna.study.Study.html#optuna.study.Study.optimize
method

run(config, data_class, model_class, **kwargs)

Run the optimization

The optimization is running on fit of the trainer. If test data is provided. Test will be performed as well.

Parameters
  • config (dict) The configuation dictionary
  • data_class (class) The data class subclassed from plkit.Data Note that this is the class itself, not the instantized object.
  • model_class (class) The data class subclassed from plkit.Module Note that this is the class itself, not the instantized object.
  • **kwargs Other arguments for study.optimize other than func and n_trials. See: https://optuna.readthedocs.io/en/stable/reference/generated/optuna.study.Study.html#optuna.study.Study.optimize