Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_best_result_larger():
parameters = [sherpa.Choice('a', [0, 1]),
sherpa.Choice('b', [3, 4])]
results_df = pandas.DataFrame(collections.OrderedDict(
[('Trial-ID', list(range(1, 9))),
('Status', [sherpa.TrialStatus.COMPLETED] * 8),
('stage', [1] * 8),
('a', [0, 0, 0, 0, 1, 1, 1, 1]),
('b', [3, 3, 4, 4, 3, 3, 4, 4]),
('Objective', [1., 1.1, 2.1, 2.2, 5., 5.1, 6., 6.1])]
))
rs = sherpa.algorithms.RandomSearch()
gs = SequentialTesting(algorithm=rs,
K=4,
n=(3, 6, 9),
P=0.5)
def get_mock_study():
mock_algorithm = mock.MagicMock()
mock_algorithm.get_suggestion.return_value = {'a': 1, 'b': 2}
mock_stopping_rule = mock.MagicMock()
return sherpa.Study(parameters=[sherpa.Discrete('a', [1,2]),
sherpa.Choice('b', [2,5,7])],
algorithm=mock_algorithm,
stopping_rule=mock_stopping_rule,
lower_is_better=True,
disable_dashboard=True)
def test_repeat_wait_for_completion():
parameters = [sherpa.Continuous('a', [0, 1]),
sherpa.Choice('b', ['x', 'y', 'z'])]
rs = sherpa.algorithms.RandomSearch(max_num_trials=10)
rs = sherpa.algorithms.Repeat(algorithm=rs, num_times=10,
wait_for_completion=True)
study = sherpa.Study(parameters=parameters, algorithm=rs,
lower_is_better=True,
disable_dashboard=True)
for i in range(10):
tnew = study.get_suggestion()
print(tnew.parameters)
assert isinstance(tnew.parameters, dict)
config = tnew.parameters
study.add_observation(tnew, objective=float(i),
iteration=1)
study.finalize(tnew)
def test_user_code_fails(test_dir):
tempdir = test_dir
parameters = [sherpa.Choice(name="param_a",
range=[1, 2, 3]),
sherpa.Continuous(name="param_b",
range=[0, 1])]
algorithm = sherpa.algorithms.RandomSearch(max_num_trials=3)
db_port = 27000
scheduler = sherpa.schedulers.LocalScheduler()
filename = os.path.join(tempdir, "test.py")
with open(filename, 'w') as f:
f.write(testscript2)
with pytest.warns(RuntimeWarning):
results = sherpa.optimize(filename=filename,
lower_is_better=True,
def test_get_best_result():
parameters = [sherpa.Choice('a', [1,2,3])]
gs = sherpa.algorithms.GridSearch()
study = sherpa.Study(parameters=parameters, algorithm=gs,
lower_is_better=True,
disable_dashboard=True)
objectives = [1.1,1.2,1.3]
for obj, trial in zip(objectives, study):
study.add_observation(trial, objective=obj)
study.finalize(trial)
assert study.get_best_result()['a'] == 1
def test_repeat_grid_search():
parameters = [sherpa.Choice('a', [1, 2]),
sherpa.Choice('b', ['a', 'b'])]
alg = sherpa.algorithms.GridSearch()
alg = sherpa.algorithms.Repeat(algorithm=alg, num_times=3)
suggestion = alg.get_suggestion(parameters)
seen = list()
while suggestion != sherpa.AlgorithmState.DONE:
seen.append((suggestion['a'], suggestion['b']))
suggestion = alg.get_suggestion(parameters)
expected_params = [(1, 'a'),
(1, 'b'),
(2, 'a'),
(2, 'b')]
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SHERPA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SHERPA. If not, see .
"""
from __future__ import print_function
import sherpa
import time
parameters = [sherpa.Choice(name="param_a",
range=[1, 2, 3]),
sherpa.Continuous(name="param_b",
range=[0, 1])]
# algorithm = sherpa.algorithms.RandomSearch(max_num_trials=40)
# algorithm = sherpa.algorithms.LocalSearch(num_random_seeds=20)
algorithm = sherpa.algorithms.BayesianOptimization(num_grid_points=2, max_num_trials=50)
# stopping_rule = sherpa.algorithms.MedianStoppingRule(min_iterations=2,
# min_trials=5)
stopping_rule = None
study = sherpa.Study(parameters=parameters,
algorithm=algorithm,
stopping_rule=stopping_rule,
lower_is_better=True,
dashboard_port=8999)
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',
acquisition_type='EI',
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'],
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SHERPA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SHERPA. If not, see .
"""
from __future__ import print_function
import sherpa
import time
parameters = [sherpa.Choice(name="param_a",
range=[1, 2, 3]),
sherpa.Continuous(name="param_b",
range=[0, 1])]
algorithm = sherpa.algorithms.RandomSearch(max_num_trials=40)
# algorithm = sherpa.algorithms.LocalSearch(num_random_seeds=20)
# algorithm = sherpa.algorithms.BayesianOptimization(num_grid_points=2, max_num_trials=50)
# stopping_rule = sherpa.algorithms.MedianStoppingRule(min_iterations=2,
# min_trials=5)
stopping_rule = None
study = sherpa.Study(parameters=parameters,
algorithm=algorithm,
stopping_rule=stopping_rule,
lower_is_better=True,
dashboard_port=8999)
default='/home/lhertel/profiles/python3env.profile',
type=str)
FLAGS = parser.parse_args()
# Define Hyperparameter ranges
parameters = [sherpa.Continuous(name='lr', range=[0.005, 0.1], scale='log'),
sherpa.Continuous(name='dropout', range=[0., 0.4]),
sherpa.Ordinal(name='batch_size', range=[16, 32, 64])]
if FLAGS.algorithm == 'PBT':
algorithm = sherpa.algorithms.PopulationBasedTraining(population_size=50,
parameter_range={
'lr':[0.0000001, 1.],
'batch_size':[16, 32, 64, 128, 256, 512]})
parameters.append(sherpa.Choice(name='epochs', range=[3]))
stoppingrule = None
else:
parameters.append(sherpa.Continuous(name='lr_decay', range=[1e-4, 1e-7], scale='log'))
parameters.append(sherpa.Choice(name='epochs', range=[25]))
algorithm = sherpa.algorithms.BayesianOptimization(num_grid_points=2)
# stoppingrule = sherpa.algorithms.MedianStoppingRule(min_trials=10,
# min_iterations=8)
# The scheduler
if not FLAGS.local:
env = FLAGS.env
opt = '-N MNISTPBT -P {} -q {} -l {} -l gpu=1'.format(FLAGS.P, FLAGS.q, FLAGS.l)
scheduler = sherpa.schedulers.SGEScheduler(environment=env, submit_options=opt)
else:
scheduler = sherpa.schedulers.LocalScheduler()