88from theano import tensor as TT
99import theano
1010
11- sharedX = lambda X , name : \
11+ sharedX = lambda X , name : \
1212 shared (numpy .asarray (X , dtype = theano .config .floatX ), name = name )
1313
14+
1415def kinetic_energy (vel ):
15- """
16- Returns the kinetic energy associated with the given velocity and mass of 1.
16+ """Returns the kinetic energy associated with the given velocity
17+ and mass of 1.
1718
1819 Parameters
1920 ----------
@@ -26,7 +27,7 @@ def kinetic_energy(vel):
2627 Vector whose i-th entry is the kinetic entry associated with vel[i].
2728
2829 """
29- return 0.5 * (vel ** 2 ).sum (axis = 1 )
30+ return 0.5 * (vel ** 2 ).sum (axis = 1 )
3031
3132
3233def hamiltonian (pos , vel , energy_fn ):
@@ -41,8 +42,8 @@ def hamiltonian(pos, vel, energy_fn):
4142 vel: theano matrix
4243 Symbolic matrix whose rows are velocity vectors.
4344 energy_fn: python function
44- Python function, operating on symbolic theano variables, used to compute
45- the potential energy at a given position.
45+ Python function, operating on symbolic theano variables, used tox
46+ compute the potential energy at a given position.
4647
4748 Returns
4849 -------
@@ -57,7 +58,7 @@ def hamiltonian(pos, vel, energy_fn):
5758def metropolis_hastings_accept (energy_prev , energy_next , s_rng ):
5859 """
5960 Performs a Metropolis-Hastings accept-reject move.
60-
61+
6162 Parameters
6263 ----------
6364 energy_prev: theano vector
@@ -132,12 +133,12 @@ def leapfrog(pos, vel, step):
132133 new_vel = vel - step * dE_dpos
133134 # from vel(t+stepsize/2) compute pos(t+stepsize)
134135 new_pos = pos + step * new_vel
135- return [new_pos , new_vel ],{}
136+ return [new_pos , new_vel ], {}
136137
137138 # compute velocity at time-step: t + stepsize/2
138139 initial_energy = energy_fn (initial_pos )
139140 dE_dpos = TT .grad (initial_energy .sum (), initial_pos )
140- vel_half_step = initial_vel - 0.5 * stepsize * dE_dpos
141+ vel_half_step = initial_vel - 0.5 * stepsize * dE_dpos
141142
142143 # compute position at time-step: t + stepsize
143144 pos_full_step = initial_pos + stepsize * vel_half_step
@@ -150,13 +151,15 @@ def leapfrog(pos, vel, step):
150151 dict (initial = vel_half_step ),
151152 ],
152153 non_sequences = [stepsize ],
153- n_steps = n_steps - 1 )
154+ n_steps = n_steps - 1 )
154155 final_pos = all_pos [- 1 ]
155156 final_vel = all_vel [- 1 ]
156- # NOTE: Scan always returns an updates dictionary, in case the scanned function draws
157- # samples from a RandomStream. These updates must then be used when compiling the Theano
158- # function, to avoid drawing the same random numbers each time the function is called. In
159- # this case however, we consciously ignore "scan_updates" because we know it is empty.
157+ # NOTE: Scan always returns an updates dictionary, in case the
158+ # scanned function draws samples from a RandomStream. These
159+ # updates must then be used when compiling the Theano function, to
160+ # avoid drawing the same random numbers each time the function is
161+ # called. In this case however, we consciously ignore
162+ # "scan_updates" because we know it is empty.
160163 assert not scan_updates
161164
162165 # The last velocity returned by scan is vel(t + (n_steps-1/2)*stepsize)
0 commit comments