annotate pylearn/sampling/tests/test_hmc.py @ 1528:dee276045768

make test don't run in debug mode as it is too slow.
author Frederic Bastien <nouiz@nouiz.org>
date Fri, 09 Nov 2012 17:06:30 -0500
parents 211b217f9691
children
rev   line source
1503
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
1 import numpy
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
2 import theano
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
3 from theano import tensor
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
4
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
5 from pylearn.sampling.hmc import HMC_sampler
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
6
1526
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
7
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
8 def _sampler_on_2d_gaussian(sampler_cls, burnin, n_samples):
1526
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
9 batchsize = 3
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
10
1503
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
11 rng = numpy.random.RandomState(234)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
12
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
13 #
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
14 # Define a covariance and mu for a gaussian
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
15 #
1526
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
16 tmp = rng.randn(2, 2).astype(theano.config.floatX)
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
17 tmp[0] += tmp[1] # induce some covariance
1503
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
18 cov = numpy.dot(tmp, tmp.T)
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
19 cov_inv = numpy.linalg.inv(cov).astype(theano.config.floatX)
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
20 mu = numpy.asarray([5, 9.5], dtype=theano.config.floatX)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
21
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
22 def gaussian_energy(xlist):
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 955
diff changeset
23 x = xlist
1526
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
24 return 0.5 * (tensor.dot((x - mu), cov_inv) * (x - mu)).sum(axis=1)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
25
1526
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
26 position = theano.shared(rng.randn(batchsize,
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
27 2).astype(theano.config.floatX))
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 955
diff changeset
28 sampler = sampler_cls(position, gaussian_energy)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
29
1447
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1374
diff changeset
30 print 'initial position', position.get_value(borrow=True)
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1374
diff changeset
31 print 'initial stepsize', sampler.stepsize.get_value(borrow=True)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
32
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
33 # DRAW SAMPLES
1526
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
34 samples = [sampler.draw() for r in xrange(burnin)] # burn-in
1503
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
35 samples = numpy.asarray([sampler.draw() for r in xrange(n_samples)])
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
36
1447
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1374
diff changeset
37 assert sampler.avg_acceptance_rate.get_value() > 0
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1374
diff changeset
38 assert sampler.avg_acceptance_rate.get_value() < 1
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
39
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
40 # TEST THAT THEY ARE FROM THE RIGHT DISTRIBUTION
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
41
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 955
diff changeset
42 # samples.shape == (1000, 3, 2)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
43
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
44 print 'target mean:', mu
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 955
diff changeset
45 print 'empirical mean: ', samples.mean(axis=0)
1503
1ee532a6f33b Fix import.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
46 #assert numpy.all(abs(mu - samples.mean(axis=0)) < 1)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
47
1447
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1374
diff changeset
48 print 'final stepsize', sampler.stepsize.get_value()
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1374
diff changeset
49 print 'final acceptance_rate', sampler.avg_acceptance_rate.get_value()
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
50
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
51 print 'target cov', cov
1526
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
52 s = samples[:, 0, :]
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
53 empirical_cov = numpy.cov(samples[:, 0, :].T)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
54 print ''
1526
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
55 print 'cov/empirical_cov', cov / empirical_cov
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
56 empirical_cov = numpy.cov(samples[:, 1, :].T)
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
57 print 'cov/empirical_cov', cov / empirical_cov
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
58 empirical_cov = numpy.cov(samples[:, 2, :].T)
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
59 print 'cov/empirical_cov', cov / empirical_cov
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
60 return sampler
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
61
1526
Frederic Bastien <nouiz@nouiz.org>
parents: 1503
diff changeset
62
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
63 def test_hmc():
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
64 print ('HMC')
1528
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
65 try:
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
66 orig_mode = theano.config.mode
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
67 if theano.config.mode in ["DebugMode", "DEBUG_MODE"]:
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
68 theano.config.mode = "FAST_RUN"
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
69 sampler = _sampler_on_2d_gaussian(
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
70 HMC_sampler.new_from_shared_positions,
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
71 burnin=3000 / 20, n_samples=90000 / 20)
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
72 finally:
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
73 theano.config.mode = orig_mode
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
74
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
75
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
76 assert abs(sampler.avg_acceptance_rate.get_value() -
dee276045768 make test don't run in debug mode as it is too slow.
Frederic Bastien <nouiz@nouiz.org>
parents: 1527
diff changeset
77 sampler.target_acceptance_rate) < .1
1447
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1374
diff changeset
78 assert sampler.stepsize.get_value() >= sampler.stepsize_min
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1374
diff changeset
79 assert sampler.stepsize.get_value() <= sampler.stepsize_max