Exemplo n.º 1
0
def variables_from_domain(domain):
    rval = OrderedDict()
    hps = OrderedDict()
    expr_to_config(domain.expr, (True, ), hps)
    for key, val in hps.items():
        if val['conditions'] != set([(True, )]):
            raise NotImplementedError('Conditional parameter: %s' % key,
                                      val['conditions'])

        if val['node'].name == 'uniform':
            low = val['node'].arg['low'].obj
            high = val['node'].arg['high'].obj
            rval[key] = {
                'name': key,
                'type': 'float',
                'size': 1,
                'min': low,
                'max': high,
                # -- following just used in this file
                'to_unit': lambda x: (x - low) / (high - low),
                'from_unit': lambda y: y * (high - low) + low,
            }
        elif val['node'].name == 'loguniform':
            raise NotImplementedError()
        elif val['node'].name == 'randint':
            raise NotImplementedError()
        elif val['node'].name == 'normal':
            raise NotImplementedError()
        elif val['node'].name == 'lognormal':
            raise NotImplementedError()
        else:
            raise NotImplementedError('Unsupported hyperparamter type: %s' %
                                      val['node'].name)
    return rval
Exemplo n.º 2
0
def variables_from_domain(domain):
    rval = OrderedDict()
    hps = OrderedDict()
    expr_to_config(domain.expr, (True,), hps)
    for key, val in hps.items():
        if val['conditions'] != set([(True,)]):
            raise NotImplementedError('Conditional parameter: %s' % key,
                                     val['conditions'])

        if val['node'].name == 'uniform':
            low = val['node'].arg['low'].obj
            high = val['node'].arg['high'].obj
            rval[key] = {
                'name': key,
                'type': 'float',
                'size': 1,
                'min': low,
                'max': high,
                # -- following just used in this file
                'to_unit': lambda x: (x - low) / (high - low),
                'from_unit': lambda y: y * (high - low) + low,
                        }
        elif val['node'].name == 'loguniform':
            raise NotImplementedError()
        elif val['node'].name == 'randint':
            raise NotImplementedError()
        elif val['node'].name == 'normal':
            raise NotImplementedError()
        elif val['node'].name == 'lognormal':
            raise NotImplementedError()
        else:
            raise NotImplementedError('Unsupported hyperparamter type: %s' %
                                     val['node'].name)
    return rval
Exemplo n.º 3
0
def test_remove_allpaths_int():
    z = hp.uniformint("z", 0, 10)
    a = hp.choice("a", [z + 1, z - 1])
    hps = {}
    expr_to_config(a, (True, ), hps)
    aconds = hps["a"]["conditions"]
    zconds = hps["z"]["conditions"]
    assert aconds == set([(True, )]), aconds
    assert zconds == set([(True, )]), zconds
Exemplo n.º 4
0
def test_remove_allpaths():
    z = hp.uniform('z', 0, 10)
    a = hp.choice('a', [ z + 1, z - 1])
    hps = {}
    expr_to_config(a, (True,), hps)
    aconds = hps['a']['conditions']
    zconds = hps['z']['conditions']
    assert aconds == set([(True,)]), aconds
    assert zconds == set([(True,)]), zconds
Exemplo n.º 5
0
def test_remove_allpaths():
    z = hp.uniform('z', 0, 10)
    a = hp.choice('a', [z + 1, z - 1])
    hps = {}
    expr_to_config(a, (True, ), hps)
    aconds = hps['a']['conditions']
    zconds = hps['z']['conditions']
    assert aconds == set([(True, )]), aconds
    assert zconds == set([(True, )]), zconds
Exemplo n.º 6
0
def test_expr_to_config():

    z = hp.randint("z", 10)
    a = hp.choice(
        "a",
        [
            hp.uniform("b", -1, 1) + z,
            {
                "c":
                1,
                "d":
                hp.choice("d", [
                    3 + hp.loguniform("c", 0, 1), 1 + hp.loguniform("e", 0, 1)
                ]),
            },
        ],
    )

    expr = as_apply((a, z))

    hps = {}
    expr_to_config(expr, (True, ), hps)

    for label, dct in list(hps.items()):
        print(label)
        print("  dist: %s(%s)" % (
            dct["node"].name,
            ", ".join(map(str, [ii.eval() for ii in dct["node"].inputs()])),
        ))
        if len(dct["conditions"]) > 1:
            print("  conditions (OR):")
            for condseq in dct["conditions"]:
                print("    ", " AND ".join(map(str, condseq)))
        elif dct["conditions"]:
            for condseq in dct["conditions"]:
                print("  conditions :", " AND ".join(map(str, condseq)))

    assert hps["a"]["node"].name == "randint"
    assert hps["b"]["node"].name == "uniform"
    assert hps["c"]["node"].name == "loguniform"
    assert hps["d"]["node"].name == "randint"
    assert hps["e"]["node"].name == "loguniform"
    assert hps["z"]["node"].name == "randint"

    assert set([(True, EQ("a", 0))]) == set([(True, EQ("a", 0))])
    assert hps["a"]["conditions"] == set([(True, )])
    assert hps["b"]["conditions"] == set([(True, EQ("a", 0))
                                          ]), hps["b"]["conditions"]
    assert hps["c"]["conditions"] == set([(True, EQ("a", 1), EQ("d", 0))])
    assert hps["d"]["conditions"] == set([(True, EQ("a", 1))])
    assert hps["e"]["conditions"] == set([(True, EQ("a", 1), EQ("d", 1))])
    assert hps["z"]["conditions"] == set([(True, ), (True, EQ("a", 0))])
