Пример #1
0
def run_example(FLAGS):
    """
    Run parallel Sherpa optimization over a set of discrete hp combinations.
    """
    
    parameters = [sherpa.Continuous('learning_rate', [1e-5, 5e-1], 'log'),
                  sherpa.Continuous('decay', [1e-8, 1e-2], 'log'),
                  sherpa.Continuous('momentum', [0., 0.99]),
                  sherpa.Continuous('dropout', [0.0001, 0.7]),
                  sherpa.Ordinal('batch_size', [32, 64, 128, 256])]

    algorithm = bayesian_optimization.GPyOpt(max_concurrent=FLAGS.concurrent,
                                             model_type='GP',
                                             acquisition_type='EI',
                                             max_num_trials=100)

    # Run on local machine.
    scheduler = LocalScheduler()

    rval = sherpa.optimize(parameters=parameters,
                           algorithm=algorithm,
                           dashboard_port=FLAGS.port,
                           lower_is_better=False,
                           command='python fashion_mlp.py',
                           scheduler=scheduler,
                           verbose=0,
                           max_concurrent=FLAGS.concurrent,
                           output_dir='./output_gpyopt_{}'.format(
                               time.strftime("%Y-%m-%d--%H-%M-%S")))
Пример #2
0
def run_example(FLAGS):
    """
    Run parallel Sherpa optimization over a set of discrete hp combinations.
    """
    parameters = [
        sherpa.Continuous('lrinit', [0.001, 0.1], 'log'),
        sherpa.Continuous('momentum', [0., 0.99]),
        sherpa.Continuous('lrdecay', [1e-7, 1e-2], 'log'),
        sherpa.Continuous('dropout', [0., 0.5])
    ]

    if FLAGS.algorithm == 'BayesianOptimization':
        print('Running GPyOpt')
        alg = bayesian_optimization.GPyOpt(max_concurrent=FLAGS.max_concurrent,
                                           model_type='GP_MCMC',
                                           acquisition_type='EI_MCMC',
                                           max_num_trials=150)
    elif FLAGS.algorithm == 'LocalSearch':
        print('Running Local Search')
        alg = sherpa.algorithms.LocalSearch(seed_configuration={
            'lrinit': 0.038,
            'momentum': 0.92,
            'lrdecay': 0.0001,
            'dropout': 0.
        },
                                            perturbation_factors=(0.9, 1.1))
    else:
        print('Running Random Search')
        alg = sherpa.algorithms.RandomSearch(max_num_trials=150)

    if FLAGS.sge:
        assert FLAGS.env, "For SGE use, you need to set an environment path."
        # Submit to SGE queue.
        env = FLAGS.env  # Script specifying environment variables.
        opt = '-N MNISTExample -P {} -q {} -l {}'.format(
            FLAGS.P, FLAGS.q, FLAGS.l)
        sched = SGEScheduler(environment=env, submit_options=opt)
    else:
        # Run on local machine.
        sched = LocalScheduler()

    rval = sherpa.optimize(parameters=parameters,
                           algorithm=alg,
                           lower_is_better=True,
                           filename='trial.py',
                           output_dir='output_{}'.format(FLAGS.studyname),
                           scheduler=sched,
                           max_concurrent=FLAGS.max_concurrent)
    print('Best results:')
    print(rval)
Пример #3
0
    def hyper_param(self, epochs):
        parameters = [
            sherpa.Continuous('learning_rate', [1e-4, 1e-2]),
            sherpa.Discrete('num_units', [32, 128]),
            sherpa.Choice('activation', ['relu', 'adam', 'sigmoid'])
        ]
        algorithm = bayesian_optimization.GPyOpt(max_num_trials=50)
        study = sherpa.Study(parameters=parameters,
                             algorithm=algorithm,
                             lower_is_better=False)
        x_test = self._label / 300
        y_test = self._label

        # Create model
        model = models.Sequential()
        model.add(
            layers.Embedding(self.totalwords,
                             64,
                             input_length=maxSequenceLen - 1))
        model.add(layers.LSTM(128))
        model.add(layers.Dense(self.totalwords, activation='softmax'))
        model.compile(loss='categorical_crossentropy',
                      optimizer='adam',
                      metrics=['accuracy'])

        epochs = epochs
        for trial in study:
            lr = trial.parameters['learning_rate']
            num_units = trial.parameters['num_units']
            act = trial.parameters['activation']

            for i in range(epochs):
                model.fit(self.predictors, self._label, batch_size=self.batch)
                loss, accuracy = model.evaluate(x_test, y_test)
                study.add_observation(trial=trial,
                                      iteration=i,
                                      objective=accuracy,
                                      context={'loss': loss})

                if study.should_trial_stop(trial):
                    break
            study.finalize(trial=trial)
            print(study.get_best_result())
