# HG changeset patch # User James Bergstra # Date 1282683669 14400 # Node ID c6d08a76096058b71f32ace3a9846144abeb51ea # Parent 8ba8b08e0442daedac3dcacf5d47fa7cfdd6e211 added sgd_updates to gd/sgd.py. Modif mcRBM to use it. diff -r 8ba8b08e0442 -r c6d08a760960 pylearn/algorithms/mcRBM.py --- a/pylearn/algorithms/mcRBM.py Tue Aug 24 16:51:53 2010 -0400 +++ b/pylearn/algorithms/mcRBM.py Tue Aug 24 17:01:09 2010 -0400 @@ -203,6 +203,7 @@ import pylearn from pylearn.sampling.hmc import HMC_sampler from pylearn.io import image_tiling +from pylearn.gd.sgd import sgd_updates #TODO: This should be in the datasets folder import pylearn.datasets.config @@ -215,15 +216,6 @@ #TODO: This should be in the nnet part of the library -def sgd_updates(params, grads, lr): - try: - float(lr) - lr = [lr for p in params] - except TypeError: - pass - updates = [(p, p - plr * gp) for (plr, p, gp) in zip(lr, params, grads)] - return updates - def hidden_cov_units_preactivation_given_v(rbm, v, small=0.5): """Return argument to the sigmoid that would give mean of covariance hid units @@ -453,7 +445,7 @@ sgd_ups = sgd_updates( rbm.params, grads, - lr=[2*s_lr, .2*s_lr, .02*s_lr, .1*s_lr, .02*s_lr ]) + stepsizes=[2*s_lr, .2*s_lr, .02*s_lr, .1*s_lr, .02*s_lr ]) learn_fn = function([batch_idx, s_lr, s_l1_penalty], outputs=[ grads[0].norm(2), diff -r 8ba8b08e0442 -r c6d08a760960 pylearn/gd/sgd.py --- a/pylearn/gd/sgd.py Tue Aug 24 16:51:53 2010 -0400 +++ b/pylearn/gd/sgd.py Tue Aug 24 17:01:09 2010 -0400 @@ -1,8 +1,27 @@ -"""A stochastic gradient descent minimizer. (Possibly the simplest minimizer.) +"""A stochastic gradient descent minimizer. """ import theano +def sgd_updates(params, grads, stepsizes): + """Return a list of (pairs) that can be used as updates in theano.function to implement + stochastic gradient descent. + + :param params: variables to adjust in order to minimize some cost + :type params: a list of variables (theano.function will require shared variables) + :param grads: the gradient on each param (with respect to some cost) + :type grads: list of theano expressions + :param stepsizes: step by this amount times the negative gradient on each iteration + :type stepsizes: [symbolic] scalar or list of one [symbolic] scalar per param + """ + try: + iter(stepsizes) + except: + stepsizes = [stepsizes for p in params] + updates = [(p, p - step * gp) for (step, p, gp) in zip(stepsizes, params, grads)] + return updates + + class StochasticGradientDescent(theano.Module): """Fixed stepsize gradient descent