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,catrefers totrial.suggest_categoricalThe 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 intrial.suggest_xxx(name, *args, **kwargs)**kwargs— The kwargs used intrial.suggest_xxx(name, *args, **kwargs)
Attributes
args— *args from Argsdefault(any) — The default from Argskwargs— **kwargs from Argssuggfunc(str) — The transformed suggestion name according tosuggtype
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 parametertrial(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 fromvalidation_epoch_end.val_accorval_lossfor example.n_trials(int) — Number of trials**kwargs— Other keyword arguments foroptuna.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 Argson(str) — on from Argsstudy(optuna.Study) — study object created from kwargstrainers(list) — list of trainers to keep track of the best onetrials— The trials</>
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 objectconf(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 dictionarydata_class(class) — The data class subclassed fromplkit.DataNote that this is the class itself, not the instantized object.model_class(class) — The data class subclassed fromplkit.ModuleNote that this is the class itself, not the instantized object.**kwargs— Other arguments forstudy.optimizeother thanfuncandn_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 dictionarydata_class(class) — The data class subclassed fromplkit.DataNote that this is the class itself, not the instantized object.model_class(class) — The data class subclassed fromplkit.ModuleNote that this is the class itself, not the instantized object.**kwargs— Other arguments forstudy.optimizeother thanfuncandn_trials. See: https://optuna.readthedocs.io/en/stable/reference/generated/optuna.study.Study.html#optuna.study.Study.optimize