Пример #4
0
    logger.info("AUTOML ON ACCURACY")
    logger.info("MOVING LNCS2")
    config["LANG"].remove("latin")
    config["LANG"].remove("english")

    port = 1

    for lang in config["LANG"]:
        i = 1
        parameters = [
            sherpa.Continuous("threshold", [0.5, 0.85]),
            sherpa.Discrete("topn", [10, 50]),
            sherpa.Continuous("t", [0.0, 1.0]),
        ]
        algorithm = bayesian_optimization.GPyOpt(max_num_trials=40)
        study = sherpa.Study(
            parameters=parameters,
            algorithm=algorithm,
            lower_is_better=False,
            dashboard_port=(9999 - port + 1),
        )
        model1 = Word2Vec.load(
            CURRENT_EXP_DIR.split("_")[0] + "_0" + "/model/" + lang +
            "/corpus1.model")
        model2 = Word2Vec.load(
            CURRENT_EXP_DIR.split("_")[0] + "_0" + "/model/" + lang +
            "/corpus2.model")
        # Load binary truths
        binary_truth = np.loadtxt(
            "./data/" + lang + "/semeval2020_ulscd_" + lang[:3] +
args, unknown = parser.parse_known_args()

GPU = int(args.gpu)

parameters = [
    sherpa.Continuous(name='lr', range=[0.001, 0.1], scale='log'),
    sherpa.Continuous(name='dropout', range=[0.0001, 0.7]),
    sherpa.Continuous(name='top_dropout', range=[0.0001, 0.7]),
    sherpa.Continuous(name='lr_decay', range=[1e-4, 1e-9], scale='log')
]

artifacts_dir = tempfile.mkdtemp()
if args.algorithm == 'ei':
    algorithm = bayesian_optimization.GPyOpt(
        max_num_trials=args.max_num_trials,
        num_initial_data_points=5,
        acquisition_type='EI',
        max_concurrent=1)
elif args.algorithm == 'ei3':
    algorithm = bayesian_optimization.GPyOpt(
        max_num_trials=args.max_num_trials // 3,
        num_initial_data_points=5,
        acquisition_type='EI',
        max_concurrent=1)
    algorithm = sherpa.algorithms.Repeat(algorithm, 3, agg=True)
elif args.algorithm == 'ei5':
    algorithm = bayesian_optimization.GPyOpt(
        max_num_trials=args.max_num_trials // 5,
        num_initial_data_points=5,
        acquisition_type='EI',
        max_concurrent=1)
Пример #6
0
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import time
import sherpa
import sherpa.algorithms.bayesian_optimization as bayesian_optimization


parameters = [sherpa.Discrete('n_estimators', [2, 50]),
              sherpa.Choice('criterion', ['gini', 'entropy']),
              sherpa.Continuous('max_features', [0.1, 0.9])]

algorithm = bayesian_optimization.GPyOpt(max_concurrent=1,
                                         model_type='GP_MCMC',
                                         acquisition_type='EI_MCMC',
                                         max_num_trials=100)

X, y = load_breast_cancer(return_X_y=True)
study = sherpa.Study(parameters=parameters,
                     algorithm=algorithm,
                     lower_is_better=False)

for trial in study:
    print("Trial ", trial.id, " with parameters ", trial.parameters)
    clf = RandomForestClassifier(criterion=trial.parameters['criterion'],
                                 max_features=trial.parameters['max_features'],
                                 n_estimators=trial.parameters['n_estimators'],
                                 random_state=0)
    scores = cross_val_score(clf, X, y, cv=5)
    print("Score: ", scores.mean())
    study.add_observation(trial, iteration=1, objective=scores.mean())