# HG changeset patch # User Frederic Bastien # Date 1315580057 14400 # Node ID 4fa5ebe8a7adc025f836f737897558bf8302743a # Parent 55534951dd911adc7541aac4d5d5297237189cc6 Auto white space fix. diff -r 55534951dd91 -r 4fa5ebe8a7ad pylearn/sampling/hmc.py --- a/pylearn/sampling/hmc.py Fri Sep 09 10:53:46 2011 -0400 +++ b/pylearn/sampling/hmc.py Fri Sep 09 10:54:17 2011 -0400 @@ -16,7 +16,7 @@ expression graph. The initialize_dynamics() theano-function does several things: -1. samples a random velocity for each particle (saving it to self.velocities) +1. samples a random velocity for each particle (saving it to self.velocities) 2. calculates the initial hamiltonian based on those velocities (saving it to self.initial_hamiltonian) 3. saves self.positions to self.initial_positions. @@ -92,11 +92,11 @@ pos: theano matrix in leapfrog update equations, represents pos(t), position at time t vel: theano matrix - in leapfrog update equations, represents vel(t - stepsize/2), + in leapfrog update equations, represents vel(t - stepsize/2), velocity at time (t - stepsize/2) step: theano scalar scalar value controlling amount by which to move - + Returns ------- rval1: [theano matrix, theano matrix] @@ -121,7 +121,7 @@ # perform leapfrog updates: the scan op is used to repeatedly compute pos(t_1 + n*sigma) and # vel(t_1 + n*sigma + 1/2) for n in [0,n_steps-2]. - (all_p, all_v), scan_updates = theano.scan(leapfrog, + (all_p, all_v), scan_updates = theano.scan(leapfrog, outputs_info=[ dict(initial=p_full_step), dict(initial=v_half_step), @@ -137,7 +137,7 @@ # function, to avoid drawing the same random numbers each time the function is called. In # this case however, we consciously ignore "scan_updates" because we know it is empty. assert not scan_updates - + # The last velocity returned by the scan op is at time-step: t + n_steps* stepsize - 1/2 # We therefore perform one more half-step to return vel(t + n_steps*stepsize) energy = energy_fn(final_p) @@ -156,7 +156,7 @@ initial_v = s_rng.normal(size=positions_shape) final_p, final_v = simulate_dynamics( - initial_p = positions, + initial_p = positions, initial_v = initial_v, stepsize = stepsize, n_steps = n_steps, @@ -166,10 +166,10 @@ energy_prev = Print('ep')(hamiltonian(positions, initial_v, energy_fn)), energy_next = Print('en')(hamiltonian(final_p, final_v, energy_fn)), s_rng=s_rng, shape=(batchsize,)) - + return Print('accept')(accept), final_p -def mcmc_updates(shrd_pos, shrd_stepsize, shrd_avg_acceptance_rate, final_p, accept, +def mcmc_updates(shrd_pos, shrd_stepsize, shrd_avg_acceptance_rate, final_p, accept, target_acceptance_rate, stepsize_inc, stepsize_dec, @@ -183,9 +183,9 @@ accept.dimshuffle(0, *(('x',)*(final_p.ndim-1))), final_p, shrd_pos)), - (shrd_stepsize, + (shrd_stepsize, TT.clip( - TT.switch( + TT.switch( shrd_avg_acceptance_rate > target_acceptance_rate, shrd_stepsize * stepsize_inc, shrd_stepsize * stepsize_dec, @@ -214,7 +214,7 @@ self.__dict__.update(kwargs) @classmethod - def new_from_shared_positions(cls, shared_positions, energy_fn, + def new_from_shared_positions(cls, shared_positions, energy_fn, initial_stepsize=0.01, target_acceptance_rate=.9, n_steps=20, stepsize_dec = 0.98, stepsize_min = 0.001, @@ -222,13 +222,13 @@ stepsize_inc = 1.02, avg_acceptance_slowness = 0.9, # used in geometric avg. 1.0 would be not moving at all seed=12345, dtype=theano.config.floatX, - shared_positions_shape=None, + shared_positions_shape=None, compile_simulate=True): """ :param shared_positions: theano ndarray shared var with many particle [initial] positions :param energy_fn: - callable such that energy_fn(positions) - returns theano vector of energies. + callable such that energy_fn(positions) + returns theano vector of energies. The len of this vector is the batchsize. The sum of this energy vector must be differentiable (with theano.tensor.grad) with @@ -245,17 +245,17 @@ s_rng = TT.shared_randomstreams.RandomStreams(seed) accept, final_p = mcmc_move( - s_rng, - shared_positions, + s_rng, + shared_positions, energy_fn, - stepsize, + stepsize, n_steps, shared_positions_shape) simulate_updates = mcmc_updates( shared_positions, stepsize, - avg_acceptance_rate, - final_p=final_p, + avg_acceptance_rate, + final_p=final_p, accept=accept, stepsize_min=stepsize_min, stepsize_max=stepsize_max, @@ -298,8 +298,7 @@ """ return list(self._updates) -#TODO: +#TODO: # Consider a heuristic for updating the *MASS* of the particles. We might want the mass to be # such that the momentum is in the same range as the gradient on the energy. Look at Radford's # recent book chapter, maybe there are hints. (2010). -