def __init__(self, kernel, X=None, Y=None): self.kernel = kernel self.X = X self.Y = Y self.th_hyp = self.kernel.th_hyp self.th_X = self.kernel.th_X self.th_N = self.kernel.th_N self.th_D = self.kernel.th_D self.th_K = self.kernel.th_K self.th_Y = T.matrix('Y') prec = sT.matrix_inverse(self.th_K) # Calculate the lml in a slow but stable way self.th_lml_stable = ( -0.5 * sT.trace(T.dot(self.th_Y.T, T.dot(prec, self.th_Y))) + -T.sum(T.log(sT.diag(sT.cholesky(self.th_K)))) + -0.5 * self.th_N * T.log(2.0 * const.pi)) # or in a fast but unstable way self.th_lml = ( -0.5 * sT.trace(T.dot(self.th_Y.T, T.dot(prec, self.th_Y))) + -0.5 * T.log(sT.det(self.th_K)) + -0.5 * self.th_N * T.log(2.0 * const.pi)) self.th_dlml_dhyp = theano.grad(self.th_lml, self.th_hyp) # Compile them to functions self.lml = theano.function([self.th_hyp, self.th_X, self.th_Y], self.th_lml) self.lml_stable = theano.function([self.th_hyp, self.th_X, self.th_Y], self.th_lml_stable) self.dlml_dhyp = theano.function([self.th_hyp, self.th_X, self.th_Y], self.th_dlml_dhyp)
def test_cholesky(): 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
def test_cholesky_and_cholesky_grad_shape(): 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.env.toposort() topo_cholgrad = f_cholgrad.maker.env.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)
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
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)
def test_cholesky(): #TODO: test upper and lower triangular #todo: unittest randomseed rng = numpy.random.RandomState(utt.fetch_seed()) r = rng.randn(5,5) pd = numpy.dot(r,r.T) x = tensor.matrix() chol = cholesky(x) f = function([x], tensor.dot(chol, chol.T)) # an optimization could remove this ch_f = function([x], chol) # quick check that chol is upper-triangular ch = ch_f(pd) print ch assert ch[0,4] != 0 assert ch[4,0] == 0 assert numpy.allclose(numpy.dot(ch.T,ch),pd) assert not numpy.allclose(numpy.dot(ch,ch.T),pd)
x = rnd.randn(N, 1) D = x.shape[0] d = x - mu p = T.log(T.prod((2*const.pi)**(-0.5*D) * sT.det(sigma)**-0.5 * T.exp(sT.diag(-0.5*T.dot(d, T.dot(prec, d.T)))))) p1 = T.sum(-0.5*D*T.log(2*const.pi) + -0.5*T.log(sT.det(sigma)) + -0.5*sT.diag(T.dot(d, T.dot(prec, d.T))) ) p2 = T.sum(-0.5*D*T.log(2*const.pi) + -T.sum(T.log(sT.diag(sT.cholesky(sigma)))) + -0.5*sT.diag(T.dot(d, T.dot(prec, d.T))) ) fp = th.function([mu, sigma], p) fp1 = th.function([mu, sigma], p1) fp2 = th.function([mu, sigma], p2) # GP inputs, N points, 2D z = rnd.randn(N, 2) kern = GPy.kern.rbf(2) + GPy.kern.white(2, 10**-6) curmu = np.zeros(N) cursig = kern.K(z) # stabdet = 2*T.sum(T.log(sT.diag(sT.cholesky(sigma)))) # fdet = th.function([sigma], stabdet)