changeset 1526:5804e44d7a1b

pep8
author Frederic Bastien <nouiz@nouiz.org>
date Fri, 09 Nov 2012 16:58:17 -0500
parents 9c24a2bdbe90
children 211b217f9691
files pylearn/sampling/tests/test_hmc.py
diffstat 1 files changed, 18 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/sampling/tests/test_hmc.py	Fri Nov 09 14:48:26 2012 -0500
+++ b/pylearn/sampling/tests/test_hmc.py	Fri Nov 09 16:58:17 2012 -0500
@@ -4,34 +4,34 @@
 
 from pylearn.sampling.hmc import HMC_sampler
 
+
 def _sampler_on_2d_gaussian(sampler_cls, burnin, n_samples):
-    batchsize=3
+    batchsize = 3
 
     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
+    tmp = rng.randn(2, 2).astype(theano.config.floatX)
+    tmp[0] += tmp[1]  # induce some covariance
     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 * (tensor.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 = theano.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)
     print 'initial stepsize', sampler.stepsize.get_value(borrow=True)
 
     # DRAW SAMPLES
-
-    samples = [sampler.draw() for r in xrange(burnin)] #burn-in
+    samples = [sampler.draw() for r in xrange(burnin)]  # burn-in
     samples = numpy.asarray([sampler.draw() for r in xrange(n_samples)])
 
     assert sampler.avg_acceptance_rate.get_value() > 0
@@ -45,24 +45,25 @@
     print 'empirical mean: ', samples.mean(axis=0)
     #assert numpy.all(abs(mu - samples.mean(axis=0)) < 1)
 
-
     print 'final stepsize', sampler.stepsize.get_value()
     print 'final acceptance_rate', sampler.avg_acceptance_rate.get_value()
 
     print 'target cov', cov
-    s = samples[:,0,:]
-    empirical_cov = numpy.cov(samples[:,0,:].T)
+    s = samples[:, 0, :]
+    empirical_cov = numpy.cov(samples[:, 0, :].T)
     print ''
-    print 'cov/empirical_cov', cov/empirical_cov
-    empirical_cov = numpy.cov(samples[:,1,:].T)
-    print 'cov/empirical_cov', cov/empirical_cov
-    empirical_cov = numpy.cov(samples[:,2,:].T)
-    print 'cov/empirical_cov', cov/empirical_cov
+    print 'cov/empirical_cov', cov / empirical_cov
+    empirical_cov = numpy.cov(samples[:, 1, :].T)
+    print 'cov/empirical_cov', cov / empirical_cov
+    empirical_cov = numpy.cov(samples[:, 2, :].T)
+    print 'cov/empirical_cov', cov / empirical_cov
     return sampler
 
+
 def test_hmc():
     print ('HMC')
-    sampler = _sampler_on_2d_gaussian(HMC_sampler.new_from_shared_positions, burnin=3000/20, n_samples=90000/20)
+    sampler = _sampler_on_2d_gaussian(HMC_sampler.new_from_shared_positions,
+                                      burnin=3000 / 20, n_samples=90000 / 20)
     assert abs(sampler.avg_acceptance_rate.get_value() - sampler.target_acceptance_rate) < .1
     assert sampler.stepsize.get_value() >= sampler.stepsize_min
     assert sampler.stepsize.get_value() <= sampler.stepsize_max