def test_as_apply_list_of_applies(): alist = [as_apply(i) for i in range(5)] al = as_apply(alist) assert isinstance(al, Apply) assert al.name == 'pos_args' # -- have to come back to this if Literal copies args assert al.pos_args == alist
def test_as_apply_list_of_applies(): alist = [as_apply(i) for i in range(5)] al = as_apply(alist) assert isinstance(al, Apply) assert al.name == "pos_args" # -- have to come back to this if Literal copies args assert al.pos_args == alist
def test_as_apply_dict_of_applies(): d = {'a': as_apply(9), 'b': as_apply(10)} ad = as_apply(d) assert isinstance(ad, Apply) assert ad.name == 'dict' assert len(ad) == 2 assert ad.named_args[0][0] == 'a' assert ad.named_args[0][1]._obj == 9 assert ad.named_args[1][0] == 'b' assert ad.named_args[1][1]._obj == 10
def test_as_apply_dict_of_applies(): d = {"a": as_apply(9), "b": as_apply(10)} ad = as_apply(d) assert isinstance(ad, Apply) assert ad.name == "dict" assert len(ad) == 2 assert ad.named_args[0][0] == "a" assert ad.named_args[0][1]._obj == 9 assert ad.named_args[1][0] == "b" assert ad.named_args[1][1]._obj == 10
def test_dfs(): dd = as_apply({"c": 11, "d": 12}) d = {"a": 9, "b": dd, "y": dd, "z": dd + 1} ad = as_apply(d) order = dfs(ad) print([str(o) for o in order]) assert order[0]._obj == 9 assert order[1]._obj == 11 assert order[2]._obj == 12 assert order[3].named_args[0][0] == "c" assert order[4]._obj == 1 assert order[5].name == "add" assert order[6].named_args[0][0] == "a" assert len(order) == 7
def test_dfs(): dd = as_apply({'c': 11, 'd': 12}) d = {'a': 9, 'b': dd, 'y': dd, 'z': dd + 1} ad = as_apply(d) order = dfs(ad) print([str(o) for o in order]) assert order[0]._obj == 9 assert order[1]._obj == 11 assert order[2]._obj == 12 assert order[3].named_args[0][0] == 'c' assert order[4]._obj == 1 assert order[5].name == 'add' assert order[6].named_args[0][0] == 'a' assert len(order) == 7
def test_clone_merge_no_merge_literals(): a, b, c = as_apply((2, 3, 2)) d = (a + b) * (c + b) len_d = len(dfs(d)) e = clone_merge(d, merge_literals=False) assert len_d == len(dfs(d)) assert len_d == len(dfs(e)) assert e.eval() == d.eval()
def test_as_apply_list_of_literals(): l = [9, 3] al = as_apply(l) assert isinstance(al, Apply) assert al.name == "pos_args" assert isinstance(al.pos_args[0], Literal) assert isinstance(al.pos_args[1], Literal) al.pos_args[0]._obj == 9 al.pos_args[1]._obj == 3
def test_as_apply_list_of_literals(): l = [9, 3] al = as_apply(l) assert isinstance(al, Apply) assert al.name == 'pos_args' assert isinstance(al.pos_args[0], Literal) assert isinstance(al.pos_args[1], Literal) al.pos_args[0]._obj == 9 al.pos_args[1]._obj == 3
def test_as_apply_dict_of_literals(): d = {'a': 9, 'b': 10} ad = as_apply(d) assert isinstance(ad, Apply) assert ad.name == 'dict' assert len(ad) == 2 assert ad.named_args[0][0] == 'a' assert ad.named_args[0][1]._obj == 9 assert ad.named_args[1][0] == 'b' assert ad.named_args[1][1]._obj == 10
def test_as_apply_tuple_of_literals(): l = (9, 3) al = as_apply(l) assert isinstance(al, Apply) assert al.name == 'pos_args' assert isinstance(al.pos_args[0], Literal) assert isinstance(al.pos_args[1], Literal) al.pos_args[0]._obj == 9 al.pos_args[1]._obj == 3 assert len(al) == 2
def test_as_apply_dict_of_literals(): d = {"a": 9, "b": 10} ad = as_apply(d) assert isinstance(ad, Apply) assert ad.name == "dict" assert len(ad) == 2 assert ad.named_args[0][0] == "a" assert ad.named_args[0][1]._obj == 9 assert ad.named_args[1][0] == "b" assert ad.named_args[1][1]._obj == 10
def test_as_apply_tuple_of_literals(): l = (9, 3) al = as_apply(l) assert isinstance(al, Apply) assert al.name == "pos_args" assert isinstance(al.pos_args[0], Literal) assert isinstance(al.pos_args[1], Literal) al.pos_args[0]._obj == 9 al.pos_args[1]._obj == 3 assert len(al) == 2
def _validate_space_exhaustive_search(space): from hyperopt.pyll.base import dfs, as_apply from hyperopt.pyll.stochastic import implicit_stochastic_symbols supported_stochastic_symbols = ['randint', 'quniform', 'qloguniform', 'qnormal', 'qlognormal', 'categorical'] for node in dfs(as_apply(space)): if node.name in implicit_stochastic_symbols: if node.name not in supported_stochastic_symbols: raise ExhaustiveSearchError( 'Exhaustive search is only possible with the following stochastic symbols: ' '' + ', '.join(supported_stochastic_symbols) )
def test_as_apply_nested_dict(): d = {"a": 9, "b": {"c": 11, "d": 12}} ad = as_apply(d) assert isinstance(ad, Apply) assert ad.name == "dict" assert len(ad) == 2 assert ad.named_args[0][0] == "a" assert ad.named_args[0][1]._obj == 9 assert ad.named_args[1][0] == "b" assert ad.named_args[1][1].name == "dict" assert ad.named_args[1][1].named_args[0][0] == "c" assert ad.named_args[1][1].named_args[0][1]._obj == 11 assert ad.named_args[1][1].named_args[1][0] == "d" assert ad.named_args[1][1].named_args[1][1]._obj == 12
def test_as_apply_nested_dict(): d = {'a': 9, 'b': {'c': 11, 'd': 12}} ad = as_apply(d) assert isinstance(ad, Apply) assert ad.name == 'dict' assert len(ad) == 2 assert ad.named_args[0][0] == 'a' assert ad.named_args[0][1]._obj == 9 assert ad.named_args[1][0] == 'b' assert ad.named_args[1][1].name == 'dict' assert ad.named_args[1][1].named_args[0][0] == 'c' assert ad.named_args[1][1].named_args[0][1]._obj == 11 assert ad.named_args[1][1].named_args[1][0] == 'd' assert ad.named_args[1][1].named_args[1][1]._obj == 12
def test_eval_arithmetic(): a, b, c = as_apply((2, 3, 4)) assert (a + b).eval() == 5 assert (a + b + c).eval() == 9 assert (a + b + 1 + c).eval() == 10 assert (a * b).eval() == 6 assert (a * b * c * (-1)).eval() == -24 assert (a - b).eval() == -1 assert (a - b * c).eval() == -10 assert (a // b).eval() == 0 # int div assert (b // a).eval() == 1 # int div assert (c / a).eval() == 2 assert (4 / a).eval() == 2 assert (a / 4.0).eval() == 0.5
def test_as_apply_passthrough(): a4 = as_apply(4) assert a4 is as_apply(a4)
def test_as_apply_literal(): assert isinstance(as_apply(7), Literal)
def convert_tpe_to_smac_from_object(search_space): expr = base.as_apply(search_space) hps = {} expr_to_config(expr, (True, ), hps) new_space = "" # print expr for label, dct in hps.items(): if dct['node'].name == "randint": assert len(dct['node'].inputs()) == 1 #randint['x', 5] -> x [0, 4]i [0] upper = dct['node'].inputs()[0].eval() new_space += '%s {%s} [0]\n' % \ (label, ", ".join(["%s" % (i,) for i in range(upper)])) elif dct['node'].name == "categorical": # No difference to a randint node upper = dct['node'].inputs()[1].eval() new_space += '%s {%s} [0]\n' % \ (label, ", ".join(["%s" % (i,) for i in range(upper)])) elif dct['node'].name == "uniform": assert len(dct['node'].inputs()) == 2 lower = dct['node'].inputs()[0].eval() upper = dct['node'].inputs()[1].eval() default = (lower + upper) / 2.0 new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, default) elif dct['node'].name == "quniform": # Assumption: q-value is always the last value assert len(dct['node'].inputs()) == 3 lower = dct['node'].inputs()[0].eval() upper = dct['node'].inputs()[1].eval() q = dct['node'].inputs()[2].eval() default = (lower + upper) / 2.0 label = "Q%s_%s" % (q, label) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, default) elif dct['node'].name == "loguniform": assert len(dct['node'].inputs()) == 2 lower = dct['node'].inputs()[0].eval() upper = dct['node'].inputs()[1].eval() default = (lower + upper) / 2.0 label = "LOG_%s" % (label, ) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, default) elif dct['node'].name == "qloguniform": assert len(dct['node'].inputs()) == 3 lower = dct['node'].inputs()[0].eval() upper = dct['node'].inputs()[1].eval() q = dct['node'].inputs()[2].eval() default = (lower + upper) / 2.0 label = "LOG_Q%s_%s" % (q, label) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, default) elif dct['node'].name == "normal": assert len(dct['node'].inputs()) == 2 mu = dct['node'].inputs()[0].eval() sigma = dct['node'].inputs()[1].eval() lower = mu - 3 * sigma upper = mu + 3 * sigma new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, mu) elif dct['node'].name == "qnormal": assert len(dct['node'].inputs()) == 3 mu = dct['node'].inputs()[0].eval() sigma = dct['node'].inputs()[1].eval() lower = mu - 3 * sigma upper = mu + 3 * sigma q = dct['node'].inputs()[2].eval() label = "Q%s_%s" % (q, label) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, mu) elif dct['node'].name == "lognormal": assert len(dct['node'].inputs()) == 2 mu = dct['node'].inputs()[0].eval() sigma = dct['node'].inputs()[1].eval() lower = mu - 3 * sigma upper = mu + 3 * sigma label = "LOG_%s" % (label, ) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, mu) elif dct['node'].name == "qlognormal": assert len(dct['node'].inputs()) == 3 mu = dct['node'].inputs()[0].eval() sigma = dct['node'].inputs()[1].eval() lower = mu - 3 * sigma upper = mu + 3 * sigma q = dct['node'].inputs()[2].eval() label = "Q%s_LOG_%s" % (q, label) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, mu) else: raise Exception("Node name %s not known" % dct['node'].name) # Now take care about conditions condict = dict() param_keys = list() # We allow only one varying param varying_param_name = "" varying_param = set() if dct['conditions']: # print "AAA", dct['conditions'] for condseq in dct['conditions']: # print '##', label, condseq if len(condseq) == 1 and condseq[0] is True: # Only true as a condition continue else: if len(condict.keys()) == 0: # Once collect all keys for condition in condseq[1:]: param_keys.append(condition.name) condict[condition.name] = condition.val else: # Now insert values assert (param_keys.count(k) == 1 for k in condseq[1:]) for condition in condseq[1:]: if condict[condition.name] != condition.val: # We found a varying parameter, it is if varying_param_name == condition.name: # either the one we already knew varying_param.add(condition.val) elif varying_param_name == "": # or the first one varying_param_name = condition.name varying_param.add(condict[condition.name]) varying_param.add(condition.val) else: # or we cannot handle this raise Exception( "This is not possible to handle:\n%s", (dct['conditions'])) # Start printing conditions # print "CCC", varying_param, varying_param_name if varying_param_name != "": new_space += '%s | %s in {%s}\n' % (label, varying_param_name, ",".join( str(i) for i in varying_param)) for key in condict.keys(): if key != varying_param_name: new_space += '%s | %s in {%s}\n' % (label, key, condict[key]) return new_space
def convert_tpe_to_smac_from_file(filename): space_name, ext = os.path.splitext(os.path.basename(filename)) #noinspection PyBroadException try: space = imp.load_source(space_name, filename) except Exception, e: print("Could not find\n%s\n\tin\n%s\n\trelative to\n%s" % (space_name, filename, os.getcwd())) import traceback print traceback.format_exc() sys.exit(1) search_space = space.space expr = base.as_apply(search_space) hps = {} expr_to_config(expr, (True,), hps) new_space = "" # print expr for label, dct in hps.items(): if dct['node'].name == "randint": assert len(dct['node'].inputs()) == 1 #randint['x', 5] -> x [0, 4]i [0] upper = dct['node'].inputs()[0].eval() new_space += '%s {%s} [0]\n' % \ (label, ", ".join(["%s" % (i,) for i in range(upper)])) elif dct['node'].name == "categorical": # No difference to a randint node upper = dct['node'].inputs()[1].eval()
def convert_tpe_to_smac_from_file(filename): space_name, ext = os.path.splitext(os.path.basename(filename)) #noinspection PyBroadException try: space = imp.load_source(space_name, filename) except Exception, e: print("Could not find\n%s\n\tin\n%s\n\trelative to\n%s" % (space_name, filename, os.getcwd())) import traceback print traceback.format_exc() sys.exit(1) search_space = space.space expr = base.as_apply(search_space) hps = {} expr_to_config(expr, (True, ), hps) new_space = "" # print expr for label, dct in hps.items(): if dct['node'].name == "randint": assert len(dct['node'].inputs()) == 1 #randint['x', 5] -> x [0, 4]i [0] upper = dct['node'].inputs()[0].eval() new_space += '%s {%s} [0]\n' % \ (label, ", ".join(["%s" % (i,) for i in range(upper)])) elif dct['node'].name == "categorical": # No difference to a randint node upper = dct['node'].inputs()[1].eval()
def convert_tpe_to_smac_from_object(search_space): expr = base.as_apply(search_space) hps = {} expr_to_config(expr, (True,), hps) new_space = "" # print expr for label, dct in hps.items(): if dct['node'].name == "randint": assert len(dct['node'].inputs()) == 1 #randint['x', 5] -> x [0, 4]i [0] upper = dct['node'].inputs()[0].eval() new_space += '%s {%s} [0]\n' % \ (label, ", ".join(["%s" % (i,) for i in range(upper)])) elif dct['node'].name == "categorical": # No difference to a randint node upper = dct['node'].inputs()[1].eval() new_space += '%s {%s} [0]\n' % \ (label, ", ".join(["%s" % (i,) for i in range(upper)])) elif dct['node'].name == "uniform": assert len(dct['node'].inputs()) == 2 lower = dct['node'].inputs()[0].eval() upper = dct['node'].inputs()[1].eval() default = (lower+upper)/2.0 new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, default) elif dct['node'].name == "quniform": # Assumption: q-value is always the last value assert len(dct['node'].inputs()) == 3 lower = dct['node'].inputs()[0].eval() upper = dct['node'].inputs()[1].eval() q = dct['node'].inputs()[2].eval() default = (lower+upper)/2.0 label = "Q%s_%s" % (q, label) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, default) elif dct['node'].name == "loguniform": assert len(dct['node'].inputs()) == 2 lower = dct['node'].inputs()[0].eval() upper = dct['node'].inputs()[1].eval() default = (lower+upper)/2.0 label = "LOG_%s" % (label,) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, default) elif dct['node'].name == "qloguniform": assert len(dct['node'].inputs()) == 3 lower = dct['node'].inputs()[0].eval() upper = dct['node'].inputs()[1].eval() q = dct['node'].inputs()[2].eval() default = (lower+upper)/2.0 label = "LOG_Q%s_%s" % (q, label) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, default) elif dct['node'].name == "normal": assert len(dct['node'].inputs()) == 2 mu = dct['node'].inputs()[0].eval() sigma = dct['node'].inputs()[1].eval() lower = mu-3*sigma upper = mu+3*sigma new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, mu) elif dct['node'].name == "qnormal": assert len(dct['node'].inputs()) == 3 mu = dct['node'].inputs()[0].eval() sigma = dct['node'].inputs()[1].eval() lower = mu-3*sigma upper = mu+3*sigma q = dct['node'].inputs()[2].eval() label = "Q%s_%s" % (q, label) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, mu) elif dct['node'].name == "lognormal": assert len(dct['node'].inputs()) == 2 mu = dct['node'].inputs()[0].eval() sigma = dct['node'].inputs()[1].eval() lower = mu-3*sigma upper = mu+3*sigma label = "LOG_%s" % (label, ) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, mu) elif dct['node'].name == "qlognormal": assert len(dct['node'].inputs()) == 3 mu = dct['node'].inputs()[0].eval() sigma = dct['node'].inputs()[1].eval() lower = mu-3*sigma upper = mu+3*sigma q = dct['node'].inputs()[2].eval() label = "Q%s_LOG_%s" % (q, label) new_space += '%s [%s, %s] [%s]\n' % (label, lower, upper, mu) else: raise Exception("Node name %s not known" % dct['node'].name) # Now take care about conditions condict = dict() param_keys = list() # We allow only one varying param varying_param_name = "" varying_param = set() if dct['conditions']: # print "AAA", dct['conditions'] for condseq in dct['conditions']: # print '##', label, condseq if len(condseq) == 1 and condseq[0] is True: # Only true as a condition continue else: if len(condict.keys()) == 0: # Once collect all keys for condition in condseq[1:]: param_keys.append(condition.name) condict[condition.name] = condition.val else: # Now insert values assert(param_keys.count(k) == 1 for k in condseq[1:]) for condition in condseq[1:]: if condict[condition.name] != condition.val: # We found a varying parameter, it is if varying_param_name == condition.name: # either the one we already knew varying_param.add(condition.val) elif varying_param_name == "": # or the first one varying_param_name = condition.name varying_param.add(condict[condition.name]) varying_param.add(condition.val) else: # or we cannot handle this raise Exception("This is not possible to handle:\n%s", (dct['conditions'])) # Start printing conditions # print "CCC", varying_param, varying_param_name if varying_param_name != "": new_space += '%s | %s in {%s}\n' % (label, varying_param_name, ",".join(str(i) for i in varying_param)) for key in condict.keys(): if key != varying_param_name: new_space += '%s | %s in {%s}\n' % (label, key, condict[key]) return new_space