Exemplo n.º 7
0
def test_expr_to_config():

    z = hp.randint('z', 10)
    a = hp.choice('a',
                  [
                      hp.uniform('b', -1, 1) + z,
                      {'c': 1, 'd': hp.choice('d',
                                              [3 + hp.loguniform('c', 0, 1),
                                               1 + hp.loguniform('e', 0, 1)])
                      }])

    expr = as_apply((a, z))

    hps = {}
    expr_to_config(expr, (True,), hps)

    for label, dct in hps.items():
        print label
        print '  dist: %s(%s)' % (
            dct['node'].name,
            ', '.join(map(str, [ii.eval() for ii in dct['node'].inputs()])))
        if len(dct['conditions']) > 1:
            print '  conditions (OR):'
            for condseq in dct['conditions']:
                print '    ', ' AND '.join(map(str, condseq))
        elif dct['conditions']:
            for condseq in dct['conditions']:
                print '  conditions :', ' AND '.join(map(str, condseq))


    assert hps['a']['node'].name == 'randint'
    assert hps['b']['node'].name == 'uniform'
    assert hps['c']['node'].name == 'loguniform'
    assert hps['d']['node'].name == 'randint'
    assert hps['e']['node'].name == 'loguniform'
    assert hps['z']['node'].name == 'randint'

    assert set([(True, EQ('a', 0))]) == set([(True, EQ('a', 0))])
    assert hps['a']['conditions'] == set([(True,)])
    assert hps['b']['conditions'] == set([
        (True, EQ('a', 0))]), hps['b']['conditions']
    assert hps['c']['conditions'] == set([
        (True, EQ('a', 1), EQ('d', 0))])
    assert hps['d']['conditions'] == set([
        (True, EQ('a', 1))])
    assert hps['e']['conditions'] == set([
        (True, EQ('a', 1), EQ('d', 1))])
    assert hps['z']['conditions'] == set([
        (True,),
        (True, EQ('a', 0))])
Exemplo n.º 8
0
    def __init__(self, domain, GPR=None):
        self.domain = domain

        # -- hps: list of hyperparameter names
        self.hps = list(sorted(domain.params.keys()))

        # -- config: type and dependency information keyed by hp name
        self.config = {}
        expr_to_config(domain.expr, None, self.config)

        if GPR is None:
            GPR = self.GPR # -- class variable

        kerns, self.hp_slices, self.x_bounds = self.init_param_helpers()
        self.gpr = GPR(kernels.product(kerns, self.hp_slices))
Exemplo n.º 9
0
    def __init__(self, domain, GPR=None):
        self.domain = domain

        # -- hps: list of hyperparameter names
        self.hps = list(sorted(domain.params.keys()))

        # -- config: type and dependency information keyed by hp name
        self.config = {}
        expr_to_config(domain.expr, None, self.config)

        if GPR is None:
            GPR = self.GPR  # -- class variable

        kerns, self.hp_slices, self.x_bounds = self.init_param_helpers()
        self.gpr = GPR(kernels.product(kerns, self.hp_slices))
