Exemplo n.º 1
0
def learn(X, y, kerneldef, opt_criterion=None, verbose=False, ftol=1e-8,
          maxiter=10000):

    n, d = X.shape

    if opt_criterion is None:
        opt_criterion = criterions.negative_log_marginal_likelihood
    else:
        pass  # check type

    cov_fn = compose(kerneldef)

    # Automatically determine the range
    meta = get_meta(kerneldef)

    bounds = [Bound(l, h) for l, h in zip(meta.lower_bound, meta.upper_bound)]
    theta0 = meta.initial_val

    def criterion(*theta):
        K = cov_fn(X, X, theta, True)  # learn with noise!
        factors = np.linalg.svd(K)
        value = opt_criterion(y, factors)
        if verbose:
            log.info("[{0}] {1}".format(value, theta))
        return value

    # up to here
    nmin = structured_minimizer(minimize)
    result = nmin(criterion, theta0, backend='scipy',
                  ftol=ftol, maxiter=maxiter, jac=False,
                  bounds=bounds, method='L-BFGS-B')
    print(result)
    return result.x
Exemplo n.º 2
0
def test_structured_params(make_quadratic):

    a, b, c, data, _ = make_quadratic
    w0 = [np.random.randn(2), np.random.randn(1)[0]]

    nmin = structured_minimizer(minimize)
    res = nmin(qobj_struc, w0, args=(data,), jac=True, bounds=None,
               method='L-BFGS-B')
    (Ea_bfgs, Eb_bfgs), Ec_bfgs = res['x']

    assert np.allclose((Ea_bfgs, Eb_bfgs, Ec_bfgs), (a, b, c), atol=1e-3,
                       rtol=0)

    nsgd = structured_sgd(sgd)
    res = nsgd(qobj_struc, w0, data, bounds=None, eval_obj=True, gtol=1e-4,
               passes=1000, rate=0.95, eta=1e-6)
    (Ea_sgd, Eb_sgd), Ec_sgd = res['x']

    assert np.allclose((Ea_sgd, Eb_sgd, Ec_sgd), (a, b, c), atol=1e-1, rtol=0)

    if nlopt_test:
        res = nmin(qobj_struc, w0, args=(data, False), jac=False, bounds=None,
                   method='LN_BOBYQA', backend='nlopt')
        (Ea_bq, Eb_bq), Ec_bq = res['x']

        assert np.allclose((Ea_bq, Eb_bq, Ec_bq), (a, b, c), atol=1e-3, rtol=0)
Exemplo n.º 3
0
def test_structured_params(make_quadratic, make_random):

    random = make_random
    a, b, c, data, _ = make_quadratic
    w0 = [Parameter(random.randn(2), Bound()),
          Parameter(random.randn(1), Bound())
          ]

    qobj_struc = lambda w12, w3, data: q_struc(w12, w3, data, qobj)
    assert_opt = lambda Eab, Ec: \
        np.allclose((a, b, c), (Eab[0], Eab[1], Ec), atol=1e-3, rtol=0)

    nmin = structured_minimizer(minimize)
    res = nmin(qobj_struc, w0, args=(data,), jac=True, method='L-BFGS-B')
    assert_opt(*res.x)

    nsgd = structured_sgd(sgd)
    res = nsgd(qobj_struc, w0, data, eval_obj=True,
               random_state=make_random)
    assert_opt(*res.x)

    qf_struc = lambda w12, w3, data: q_struc(w12, w3, data, qfun)
    qg_struc = lambda w12, w3, data: q_struc(w12, w3, data, qgrad)
    res = nmin(qf_struc, w0, args=(data,), jac=qg_struc, method='L-BFGS-B')
    assert_opt(*res.x)
Exemplo n.º 4
0
def test_structured_params(make_quadratic, make_random):

    random = make_random
    a, b, c, data, _ = make_quadratic
    w0 = [
        Parameter(random.randn(2), Bound()),
        Parameter(random.randn(1), Bound())
    ]

    qobj_struc = lambda w12, w3, data: q_struc(w12, w3, data, qobj)
    assert_opt = lambda Eab, Ec: \
        np.allclose((a, b, c), (Eab[0], Eab[1], Ec), atol=1e-3, rtol=0)

    nmin = structured_minimizer(minimize)
    res = nmin(qobj_struc, w0, args=(data, ), jac=True, method='L-BFGS-B')
    assert_opt(*res.x)

    nsgd = structured_sgd(sgd)
    res = nsgd(qobj_struc, w0, data, eval_obj=True, random_state=make_random)
    assert_opt(*res.x)

    qf_struc = lambda w12, w3, data: q_struc(w12, w3, data, qfun)
    qg_struc = lambda w12, w3, data: q_struc(w12, w3, data, qgrad)
    res = nmin(qf_struc, w0, args=(data, ), jac=qg_struc, method='L-BFGS-B')
    assert_opt(*res.x)
Exemplo n.º 5
0
def test_rand_start(make_quadratic, make_random):

    random = make_random
    a, b, c, data, _ = make_quadratic

    w0 = [Parameter(gamma(1), Positive(), shape=(2, )), Parameter(1., Bound())]

    qobj_struc = lambda w12, w3, data: q_struc(w12, w3, data, qobj)
    assert_opt = lambda Eab, Ec: \
        np.allclose((a, b, c), (Eab[0], Eab[1], Ec), atol=1e-3, rtol=0)

    nmin = structured_minimizer(logtrick_minimizer(minimize))
    res = nmin(qobj_struc,
               w0,
               args=(data, ),
               jac=True,
               method='L-BFGS-B',
               random_state=random,
               nstarts=100)
    assert_opt(*res.x)

    nsgd = structured_sgd(logtrick_sgd(sgd))
    res = nsgd(qobj_struc,
               w0,
               data,
               eval_obj=True,
               nstarts=100,
               random_state=random)
    assert_opt(*res.x)
Exemplo n.º 6
0
def test_rand_start(make_quadratic, make_random):

    random = make_random
    a, b, c, data, _ = make_quadratic

    w0 = [
        Parameter(gamma(1), Positive(), shape=(2,)),
        Parameter(1., Bound())
    ]

    qobj_struc = lambda w12, w3, data: q_struc(w12, w3, data, qobj)
    assert_opt = lambda Eab, Ec: \
        np.allclose((a, b, c), (Eab[0], Eab[1], Ec), atol=1e-3, rtol=0)

    nmin = structured_minimizer(logtrick_minimizer(minimize))
    res = nmin(qobj_struc, w0, args=(data,), jac=True, method='L-BFGS-B',
               random_state=random, nstarts=100)
    assert_opt(*res.x)

    nsgd = structured_sgd(logtrick_sgd(sgd))
    res = nsgd(qobj_struc, w0, data, eval_obj=True, nstarts=100,
               random_state=random)
    assert_opt(*res.x)