# HG changeset patch # User Frederic Bastien # Date 1315837464 14400 # Node ID 1ee532a6f33b0664aea70dcc56b4652e722f9adb # Parent 4fa5ebe8a7adc025f836f737897558bf8302743a Fix import. diff -r 4fa5ebe8a7ad -r 1ee532a6f33b pylearn/sampling/tests/test_hmc.py --- a/pylearn/sampling/tests/test_hmc.py Fri Sep 09 10:54:17 2011 -0400 +++ b/pylearn/sampling/tests/test_hmc.py Mon Sep 12 10:24:24 2011 -0400 @@ -1,25 +1,29 @@ -from pylearn.sampling.hmc import * +import numpy +import theano +from theano import tensor + +from pylearn.sampling.hmc import HMC_sampler def _sampler_on_2d_gaussian(sampler_cls, burnin, n_samples): batchsize=3 - rng = np.random.RandomState(234) + rng = numpy.random.RandomState(234) # # Define a covariance and mu for a gaussian # tmp = rng.randn(2,2).astype(theano.config.floatX) tmp[0] += tmp[1] #induce some covariance - cov = np.dot(tmp, tmp.T) - cov_inv = np.linalg.inv(cov).astype(theano.config.floatX) - mu = np.asarray([5, 9.5], dtype=theano.config.floatX) + cov = numpy.dot(tmp, tmp.T) + cov_inv = numpy.linalg.inv(cov).astype(theano.config.floatX) + mu = numpy.asarray([5, 9.5], dtype=theano.config.floatX) def gaussian_energy(xlist): x = xlist - return 0.5 * (TT.dot((x-mu),cov_inv)*(x-mu)).sum(axis=1) + return 0.5 * (tensor.dot((x-mu),cov_inv)*(x-mu)).sum(axis=1) - position = shared(rng.randn(batchsize, 2).astype(theano.config.floatX)) + position = theano.shared(rng.randn(batchsize, 2).astype(theano.config.floatX)) sampler = sampler_cls(position, gaussian_energy) print 'initial position', position.get_value(borrow=True) @@ -28,7 +32,7 @@ # DRAW SAMPLES samples = [sampler.draw() for r in xrange(burnin)] #burn-in - samples = np.asarray([sampler.draw() for r in xrange(n_samples)]) + samples = numpy.asarray([sampler.draw() for r in xrange(n_samples)]) assert sampler.avg_acceptance_rate.get_value() > 0 assert sampler.avg_acceptance_rate.get_value() < 1 @@ -39,7 +43,7 @@ print 'target mean:', mu print 'empirical mean: ', samples.mean(axis=0) - #assert np.all(abs(mu - samples.mean(axis=0)) < 1) + #assert numpy.all(abs(mu - samples.mean(axis=0)) < 1) print 'final stepsize', sampler.stepsize.get_value() @@ -47,12 +51,12 @@ print 'target cov', cov s = samples[:,0,:] - empirical_cov = np.cov(samples[:,0,:].T) + empirical_cov = numpy.cov(samples[:,0,:].T) print '' print 'cov/empirical_cov', cov/empirical_cov - empirical_cov = np.cov(samples[:,1,:].T) + empirical_cov = numpy.cov(samples[:,1,:].T) print 'cov/empirical_cov', cov/empirical_cov - empirical_cov = np.cov(samples[:,2,:].T) + empirical_cov = numpy.cov(samples[:,2,:].T) print 'cov/empirical_cov', cov/empirical_cov return sampler