def sgd(lr, tparams, grads, inp, cost, extra_ups=[], extra_outs=[],
exclude_params=set([])):
'''Stochastic gradient descent'''
gshared = [theano.shared(p.get_value() * 0., name='%s_grad'%k)
for k, p in tparams.iteritems()]
gsup = [(gs, g) for gs, g in zip(gshared, grads)]
f_grad_shared = theano.function(
inp, [cost]+extra_outs, updates=gsup+extra_ups, profile=profile)
pup = [(p, p - lr * g) for p, g in zip(tools.itemlist(tparams), gshared)
if p.name not in exclude_params]
if not isinstance(lr, list): lr = [lr]
f_update = theano.function(lr, [], updates=pup, profile=profile)
return f_grad_shared, f_update
评论列表
文章目录