def test_expr_to_config():

    z = hp.randint('z', 10)
    a = hp.choice('a', [
        hp.uniform('b', -1, 1) + z, {
            'c':
            1,
            'd':
            hp.choice(
                'd',
                [3 + hp.loguniform('c', 0, 1), 1 + hp.loguniform('e', 0, 1)])
        }
    ])

    expr = as_apply((a, z))

    hps = {}
    expr_to_config(expr, (True, ), hps)

    for label, dct in hps.items():
        print label
        print '  dist: %s(%s)' % (dct['node'].name, ', '.join(
            map(str, [ii.eval() for ii in dct['node'].inputs()])))
        if len(dct['conditions']) > 1:
            print '  conditions (OR):'
            for condseq in dct['conditions']:
                print '    ', ' AND '.join(map(str, condseq))
        elif dct['conditions']:
            for condseq in dct['conditions']:
                print '  conditions :', ' AND '.join(map(str, condseq))

    assert hps['a']['node'].name == 'randint'
    assert hps['b']['node'].name == 'uniform'
    assert hps['c']['node'].name == 'loguniform'
    assert hps['d']['node'].name == 'randint'
    assert hps['e']['node'].name == 'loguniform'
    assert hps['z']['node'].name == 'randint'

    assert set([(True, EQ('a', 0))]) == set([(True, EQ('a', 0))])
    assert hps['a']['conditions'] == set([(True, )])
    assert hps['b']['conditions'] == set([(True, EQ('a', 0))
                                          ]), hps['b']['conditions']
    assert hps['c']['conditions'] == set([(True, EQ('a', 1), EQ('d', 0))])
    assert hps['d']['conditions'] == set([(True, EQ('a', 1))])
    assert hps['e']['conditions'] == set([(True, EQ('a', 1), EQ('d', 1))])
    assert hps['z']['conditions'] == set([(True, ), (True, EQ('a', 0))])
Exemplo n.º 11
0
def run_config(config):
    argd = config["argd"]

    def config_lookup(key):
        if key == "scale_mult1":
            return argd["W_init_algo_old_multiplier"]

        if key == "scale_heur1":
            if "old" == argd["W_init_algo"]:
                return 0
            else:
                assert "Xavier" == argd["W_init_algo"]
                return 1

        if key == "preproc":
            return {"raw": 0, "normalize": 1, "pca": 2}[argd["preprocessing"]]

        if key == "batch_size":
            return 0 if 20 == argd["batchsize"] else 1

        if key == "nhid1":
            return argd["n_hid"]

        if key == "dist1":
            return 0 if argd["W_init_algo"] == "old" else 1

        if key == "squash":
            return 0 if argd["squash"] == "tanh" else 1

        if key == "colnorm_thresh":
            return 1e-7

        if key == "l2_penalty_nz":
            return argd["l2_penalty"]

        if key == "l2_penalty":
            return 0 if argd["l2_penalty"] == 0 else 1

        if key == "iseed":
            # convert from seed value to choice index
            return argd["iseed"] - 5

        try:
            return argd[key]
        except KeyError:
            print "Returning GarbageCollected for %s" % key
            return hyperopt.pyll.base.GarbageCollected

    expr = nnet1_preproc_space()
    hps = {}
    expr_to_config(expr, None, hps)
    print config
    memo = {}
    for k, v in hps.items():
        # print k, v
        memo[v["node"]] = config_lookup(k)

    print memo
    rval = eval_fn(expr=expr, memo=memo, ctrl=None, protocol_cls=RectanglesVectorXV)
    print "-" * 80
    print "COMPUTED RESULTS IN TERMS OF *ERROR*"
    print rval["loss"]
    print "-" * 80
    print "SAVED RESULTS IN TERMS OF *ACCURACY*"
    print config["result"]
    print "-" * 80
Exemplo n.º 12
0
def run_config(config):
    argd = config['argd']

    def config_lookup(key):
        if key == 'scale_mult1':
            return argd['W_init_algo_old_multiplier']

        if key == 'scale_heur1':
            if 'old' == argd['W_init_algo']:
                return 0
            else:
                assert 'Xavier' == argd['W_init_algo']
                return 1

        if key == 'preproc':
            return {'raw': 0, 'normalize': 1, 'pca': 2}[argd['preprocessing']]

        if key == 'batch_size':
            return 0 if 20 == argd['batchsize'] else 1

        if key == 'nhid1':
            return argd['n_hid']

        if key == 'dist1':
            return 0 if argd['W_init_algo'] == 'old' else 1

        if key == 'squash':
            return 0 if argd['squash'] == 'tanh' else 1

        if key == 'colnorm_thresh':
            return 1e-7

        if key == 'l2_penalty_nz':
            return argd['l2_penalty']

        if key == 'l2_penalty':
            return 0 if argd['l2_penalty'] == 0 else 1

        if key == 'iseed':
            # convert from seed value to choice index
            return argd['iseed'] - 5

        try:
            return argd[key]
        except KeyError:
            print 'Returning GarbageCollected for %s' % key
            return hyperopt.pyll.base.GarbageCollected

    expr = nnet1_preproc_space()
    hps = {}
    expr_to_config(expr, None, hps)
    print config
    memo = {}
    for k, v in hps.items():
        #print k, v
        memo[v['node']] = config_lookup(k)

    print memo
    rval = eval_fn(expr=expr,
                   memo=memo,
                   ctrl=None,
                   protocol_cls=RectanglesVectorXV)
    print '-' * 80
    print 'COMPUTED RESULTS IN TERMS OF *ERROR*'
    print rval['loss']
    print '-' * 80
    print 'SAVED RESULTS IN TERMS OF *ACCURACY*'
    print config['result']
    print '-' * 80