comparison pylearn/sampling/tests/test_hmc.py @ 1447:fbe470217937

Use .get_value() and .set_value() of shared instead of the .value property
author Pascal Lamblin <lamblinp@iro.umontreal.ca>
date Wed, 16 Mar 2011 20:20:02 -0400
parents 8b61566b0d36
children 1ee532a6f33b
comparison
equal deleted inserted replaced
1446:6e50d209b5f1 1447:fbe470217937
20 20
21 21
22 position = shared(rng.randn(batchsize, 2).astype(theano.config.floatX)) 22 position = shared(rng.randn(batchsize, 2).astype(theano.config.floatX))
23 sampler = sampler_cls(position, gaussian_energy) 23 sampler = sampler_cls(position, gaussian_energy)
24 24
25 print 'initial position', position.value 25 print 'initial position', position.get_value(borrow=True)
26 print 'initial stepsize', sampler.stepsize.value 26 print 'initial stepsize', sampler.stepsize.get_value(borrow=True)
27 27
28 # DRAW SAMPLES 28 # DRAW SAMPLES
29 29
30 samples = [sampler.draw() for r in xrange(burnin)] #burn-in 30 samples = [sampler.draw() for r in xrange(burnin)] #burn-in
31 samples = np.asarray([sampler.draw() for r in xrange(n_samples)]) 31 samples = np.asarray([sampler.draw() for r in xrange(n_samples)])
32 32
33 assert sampler.avg_acceptance_rate.value > 0 33 assert sampler.avg_acceptance_rate.get_value() > 0
34 assert sampler.avg_acceptance_rate.value < 1 34 assert sampler.avg_acceptance_rate.get_value() < 1
35 35
36 # TEST THAT THEY ARE FROM THE RIGHT DISTRIBUTION 36 # TEST THAT THEY ARE FROM THE RIGHT DISTRIBUTION
37 37
38 # samples.shape == (1000, 3, 2) 38 # samples.shape == (1000, 3, 2)
39 39
40 print 'target mean:', mu 40 print 'target mean:', mu
41 print 'empirical mean: ', samples.mean(axis=0) 41 print 'empirical mean: ', samples.mean(axis=0)
42 #assert np.all(abs(mu - samples.mean(axis=0)) < 1) 42 #assert np.all(abs(mu - samples.mean(axis=0)) < 1)
43 43
44 44
45 print 'final stepsize', sampler.stepsize.value 45 print 'final stepsize', sampler.stepsize.get_value()
46 print 'final acceptance_rate', sampler.avg_acceptance_rate.value 46 print 'final acceptance_rate', sampler.avg_acceptance_rate.get_value()
47 47
48 print 'target cov', cov 48 print 'target cov', cov
49 s = samples[:,0,:] 49 s = samples[:,0,:]
50 empirical_cov = np.cov(samples[:,0,:].T) 50 empirical_cov = np.cov(samples[:,0,:].T)
51 print '' 51 print ''
57 return sampler 57 return sampler
58 58
59 def test_hmc(): 59 def test_hmc():
60 print ('HMC') 60 print ('HMC')
61 sampler = _sampler_on_2d_gaussian(HMC_sampler.new_from_shared_positions, burnin=3000/20, n_samples=90000/20) 61 sampler = _sampler_on_2d_gaussian(HMC_sampler.new_from_shared_positions, burnin=3000/20, n_samples=90000/20)
62 assert abs(sampler.avg_acceptance_rate.value - sampler.target_acceptance_rate) < .1 62 assert abs(sampler.avg_acceptance_rate.get_value() - sampler.target_acceptance_rate) < .1
63 assert sampler.stepsize.value >= sampler.stepsize_min 63 assert sampler.stepsize.get_value() >= sampler.stepsize_min
64 assert sampler.stepsize.value <= sampler.stepsize_max 64 assert sampler.stepsize.get_value() <= sampler.stepsize_max
65 65