python grad_如何用python编写adagrad

我也在寻找同样的东西,最终以zuz已经指出的资源风格实现了它。所以也许这对在这里寻求帮助的人有帮助。在def adagrad(lr, tparams, grads, inp, cost):

# stores the current grads

gshared = [theano.shared(np.zeros_like(p.get_value(),

dtype=theano.config.floatX),

name='%s_grad' % k)

for k, p in tparams.iteritems()]

grads_updates = zip(gshared, grads)

# stores the sum of all grads squared

hist_gshared = [theano.shared(np.zeros_like(p.get_value(),

dtype=theano.config.floatX),

name='%s_grad' % k)

for k, p in tparams.iteritems()]

rgrads_updates = [(rg, rg + T.sqr(g)) for rg, g in zip(hist_gshared, grads)]

# calculate cost and store grads

f_grad_shared = theano.function(inp, cost,

updates=grads_updates + rgrads_updates,

on_unused_input='ignore')

# apply actual update with the initial learning rate lr

n = 1e-6

updates = [(p, p - (lr/(T.sqrt(rg) + n))*g)

for p, g, rg in zip(tparams.values(), gshared, hist_gshared)]

f_update = theano.function([lr], [], updates=updates, on_unused_input='ignore')

return f_grad_shared, f_update

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值