def setUp(self): l1 = log(low=-3, high=5, base=10) l2 = log(low=-2, high=3, base=10) u = uniform(low=-1, high=1) qu = quantized_uniform(low=1, high=20, step=1) self.space = Space([{ "algo": { "svm": { "C": l1, "kernel": { "linear": None, "rbf": { "gamma": l2 } }, "cond2": { "aa": None, "bb": { "abc": u } } }, "knn": { "n_neighbors": qu } } }, { "cond3": 0, "p": l1, "p2": qu }])
def init_choco_sampler(args): conn = choco.SQLiteConnection(args['sqlite_dbase']) space = { "lr": choco.log(low=-5, high=-3, base=10), "lr_decay": choco.uniform(high=1, low=0), "reg_scale": choco.uniform(low=0, high=1), "last_reg_scale": choco.uniform(low=0, high=1), "weight": choco.uniform(low=1, high=50), "weight_decay": choco.uniform(low=0, high=1), "contrast": choco.uniform(low=-100, high=100) } sampler = choco.CMAES(conn, space) return (sampler, conn)
def create_space(): space = { "learning_rate": choco.log(low=-5, high=-2, base=10), "dropout_keep_prob": choco.quantized_uniform(low=0.0, high=0.95, step=0.05), "num_filters": choco.quantized_uniform(low=50, high=200, step=10), "batch_size": choco.quantized_uniform(low=64, high=256, step=16), "num_epochs": choco.quantized_uniform(low=100, high=200, step=10), "l2_reg_lambda": choco.quantized_uniform(low=0.0, high=10.0, step=0.5), "eps": choco.quantized_uniform(low=1.0, high=10.0, step=0.02), "dev_sample_percentage": choco.quantized_uniform(low=0.1, high=0.3, step=0.01) } return space
def convert_param_to_choco(param): """Convert a single search parameter suitably for ``chocolate``. """ from math import log10 import chocolate as choco if param['type'] == 'BOOL': return choco.choice([False, True]) if param['type'] == 'INT': return choco.quantized_uniform(low=param['min'], high=param['max'] + 1, step=1) if param['type'] == 'STRING': return choco.choice(param['options']) if param['type'] == 'FLOAT': return choco.uniform(low=param['min'], high=param['max']) if param['type'] == 'FLOAT_EXP': return choco.log(low=log10(param['min']), high=log10(param['max']), base=10) else: raise ValueError("Didn't understand space {}.".format(param))
from utils import read_data, write_data, metrics, submit from sklearn.svm import SVC from param_config import config import chocolate as choco from utils import metrics from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split np.random.seed(config.set_seed) TOTAL_RUNS = 5000 c_d = np.logspace(-4, 6, TOTAL_RUNS) g_d = np.logspace(-5, 6, TOTAL_RUNS) param_grid = { "C": choco.log(-4, 6, base=10), "gamma": choco.log(-5, 6, base=10) } def model_train(x_train, y_train, x_val, y_val, params): svc = SVC(kernel='rbf', verbose=True, **params) svc.fit(x_train, y_train) y_hat = svc.predict(x_val) f1 = metrics(y_val, y_hat) return -f1 def split(X, y): x_train, x_val, y_train, y_val = train_test_split(
def f1_score_model(trn_x, trn_y, tst_x, tst_y, model, **params): with warnings.catch_warnings(): warnings.simplefilter("ignore") m = models[model](**params) m.fit(trn_x, trn_y) y_pred = m.predict(tst_x) return -1*skm.f1_score(tst_y, y_pred, average='macro') space = [ # {'model': 'RandomForestClassifier', # "max_depth" : choco.quantized_uniform(2, 32, 2), # "min_samples_split": choco.quantized_uniform(2, 600, 2), # "n_estimators" : choco.quantized_uniform(125, 800, 25),}, {'model': 'SVC', "gamma": 'auto', "C": choco.log(-3, 3, 10), "kernel": choco.choice(['linear', 'poly', 'rbf', 'sigmoid', 'precomputed']), "tol": choco.log(-5, -2, 10),}, {'model': 'XGBClassifier', "learning_rate" : choco.uniform(0.001, 0.1), "max_depth" : choco.quantized_uniform(2, 16, 2), "min_child_weight": choco.quantized_uniform(2, 10, 2), "subsample" : choco.quantized_uniform(0.7, 1.05, 0.05), "n_estimators" : choco.quantized_uniform(25, 525, 25),}, {'model': 'LogisticRegression', "penalty" : choco.choice(['l1', 'l2']), "C" : choco.log(-2, 1, 10),}, ] models = { 'RandomForestClassifier': RandomForestClassifier,
self.validation_error = 100000.0 if __name__ == '__main__': if len(sys.argv) == 1: # original params from article model=LatentAttention(frac_train=0.99, n_z=20, batchsize=100, learning_rate=0.001, max_epochs=10, e_h1=16, e_h2=32, d_h1=32, d_h2=16, run_id=-1); model.train() print("loss={}".format(float(model.validation_error))) exit(0) # Params from optimizer search_space = { "n_z": choco.quantized_uniform(5, 100, 1), "learning_rate": choco.log(-20, -8, 2), "max_epochs": choco.quantized_uniform(5, 200, 1), "e_h1": choco.quantized_uniform(16, 256, 1), "e_h2": choco.quantized_uniform(16, 256, 1), "d_h1": choco.quantized_uniform(16, 256, 1), "d_h2": choco.quantized_uniform(16, 256, 1), } connection = choco.SQLiteConnection("sqlite:///no_labels_results.sqlite3") sampler = choco.Bayes(connection, search_space) token, sample = sampler.next() print("Parameters: {} Token: {}".format(sample, token)) run_id = token['_chocolate_id'] model = LatentAttention(0.99, batchsize=150, run_id=run_id, **sample) model.train() sampler.update(token, float(model.validation_error))
return func def f1_score_model(trn_x, trn_y, tst_x, tst_y, model, **params): m = models[model](**params) m.fit(trn_x, trn_y) y_pred = m.predict(tst_x) return -1 * skm.f1_score(tst_y, y_pred, average='macro') space = [ { 'model': 'SVC', "gamma": 'auto', "C": choco.log(-3, 3, 10), "kernel": choco.choice(['linear', 'poly', 'rbf', 'sigmoid', 'precomputed']), "tol": choco.log(-5, -2, 10), }, { 'model': 'XGBClassifier', "learning_rate": choco.uniform(0.001, 0.1), "max_depth": choco.quantized_uniform(2, 16, 2), "min_child_weight": choco.quantized_uniform(2, 10, 2), "subsample": choco.quantized_uniform(0.7, 1.05, 0.05), "n_estimators": choco.quantized_uniform(25, 525, 25), }, { 'model': 'RandomForestClassifier', "max_depth": choco.quantized_uniform(2, 10, 2),