<s> from __future__ import print_function , absolute_import , division <EOL> import sys <EOL> from six import iteritems <EOL> import numpy as np <EOL> from numpy . testing . decorators import skipif <EOL> from osprey . search_space import SearchSpace <EOL> from osprey . search_space import IntVariable , EnumVariable , FloatVariable <EOL> from osprey . strategies import RandomSearch , HyperoptTPE , GP , GridSearch <EOL> try : <EOL> from hyperopt import hp , fmin , tpe , Trials <EOL> except : <EOL> pass <EOL> def test_random ( ) : <EOL> searchspace = SearchSpace ( ) <EOL> searchspace . add_float ( '<STR_LIT:x>' , - <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> random = np . random . RandomState ( <NUM_LIT:0> ) <EOL> RandomSearch ( seed = random ) . suggest ( [ ] , searchspace ) <EOL> def test_grid ( ) : <EOL> searchspace = SearchSpace ( ) <EOL> searchspace . add_enum ( '<STR_LIT:x>' , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> searchspace . add_jump ( '<STR_LIT:y>' , min = <NUM_LIT:3> , max = <NUM_LIT:4> , step = <NUM_LIT:1> ) <EOL> grid_search = GridSearch ( ) <EOL> suggestions = [ grid_search . suggest ( [ ] , searchspace ) for _ in range ( <NUM_LIT:4> ) ] <EOL> suggestions = [ ( s [ '<STR_LIT:x>' ] , s [ '<STR_LIT:y>' ] ) for s in suggestions ] <EOL> assert suggestions == [ ( <NUM_LIT:1> , <NUM_LIT:3> ) , ( <NUM_LIT:1> , <NUM_LIT:4> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) , ( <NUM_LIT:2> , <NUM_LIT:4> ) ] , "<STR_LIT>" <EOL> def hyperopt_x2_iterates ( n_iters = <NUM_LIT:100> ) : <EOL> iterates = [ ] <EOL> trials = Trials ( ) <EOL> random = np . random . RandomState ( <NUM_LIT:0> ) <EOL> def fn ( params ) : <EOL> iterates . append ( params [ '<STR_LIT:x>' ] ) <EOL> return params [ '<STR_LIT:x>' ] ** <NUM_LIT:2> <EOL> for i in range ( n_iters ) : <EOL> fmin ( fn = fn , algo = tpe . suggest , max_evals = i + <NUM_LIT:1> , trials = trials , <EOL> space = { '<STR_LIT:x>' : hp . uniform ( '<STR_LIT:x>' , - <NUM_LIT:10> , <NUM_LIT:10> ) } , <EOL> ** HyperoptTPE . _hyperopt_fmin_random_kwarg ( random ) ) <EOL> return np . array ( iterates ) <EOL> def our_x2_iterates ( n_iters = <NUM_LIT:100> ) : <EOL> history = [ ] <EOL> searchspace = SearchSpace ( ) <EOL> searchspace . add_float ( '<STR_LIT:x>' , - <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> random = np . random . RandomState ( <NUM_LIT:0> ) <EOL> def fn ( params ) : <EOL> return - params [ '<STR_LIT:x>' ] ** <NUM_LIT:2> <EOL> for i in range ( n_iters ) : <EOL> params = HyperoptTPE ( seed = random ) . suggest ( history , searchspace ) <EOL> history . append ( ( params , fn ( params ) , '<STR_LIT>' ) ) <EOL> return np . array ( [ h [ <NUM_LIT:0> ] [ '<STR_LIT:x>' ] for h in history ] ) <EOL> @ skipif ( '<STR_LIT>' not in sys . modules , '<STR_LIT>' ) <EOL> def test_1 ( ) : <EOL> ours = our_x2_iterates ( <NUM_LIT> ) <EOL> ref = hyperopt_x2_iterates ( <NUM_LIT> ) <EOL> np . testing . assert_array_equal ( ref , ours ) <EOL> @ skipif ( '<STR_LIT>' not in sys . modules , '<STR_LIT>' ) <EOL> def test_gp ( ) : <EOL> searchspace = SearchSpace ( ) <EOL> searchspace . add_float ( '<STR_LIT:x>' , - <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> searchspace . add_float ( '<STR_LIT:y>' , <NUM_LIT:1> , <NUM_LIT:10> , warp = '<STR_LIT>' ) <EOL> searchspace . add_int ( '<STR_LIT:z>' , - <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> searchspace . add_enum ( '<STR_LIT:w>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> history = [ ( searchspace . rvs ( ) , np . random . random ( ) , '<STR_LIT>' ) <EOL> for _ in range ( <NUM_LIT:4> ) ] <EOL> params = GP ( ) . suggest ( history , searchspace ) <EOL> for k , v in iteritems ( params ) : <EOL> assert k in searchspace . variables <EOL> if isinstance ( searchspace [ k ] , EnumVariable ) : <EOL> assert v in searchspace [ k ] . choices <EOL> elif isinstance ( searchspace [ k ] , FloatVariable ) : <EOL> assert searchspace [ k ] . min <= v <= searchspace [ k ] . max <EOL> elif isinstance ( searchspace [ k ] , IntVariable ) : <EOL> assert searchspace [ k ] . min <= v <= searchspace [ k ] . max <EOL> else : <EOL> assert False </s>
