def test_moe_rest_1(): moe_url = os.environ.get('MOE_API_URL', 'http://ERROR-sdjssfssdbsdf.com') try: request.urlopen(moe_url) except error.URLError: raise nose.SkipTest( 'No available MOE REST API endpoint (set with ' 'MOE_API_URL environment variable)') searchspace = SearchSpace() searchspace.add_float('x', -10, 10) searchspace.add_float('y', 1, 10, warp='log') searchspace.add_int('z', -10, 10) searchspace.add_enum('w', ['opt1', 'opt2']) history = [(searchspace.rvs(), np.random.random(), 'SUCCEEDED') for _ in range(4)] params = MOE(url=moe_url).suggest(history, searchspace) for k, v in iteritems(params): assert k in searchspace.variables if isinstance(searchspace[k], EnumVariable): assert v in searchspace[k].choices elif isinstance(searchspace[k], FloatVariable): assert searchspace[k].min <= v <= searchspace[k].max elif isinstance(searchspace[k], IntVariable): assert searchspace[k].min <= v <= searchspace[k].max else: assert False
def test_2_2(): s = SearchSpace() s.add_int('a', 0, 3) values = [ pyll.stochastic.sample(s['a'].to_hyperopt()) for _ in xrange(200) ] _run_chi2_test(values, bin_edges=range(5))
def test_check_repeated_params(): searchspace = SearchSpace() searchspace.add_enum('x', [1, 2]) searchspace.add_jump('y', min=3, max=4, num=2) history = [] grid_search1 = GridSearch() for _ in range(4): params = grid_search1.suggest(history, searchspace) history.append((params, 0.0, 'SUCCEEDED')) grid_search2 = GridSearch() for _ in range(4): params = grid_search2.suggest(history, searchspace) assert grid_search2.is_repeated_suggestion(params, history) history = [] grid_search3 = GridSearch() for _ in range(4): params = grid_search3.suggest(history, searchspace) history.append((params, 0.0, 'FAILED')) grid_search4 = GridSearch() for _ in range(4): params = grid_search4.suggest(history, searchspace) assert not grid_search4.is_repeated_suggestion(params, history)
def test_4_1(): s = SearchSpace() s.add_enum('c', [True, False]) values = [s.rvs()['c'] for _ in range(100)] assert all(v in [True, False] for v in values) _run_chi2_test(np.array(values, dtype=int), bin_edges=range(3))
def test_3_1(): s = SearchSpace() s.add_float('b', -2, 2) values = [s.rvs()['b'] for _ in range(100)] assert all(-2 < v < 2 for v in values) _run_chi2_test(values, bin_edges=np.linspace(-2, 2, 10))
def test_3_2(): s = SearchSpace() s.add_float('b', -2, 2) values = [pyll.stochastic.sample(s['b'].to_hyperopt()) for _ in xrange(100)] assert all(-2 < v < 2 for v in values) _run_chi2_test(values, bin_edges=np.linspace(-2, 2, 10))
def test_grid(): searchspace = SearchSpace() searchspace.add_enum('x', [1, 2]) searchspace.add_jump('y', min=3, max=4, num=2) grid_search = GridSearch() suggestions = [grid_search.suggest([], searchspace) for _ in range(4)] suggestions = [(s['x'], s['y']) for s in suggestions] assert suggestions == [(1, 3), (1, 4), (2, 3), (2, 4)], "Didn't examine whole space correctly"
def test_4_2(): s = SearchSpace() s.add_enum('c', [True, False]) values = [pyll.stochastic.sample(s['c'].to_hyperopt()) for _ in xrange(100)] assert all(v in [True, False] for v in values) _run_chi2_test(np.array(values, dtype=int), bin_edges=range(3))
def test_5_2(): s = SearchSpace() s.add_float('a', 1e-5, 1, warp='log') bin_edges = np.logspace(np.log10(s['a'].min), np.log10(s['a'].max), num=5) values = [pyll.stochastic.sample(s['a'].to_hyperopt()) for _ in xrange(100)] _run_chi2_test(values, bin_edges)
def test_5_1(): s = SearchSpace() s.add_float('a', 1e-5, 1, warp='log') n_bins = 10 n_samples = 1000 bin_edges = np.logspace(np.log10(s['a'].min), np.log10(s['a'].max), num=n_bins+1) values = [s.rvs()['a'] for _ in xrange(n_samples)] _run_chi2_test(values, bin_edges)
def our_x2_iterates(n_iters=100): history = [] searchspace = SearchSpace() searchspace.add_float('x', -10, 10) random = np.random.RandomState(0) # note the switch of sign, because _our_ function hyperopt_tpe is # a maximizer, not a minimizer fn = lambda params: -params['x']**2 for i in range(n_iters): params = HyperoptTPE(seed=random).suggest(history, searchspace) history.append((params, fn(params), 'SUCCEEDED')) return np.array([h[0]['x'] for h in history])
def our_x2_iterates(n_iters=100): history = [] searchspace = SearchSpace() searchspace.add_float('x', -10, 10) random = np.random.RandomState(0) # note the switch of sign, because _our_ function hyperopt_tpe is # a maximizer, not a minimizer def fn(params): return -params['x']**2 for i in range(n_iters): params = HyperoptTPE(seed=random).suggest(history, searchspace) history.append((params, fn(params), 'SUCCEEDED')) return np.array([h[0]['x'] for h in history])
def test_gp(): searchspace = SearchSpace() searchspace.add_float('x', -10, 10) searchspace.add_float('y', 1, 10, warp='log') searchspace.add_int('z', -10, 10) searchspace.add_enum('w', ['opt1', 'opt2']) history = [(searchspace.rvs(), np.random.random(), 'SUCCEEDED') for _ in range(4)] params = GP().suggest(history, searchspace) for k, v in iteritems(params): assert k in searchspace.variables if isinstance(searchspace[k], EnumVariable): assert v in searchspace[k].choices elif isinstance(searchspace[k], FloatVariable): assert searchspace[k].min <= v <= searchspace[k].max elif isinstance(searchspace[k], IntVariable): assert searchspace[k].min <= v <= searchspace[k].max else: assert False
def test_1(): s = SearchSpace() s.add_int('a', 1, 2) s.add_float('b', 2, 3) s.add_enum('c', ['a', 'b', 'c']) assert s.n_dims == 3 assert s['a'].min == 1 assert s['a'].max == 2 assert s['a'].name == 'a' assert s['b'].min == 2 assert s['b'].max == 3 assert s['b'].name == 'b' assert s['c'].choices == ['a', 'b', 'c'] assert s['c'].name == 'c'
def test_2_1(): s = SearchSpace() s.add_int('a', 0, 3) values = [s.rvs()['a'] for _ in xrange(100)] _run_chi2_test(values, bin_edges=range(5))
def test_random(): searchspace = SearchSpace() searchspace.add_float('x', -10, 10) random = np.random.RandomState(0) RandomSearch(seed=random).suggest([], searchspace)
def test_2_2(): s = SearchSpace() s.add_int('a', 0, 3) values = [pyll.stochastic.sample(s['a'].to_hyperopt()) for _ in xrange(200)] _run_chi2_test(values, bin_edges=range(5))