Exemplo n.º 1
0
def test_cholesky_grad():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the Cholesky op.")
    rng = numpy.random.RandomState(utt.fetch_seed())
    r = rng.randn(5, 5).astype(config.floatX)
    pd = numpy.dot(r, r.T)
    # Check the default.
    yield utt.verify_grad, cholesky, [pd], 3, rng
    # Explicit lower-triangular.
    yield utt.verify_grad, Cholesky(lower=True), [pd], 3, rng
    # Explicit upper-triangular.
    yield utt.verify_grad, Cholesky(lower=False), [pd], 3, rng
Exemplo n.º 2
0
def test_cholesky_grad():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the Cholesky op.")
    rng = numpy.random.RandomState(utt.fetch_seed())
    r = rng.randn(5, 5).astype(config.floatX)
    pd = numpy.dot(r, r.T)
    eps = None
    if config.floatX == "float64":
        eps = 2e-8
    # Check the default.
    yield (lambda: utt.verify_grad(cholesky, [pd], 3, rng, eps=eps))
    # Explicit lower-triangular.
    yield (
        lambda: utt.verify_grad(Cholesky(lower=True), [pd], 3, rng, eps=eps))
    # Explicit upper-triangular.
    yield (
        lambda: utt.verify_grad(Cholesky(lower=False), [pd], 3, rng, eps=eps))
Exemplo n.º 3
0
def test_tag_solve_triangular():
    cholesky_lower = Cholesky(lower=True)
    cholesky_upper = Cholesky(lower=False)
    A = tensor.matrix('A')
    x = tensor.vector('x')
    L = cholesky_lower(A)
    U = cholesky_upper(A)
    b1 = solve(L, x)
    b2 = solve(U, x)
    f = theano.function([A,x], b1)
    for node in f.maker.fgraph.toposort():
        if isinstance(node.op, Solve):
            assert node.op.A_structure == 'lower_triangular'
    f = theano.function([A,x], b2)
    for node in f.maker.fgraph.toposort():
        if isinstance(node.op, Solve):
            assert node.op.A_structure == 'upper_triangular'
Exemplo n.º 4
0
def test_cholesky():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the Cholesky op.")

    rng = numpy.random.RandomState(utt.fetch_seed())
    r = rng.randn(5, 5).astype(config.floatX)
    pd = numpy.dot(r, r.T)
    x = tensor.matrix()
    chol = cholesky(x)
    # Check the default.
    ch_f = function([x], chol)
    yield check_lower_triangular, pd, ch_f
    # Explicit lower-triangular.
    chol = Cholesky(lower=True)(x)
    ch_f = function([x], chol)
    yield check_lower_triangular, pd, ch_f
    # Explicit upper-triangular.
    chol = Cholesky(lower=False)(x)
    ch_f = function([x], chol)
    yield check_upper_triangular, pd, ch_f
Exemplo n.º 5
0
def test_tag_solve_triangular():
    if not imported_scipy:
        pytest.skip("Scipy needed for the Cholesky op.")
    cholesky_lower = Cholesky(lower=True)
    cholesky_upper = Cholesky(lower=False)
    A = tensor.matrix("A")
    x = tensor.vector("x")
    L = cholesky_lower(A)
    U = cholesky_upper(A)
    b1 = solve(L, x)
    b2 = solve(U, x)
    f = theano.function([A, x], b1)
    if config.mode != "FAST_COMPILE":
        for node in f.maker.fgraph.toposort():
            if isinstance(node.op, Solve):
                assert node.op.A_structure == "lower_triangular"
    f = theano.function([A, x], b2)
    if config.mode != "FAST_COMPILE":
        for node in f.maker.fgraph.toposort():
            if isinstance(node.op, Solve):
                assert node.op.A_structure == "upper_triangular"
Exemplo n.º 6
0
def test_cholesky_and_cholesky_grad_shape():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the Cholesky op.")

    rng = numpy.random.RandomState(utt.fetch_seed())
    x = tensor.matrix()
    for l in (cholesky(x), Cholesky(lower=True)(x), Cholesky(lower=False)(x)):
        f_chol = theano.function([x], l.shape)
        g = tensor.grad(l.sum(), x)
        f_cholgrad = theano.function([x], g.shape)
        topo_chol = f_chol.maker.fgraph.toposort()
        topo_cholgrad = f_cholgrad.maker.fgraph.toposort()
        if config.mode != 'FAST_COMPILE':
            assert sum([node.op.__class__ == Cholesky
                        for node in topo_chol]) == 0
            assert sum([node.op.__class__ == CholeskyGrad
                        for node in topo_cholgrad]) == 0
        for shp in [2, 3, 5]:
            m = numpy.cov(rng.randn(shp, shp + 10)).astype(config.floatX)
            yield numpy.testing.assert_equal, f_chol(m), (shp, shp)
            yield numpy.testing.assert_equal, f_cholgrad(m), (shp, shp)
Exemplo n.º 7
0
# -*- coding: utf-8 -*-

"""Module containing models for Gaussian processes."""


import numpy as np
import theano.tensor as T

from theano.sandbox.linalg.ops import MatrixInverse, Det, psd, Cholesky
minv = MatrixInverse()
det = Det()
cholesky = Cholesky()

from ..util import lookup, get_named_variables
from ..component import misc, kernel as kernel_


# TODO document


def parameters(n_inpt):
    return dict(length_scales=n_inpt, noise=1, amplitude=1)


def exprs(inpt, test_inpt, target, length_scales, noise, amplitude, kernel):
    exprs = {}
    # To stay compatible with the prediction api, target will
    # be a matrix to the outside. But in the following, it's easier
    # if it is a vector in the inside. We keep a reference to the
    # matrix anyway, to return it.
Exemplo n.º 8
0
Arquivo: mvn.py Projeto: osdf/breze
# -*- coding: utf-8 -*-

"""Module containing expression buildes for the multivariate normal."""


import numpy as np
import theano.tensor as T
from theano.sandbox.linalg.ops import Det, MatrixInverse, psd, Cholesky

det = Det()
minv = MatrixInverse()
chol = Cholesky()


# TODO add examples that also work as tests.


def pdf(sample, mean, cov):
    """Return a theano expression representing the values of the probability
    density function of the multivariate normal.

    Parameters
    ----------

    sample : Theano variable
        Array of shape ``(n, d)`` where ``n`` is the number of samples and
        ``d`` the dimensionality of the data.

    mean : Theano variable
        Array of shape ``(d,)`` representing the mean of the distribution.