def tune(): X, y = get_data() too = torch.optim.Adam, torch.optim.Adadelta, torch.optim.Adagrad, torch.optim.ASGD to = ht.CategoricalParameter('torch_optimizer', options=too) eta = ht.ContinuousParameter('eta', lower_bound=1e-10, upper_bound=1e-1) mi = ht.DiscreteParameter('max_iter', lower_bound=1e2, upper_bound=1e4) hl1 = ht.DiscreteParameter('', lower_bound=10, upper_bound=100) hl2 = ht.DiscreteParameter('', lower_bound=10, upper_bound=100) hls = ht.TupleParameter('hidden_layer_sizes', values=(hl1, hl2)) tp1 = ht.CategoricalParameter('', options=(nn.Linear, )) tp2 = ht.CategoricalParameter('', options=(nn.Linear, )) tp3 = ht.CategoricalParameter('', options=(nn.Linear, )) top = ht.TupleParameter('topology', values=(tp1, tp2, tp3)) hypers = [to, eta, mi, hls, top] tuner = ht.HyperTune(algorithm=Net, parameters=hypers, train_func=Net.fit, objective_func=Net.mse, train_func_args=(X, y), objective_func_args=(X, y), max_evals=100, maximize=False, num_replications=1) tuner.tune() print(tuner.get_results())
def test_categorical_param(self): c = ht.CategoricalParameter('c', options=('a', 'b')) self.assertEqual(c.name, 'c') self.assertEqual(c.shape, 1) v = c.get_val([-1]) self.assertEqual(v, 'a') co = ht.ContinuousParameter('co', lower_bound=0, upper_bound=1) con = ht.ConstantParameter('con', value=100) c = ht.CategoricalParameter('c', options=('a', co, con)) self.assertEqual(c.shape, 2)
self._optimizer_.step() def mse(self, X, y): y_hat = self.predict(X) return self._loss_func_(y, y_hat).item() # make an example dataset p = 750 X = np.random.rand(10**4, 3) y = np.random.rand(10**4) X_train, X_test, y_train, y_test = X[:p], X[p:], y[:p], y[p:] # define the target hyperparameters too = torch.optim.Adam, torch.optim.Adadelta, torch.optim.Adagrad, torch.optim.ASGD to = ht.CategoricalParameter('torch_optimizer', options=too) eta = ht.ContinuousParameter('eta', lower_bound=1e-10, upper_bound=1e-1) mi = ht.DiscreteParameter('max_iter', lower_bound=1e2, upper_bound=1e4) hl1 = ht.DiscreteParameter('', lower_bound=10, upper_bound=100) hl2 = ht.DiscreteParameter('', lower_bound=10, upper_bound=100) hls = ht.TupleParameter('hidden_layer_sizes', values=(hl1, hl2)) tp1 = ht.CategoricalParameter('', options=(nn.Linear, )) tp2 = ht.CategoricalParameter('', options=(nn.Linear, )) tp3 = ht.CategoricalParameter('', options=(nn.Linear, )) top = ht.TupleParameter('topology', values=(tp1, tp2, tp3)) hypers = [to, eta, mi, hls, top] # define a Hypertune object tuner = ht.HyperTune(algorithm=Net, parameters=hypers, train_func=Net.fit,
from sklearn.neural_network import MLPRegressor import hypertune as ht import numpy as np # make an example dataset p = 75 X = np.random.rand(100, 3) y = np.random.rand(100) X_train, X_test, y_train, y_test = X[:p], X[p:], y[:p], y[p:] # define the target hyperparameters activation = ht.CategoricalParameter('activation', options=('identity', 'logistic', 'tanh', 'relu')) learning_rate_init = ht.ContinuousParameter('learning_rate_init', lower_bound=10**-5, upper_bound=0.1) max_iter = ht.DiscreteParameter('max_iter', lower_bound=500, upper_bound=10**3) hl1 = ht.DiscreteParameter('', lower_bound=50, upper_bound=250) hl2 = ht.DiscreteParameter('', lower_bound=100, upper_bound=250) hl3 = ht.DiscreteParameter('', lower_bound=1, upper_bound=100) hidden_layer_sizes = ht.TupleParameter('hidden_layer_sizes', values=(hl1, hl2, hl3)) hypers = [ activation, learning_rate_init, max_iter, learning_rate, hidden_layer_sizes ] # define a Hypertune object tuner = ht.HyperTune(algorithm=MLPRegressor,
for i, (train_idxs, test_idxs) in enumerate(splits): X_train, y_train = X[train_idxs], y[train_idxs] X_test, y_test = X[test_idxs], y[test_idxs] algo.fit(X_train, y_train) results[i] = algo.score(X_test, y_test) return np.mean(results) X, y = datasets.iris(return_splits=False) splits = list(KFold(n_splits=4).split(X)) learning_rate = ht.CategoricalParameter('learning_rate', options=('constant', 'invscaling', 'adaptive')) learning_rate_init = ht.ContinuousParameter('learning_rate_init', lower_bound=10**-5, upper_bound=0.1) max_iter = ht.DiscreteParameter('max_iter', lower_bound=500, upper_bound=10**3) hypers = [learning_rate, learning_rate_init, max_iter] tuner = ht.HyperTune(algorithm=MLPClassifier, parameters=hypers, train_func=train, objective_func=objective_func, objective_func_args=(X, y, splits), max_evals=10**2)