def test_det_shape(): rng = numpy.random.RandomState(utt.fetch_seed()) r = rng.randn(5, 5).astype(config.floatX) x = tensor.matrix() f = theano.function([x], det(x)) f_shape = theano.function([x], det(x).shape) assert numpy.all(f(r).shape == f_shape(r))
def test_det(): rng = numpy.random.RandomState(utt.fetch_seed()) r = rng.randn(5, 5).astype(config.floatX) x = tensor.matrix() f = theano.function([x], det(x)) assert numpy.allclose(numpy.linalg.det(r), f(r))
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 __init__(self, n_in, n_out, hls, acts): self.net = MLP(n_in, n_out, hls, acts) self.params = self.net.params self.X = T.vector('X') self.X.tag.test_value = numpy.random.uniform(size=(784, ), high=1, low=0.0).astype('float32') self.lr = T.scalar('lr') self.lr.tag.test_value = 0.25 self.Z = self.net(self.X) self.W = self.net.layers[0].W #self.dtanh = 1 - self.Z**2 self.J = self.W self.logpx = T.log(T.abs_(det(self.J))) self.grads = T.grad(self.logpx, self.net.params) self.updates = map(lambda (param, grad): (param, param - self.lr * grad), zip(self.params, self.grads)) self.train_fn = theano.function([self.X, self.lr], self.logpx, updates=self.updates)
def __init__(self, n_in, n_out, hls, acts): self.net = MLP(n_in, n_out, hls, acts) self.params = self.net.params self.X = T.vector('X') self.X.tag.test_value = numpy.random.uniform(size=(784, ), high=1, low=0.0).astype('float32') self.lr = T.scalar('lr') self.lr.tag.test_value = 0.25 self.Z = self.net(self.X) self.W = self.net.layers[0].W #self.dtanh = 1 - self.Z**2 self.J = self.W self.logpx = T.log(T.abs_(det(self.J))) self.grads = T.grad(self.logpx, self.net.params) self.updates = map( lambda (param, grad): (param, param - self.lr * grad), zip(self.params, self.grads)) self.train_fn = theano.function([self.X, self.lr], self.logpx, updates=self.updates)
# [-1.5, 1.5], # [-1.4, 1.5], # [1.4, -1.5], # [-45.0, 83.5], # [-100.3, 68.3], # [1000.4, 432.4], # [32441.8, 12341.3]]) N = 100 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)
import os mu = T.vector('mu') sigma = T.matrix('sigma') prec = sT.matrix_inverse(sigma) x = np.array([[1.5, -1.5], [-1.5, 1.5], [-1.4, 1.5], [1.4, -1.5]]) D = x.shape[1] N = x.shape[0] d = x - mu #p = -(const.pi)T.dot(T.dot(d, sigma), d.T).trace() #p = T.log((2*const.pi)**(-0.5*D) * ) #p = sT.det(sigma) + T.dot(mu, 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)))))) dp_dmu = T.grad(p, mu) dp_dsigma = T.grad(p, sigma) fp = th.function([mu, sigma], p) fd = th.function([mu], d) fdp_dmu = th.function([mu, sigma], dp_dmu) fdp_dsigma = th.function([mu, sigma], dp_dsigma) curmu = np.array([7.5, -3.23]) cursig = np.array([[1., 0], [0, 1.]]) # curmu = np.zeros(2) # cursig = np.dot(x.T, x) / N