annotate pylearn/sampling/hmc.py @ 1501:55534951dd91

Clean up import and remove deprecation warning.
author Frederic Bastien <nouiz@nouiz.org>
date Fri, 09 Sep 2011 10:53:46 -0400
parents fbe470217937
children 4fa5ebe8a7ad
rev   line source
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
1
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
2 """Hybrid / Hamiltonian Monte Carlo Sampling
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
3
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
4 This algorithm is described in Radford Neal's PhD Thesis, pages 63--70.
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
5
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
6 :note:
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
7
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
8 The 'mass' of so-called particles is taken to be 1, so that kinetic energy (K) is the sum
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
9 of squared velocities (p).
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
10
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
11 :math:`K = \sum_i p_i^2 / 2`
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
12
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
13
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
14 The 'leap-frog' algorithm that advances by turns the velocity and the position is
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
15 currently implemented via several theano functions, rather than one complete theano
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
16 expression graph.
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
17
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
18 The initialize_dynamics() theano-function does several things:
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
19 1. samples a random velocity for each particle (saving it to self.velocities)
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
20 2. calculates the initial hamiltonian based on those velocities (saving it to
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
21 self.initial_hamiltonian)
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
22 3. saves self.positions to self.initial_positions.
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
23
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
24 The finalize_dynamics() theano-function re-calculates the Hamiltonian for each particle
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
25 based on the self.positions and self.velocities, and then implements the
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
26 Metropolis-Hastings accept/reject for each particle in the simulation by consulting the
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
27 self.initial_hamiltonian storing the result to self.
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
28
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
29
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
30 """
1270
d38cb039c662 debugging mcRBM
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1269
diff changeset
31 import numpy
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
32 from theano import function, shared
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
33 from theano import tensor as TT
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
34 import theano
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
35 from theano.printing import Print
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
36
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
37 def Print(msg):
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
38 return lambda x: x
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
39
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
40 def kinetic_energy(velocities, masses):
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
41 if masses != 1:
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
42 raise NotImplementedError()
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
43 return 0.5 * (velocities**2).sum(axis=1)
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
44
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
45 def metropolis_hastings_accept(energy_prev, energy_next, s_rng, shape=None):
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
46 """
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
47 Return acceptance of moves - energy_prev and energy_next are vectors of the energy of each
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
48 particle.
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
49 """
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
50 if shape is None:
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
51 shape = energy_prev.shape
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
52 ediff = Print('diff')(energy_prev - energy_next)
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
53 return (TT.exp(ediff) - s_rng.uniform(size=shape)) >= 0
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
54
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
55 def hamiltonian(pos, vel, energy_fn):
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
56 """Return a vector of energies - sum of kinetic and potential energy
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
57 """
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
58 # assuming mass is 1
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
59 return energy_fn(pos) + kinetic_energy(vel, 1)
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
60
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
61 def simulate_dynamics(initial_p, initial_v, stepsize, n_steps, energy_fn):
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
62 """
1341
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
63 Return final (position, velocity) obtained after an `n_steps` leapfrog updates, using
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
64 Hamiltonian dynamics.
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
65
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
66 Parameters
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
67 ----------
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
68 initial_p: shared theano matrix
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
69 Initial position at which to start the simulation
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
70 initial_v: shared theano matrix
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
71 Initial velocity of particles
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
72 stepsize: shared theano scalar
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
73 Scalar value controlling amount by which to move
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
74 energy_fn: python function
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
75 Python function, operating on symbolic theano variables, used to compute the potential
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
76 energy at a given position.
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
77
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
78 Returns
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
79 -------
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
80 rval1: theano matrix
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
81 Final positions obtained after simulation
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
82 rval2: theano matrix
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
83 Final velocity obtained after simulation
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
84 """
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
85
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
86 def leapfrog(pos, vel, step):
1341
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
87 """
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
88 Inside loop of Scan. Performs one step of leapfrog update, using Hamiltonian dynamics.
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
89
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
90 Parameters
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
91 ----------
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
92 pos: theano matrix
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
93 in leapfrog update equations, represents pos(t), position at time t
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
94 vel: theano matrix
1342
4ac393ec2eb7 Fixed comments in leapfrog method
gdesjardins
parents: 1341
diff changeset
95 in leapfrog update equations, represents vel(t - stepsize/2),
4ac393ec2eb7 Fixed comments in leapfrog method
gdesjardins
parents: 1341
diff changeset
96 velocity at time (t - stepsize/2)
1341
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
97 step: theano scalar
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
98 scalar value controlling amount by which to move
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
99
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
100 Returns
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
101 -------
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
102 rval1: [theano matrix, theano matrix]
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
103 Symbolic theano matrices for new position pos(t + stepsize), and velocity
1342
4ac393ec2eb7 Fixed comments in leapfrog method
gdesjardins
parents: 1341
diff changeset
104 vel(t + stepsize/2)
1341
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
105 rval2: dictionary
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
106 Dictionary of updates for the Scan Op
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
107 """
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
108 # from pos(t) and vel(t-sigma/2), compute vel(t+sigma/2)
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
109 dE_dpos = TT.grad(energy_fn(pos).sum(), pos)
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
110 new_vel = vel - step * dE_dpos
1341
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
111 # from vel(t+sigma/2) compute pos(t+sigma)
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
112 new_pos = pos + step * new_vel
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
113 return [new_pos, new_vel],{}
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
114
1341
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
115 # compute velocity at time-step: t + sigma/2
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
116 initial_energy = energy_fn(initial_p)
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
117 dE_dpos = TT.grad(initial_energy.sum(), initial_p)
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
118 v_half_step = initial_v - 0.5*stepsize*dE_dpos
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
119
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
120 p_full_step = initial_p + stepsize * v_half_step
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
121
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
122 # perform leapfrog updates: the scan op is used to repeatedly compute pos(t_1 + n*sigma) and
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
123 # vel(t_1 + n*sigma + 1/2) for n in [0,n_steps-2].
1501
55534951dd91 Clean up import and remove deprecation warning.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
124 (all_p, all_v), scan_updates = theano.scan(leapfrog,
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
125 outputs_info=[
1501
55534951dd91 Clean up import and remove deprecation warning.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
126 dict(initial=p_full_step),
55534951dd91 Clean up import and remove deprecation warning.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
127 dict(initial=v_half_step),
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
128 ],
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
129 non_sequences=[stepsize],
1341
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
130 n_steps=n_steps-1)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
131
1501
55534951dd91 Clean up import and remove deprecation warning.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
132 final_p = all_p[-1]
55534951dd91 Clean up import and remove deprecation warning.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
133 final_v = all_v[-1]
55534951dd91 Clean up import and remove deprecation warning.
Frederic Bastien <nouiz@nouiz.org>
parents: 1447
diff changeset
134
1390
746ebceeb46f added comments to hmc code (old outstanding changes)
gdesjardins
parents: 1342
diff changeset
135 # NOTE: Scan always returns an updates dictionary, in case the scanned function draws
746ebceeb46f added comments to hmc code (old outstanding changes)
gdesjardins
parents: 1342
diff changeset
136 # samples from a RandomStream. These updates must then be used when compiling the Theano
746ebceeb46f added comments to hmc code (old outstanding changes)
gdesjardins
parents: 1342
diff changeset
137 # function, to avoid drawing the same random numbers each time the function is called. In
746ebceeb46f added comments to hmc code (old outstanding changes)
gdesjardins
parents: 1342
diff changeset
138 # this case however, we consciously ignore "scan_updates" because we know it is empty.
746ebceeb46f added comments to hmc code (old outstanding changes)
gdesjardins
parents: 1342
diff changeset
139 assert not scan_updates
746ebceeb46f added comments to hmc code (old outstanding changes)
gdesjardins
parents: 1342
diff changeset
140
1341
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
141 # The last velocity returned by the scan op is at time-step: t + n_steps* stepsize - 1/2
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
142 # We therefore perform one more half-step to return vel(t + n_steps*stepsize)
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
143 energy = energy_fn(final_p)
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
144 final_v = final_v - 0.5 * stepsize * TT.grad(energy.sum(), final_p)
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
145 return final_p, final_v
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
146
1341
fbe4a6383441 HMC: perform half-step on velocity first (instead of position).
gdesjardins
parents: 1280
diff changeset
147
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
148 def mcmc_move(s_rng, positions, energy_fn, stepsize, n_steps, positions_shape=None):
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
149 """Return new position
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
150 """
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
151 if positions_shape is None:
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
152 positions_shape = positions.shape
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
153
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
154 batchsize = positions_shape[0]
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
155
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
156 initial_v = s_rng.normal(size=positions_shape)
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
157
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
158 final_p, final_v = simulate_dynamics(
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
159 initial_p = positions,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
160 initial_v = initial_v,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
161 stepsize = stepsize,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
162 n_steps = n_steps,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
163 energy_fn = energy_fn)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
164
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
165 accept = metropolis_hastings_accept(
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
166 energy_prev = Print('ep')(hamiltonian(positions, initial_v, energy_fn)),
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
167 energy_next = Print('en')(hamiltonian(final_p, final_v, energy_fn)),
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
168 s_rng=s_rng, shape=(batchsize,))
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
169
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
170 return Print('accept')(accept), final_p
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
171
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
172 def mcmc_updates(shrd_pos, shrd_stepsize, shrd_avg_acceptance_rate, final_p, accept,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
173 target_acceptance_rate,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
174 stepsize_inc,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
175 stepsize_dec,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
176 stepsize_min,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
177 stepsize_max,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
178 avg_acceptance_slowness
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
179 ):
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
180 return [
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
181 (shrd_pos,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
182 TT.switch(
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
183 accept.dimshuffle(0, *(('x',)*(final_p.ndim-1))),
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
184 final_p,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
185 shrd_pos)),
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
186 (shrd_stepsize,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
187 TT.clip(
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
188 TT.switch(
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
189 shrd_avg_acceptance_rate > target_acceptance_rate,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
190 shrd_stepsize * stepsize_inc,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
191 shrd_stepsize * stepsize_dec,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
192 ),
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
193 stepsize_min,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
194 stepsize_max)),
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
195 (shrd_avg_acceptance_rate,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
196 Print('arate')(TT.add(
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
197 avg_acceptance_slowness * shrd_avg_acceptance_rate,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
198 (1.0 - avg_acceptance_slowness) * accept.mean()))),
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
199 ]
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
200
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
201 class HMC_sampler(object):
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
202 """Convenience wrapper for HMC
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
203
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
204 The `draw` function advances the markov chain and returns the current sample by calling
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
205 `simulate` and `get_position` in sequence.
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
206
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
207 """
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
208
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
209 # Constants taken from Marc'Aurelio's 'train_mcRBM.py' file found in the code online for his
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
210 # paper.
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
211
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
212 def __init__(self, **kwargs):
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
213 # add things to __dict__
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
214 self.__dict__.update(kwargs)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
215
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
216 @classmethod
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
217 def new_from_shared_positions(cls, shared_positions, energy_fn,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
218 initial_stepsize=0.01, target_acceptance_rate=.9, n_steps=20,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
219 stepsize_dec = 0.98,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
220 stepsize_min = 0.001,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
221 stepsize_max = 0.25,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
222 stepsize_inc = 1.02,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
223 avg_acceptance_slowness = 0.9, # used in geometric avg. 1.0 would be not moving at all
1280
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
224 seed=12345, dtype=theano.config.floatX,
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
225 shared_positions_shape=None,
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
226 compile_simulate=True):
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
227 """
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
228 :param shared_positions: theano ndarray shared var with many particle [initial] positions
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
229 :param energy_fn:
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
230 callable such that energy_fn(positions)
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
231 returns theano vector of energies.
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
232 The len of this vector is the batchsize.
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
233
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
234 The sum of this energy vector must be differentiable (with theano.tensor.grad) with
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
235 respect to the positions for HMC sampling to work.
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
236 """
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
237 # allocate shared vars
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
238
1280
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
239 if shared_positions_shape==None:
1447
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1390
diff changeset
240 shared_positions_shape = shared_positions.get_value(borrow=True).shape
1280
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
241 batchsize = shared_positions_shape[0]
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
242
1270
d38cb039c662 debugging mcRBM
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1269
diff changeset
243 stepsize = shared(numpy.asarray(initial_stepsize).astype(theano.config.floatX), 'hmc_stepsize')
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
244 avg_acceptance_rate = shared(target_acceptance_rate, 'avg_acceptance_rate')
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
245 s_rng = TT.shared_randomstreams.RandomStreams(seed)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
246
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
247 accept, final_p = mcmc_move(
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
248 s_rng,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
249 shared_positions,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
250 energy_fn,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
251 stepsize,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
252 n_steps,
1280
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
253 shared_positions_shape)
1266
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
254 simulate_updates = mcmc_updates(
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
255 shared_positions,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
256 stepsize,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
257 avg_acceptance_rate,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
258 final_p=final_p,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
259 accept=accept,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
260 stepsize_min=stepsize_min,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
261 stepsize_max=stepsize_max,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
262 stepsize_inc=stepsize_inc,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
263 stepsize_dec=stepsize_dec,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
264 target_acceptance_rate=target_acceptance_rate,
bc4d98995bad hmc - separated simulate into simulate_updates
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1265
diff changeset
265 avg_acceptance_slowness=avg_acceptance_slowness)
1280
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
266 if compile_simulate:
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
267 simulate = function([], [], updates=simulate_updates)
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
268 else:
1eaa015e88c1 extended hmc to allow for use as a cd1 sampler
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1274
diff changeset
269 simulate = None
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
270 return cls(
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
271 positions=shared_positions,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
272 stepsize=stepsize,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
273 stepsize_min=stepsize_min,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
274 stepsize_max=stepsize_max,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
275 avg_acceptance_rate=avg_acceptance_rate,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
276 target_acceptance_rate=target_acceptance_rate,
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
277 s_rng=s_rng,
1274
9d5905d6d879 hmc - changed updates to member fn from lambda for pickling
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1271
diff changeset
278 _updates=simulate_updates,
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
279 simulate=simulate)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
280
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
281 def draw(self, **kwargs):
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
282 """Return the current sample in the Markov chain as a list of numpy arrays
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
283
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
284 The size of the arrays will match the size of the `initial_position` argument to
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
285 __init__.
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
286
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
287 The `kwargs` dictionary is passed to the shared variable (self.positions) `get_value()`
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
288 function. So for example, to avoid copying the shared variable value, consider passing
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
289 `borrow=True`.
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
290 """
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
291 self.simulate()
1447
fbe470217937 Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents: 1390
diff changeset
292 return self.positions.get_value(borrow=False)
955
492473059b37 Adding sampling module
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
293
1274
9d5905d6d879 hmc - changed updates to member fn from lambda for pickling
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1271
diff changeset
294 def updates(self):
9d5905d6d879 hmc - changed updates to member fn from lambda for pickling
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1271
diff changeset
295 """Returns the update expressions required to simulate the Markov Chain
9d5905d6d879 hmc - changed updates to member fn from lambda for pickling
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1271
diff changeset
296
9d5905d6d879 hmc - changed updates to member fn from lambda for pickling
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1271
diff changeset
297 :TODO: :WRITEME: *prescriptive* definition of what this method does (API)
9d5905d6d879 hmc - changed updates to member fn from lambda for pickling
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1271
diff changeset
298 """
9d5905d6d879 hmc - changed updates to member fn from lambda for pickling
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1271
diff changeset
299 return list(self._updates)
9d5905d6d879 hmc - changed updates to member fn from lambda for pickling
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1271
diff changeset
300
1265
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
301 #TODO:
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
302 # Consider a heuristic for updating the *MASS* of the particles. We might want the mass to be
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
303 # such that the momentum is in the same range as the gradient on the energy. Look at Radford's
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
304 # recent book chapter, maybe there are hints. (2010).
d6665a5af743 hmc - replaced with new refactored code
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1264
diff changeset
305