-
I want to create a search space for a neural network with n layers (randomly chosen from [2,3,4,5]) and each layer has certain units (randomly chosen from [100, 200, 300]) For example: if the network has 2 layers, then the units could be [100, 300], [300, 200], [200, 200] , etc. if the network has 3 layers, then the units could be [100, 200, 100], [300, 100, 200], [200,300,300], etc. The code could look like def train_nn(config):
...
# define search space dictionary
space = {"n_layers": tune.choice([2,3,4,5])}
# add an additional item to the search space dictionary
space["n_units"= [tune.choice([100, 200,300]) for i in range(space['n_layers'])]
analysis = tune.run(
train_nn,
config = space,
) Here I try to create space['n_units'] by the randomly selected value of space['n_layers']. But there is syntax error: TypeError Traceback (most recent call last)
<ipython-input-13-e7959554a989> in <module>()
1 # search space
2 space = {"n_layers": tune.choice([2,3,4,5]),
----> 3 "n_units": [tune.choice([100, 200,300]) for i in range(space['n_layers'])]
TypeError: 'Categorical' object cannot be interpreted as an integer It's because The implement in def objective(trial: optuna.Trial):
num_layers = trial.suggest_int('n_layers', 2, 5) # `num_layers` is 2, 3, 4, or 5.
layers= [] # 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}', [100, 200, 300])
layers.append(num_units)
emb_drop = trial.suggest_discrete_uniform('emb_drop', 0, 1, 0.05)
learn = tabular_learner(data, layers=layers, 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 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'd suggest this:
and in the trainable function, simply ignore the unused hps. |
Beta Was this translation helpful? Give feedback.
I'd suggest this:
and in the trainable function, simply ignore the unused hps.