Skip to content

Instantly share code, notes, and snippets.

@crcrpar
Created October 17, 2019 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crcrpar/e5fa77c859fb4bd6ddac867c81c50bdf to your computer and use it in GitHub Desktop.
Save crcrpar/e5fa77c859fb4bd6ddac867c81c50bdf to your computer and use it in GitHub Desktop.
# data: fastai's DataBunch
def objective(trial: optuna.Trial):
num_layers = trial.suggest_int('n_layers', 1, 5) # `num_layers` is 1, 2, 3, 4, or 5.
layers, ps = [], [] # define the number of unit of each layer / the ratio of dropout of each layer
for i in range(n_layers - 1): # `TabularModel` automatically adds the last layer.
num_units = trial.suggest_categorical(f'num_units_layer_{i}', [800, 900, 1000, 1100, 1200])
p = trial.suggest_discrete_uniform(f'dropout_p_layer_{i}', 0, 1, 0.05)
layers.append(num_units); ps.append(p)
emb_drop = trial.suggest_discrete_uniform('emb_drop', 0, 1, 0.05)
learn = tabular_learner(data, layers=layers, ps=ps, emb_drop=emb_drop, y_range=y_range, metrics=exp_rmspe)
learn.fit_one_cycle(5, 1e-3, wd=0.2)
return learn.validate()[-1].item() # Of course you can use the last record of `learn.recorder`.
study = optuna.create_study()
study.optimize(objective)
best_trial = study.best_trial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment