首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在下面的Theano方法中,更新params的方式是否有错误?

在下面的Theano方法中,更新params的方式是否有错误?
EN

Stack Overflow用户
提问于 2015-10-13 12:46:46
回答 1查看 179关注 0票数 1

我在网上阅读了一个基于动量的学习教程,并在Theano上看到了这个方法。

代码语言:javascript
复制
def gradient_updates_momentum(cost, params, learning_rate, momentum):
    '''
Compute updates for gradient descent with momentum

:parameters:
    - cost : theano.tensor.var.TensorVariable
        Theano cost function to minimize
    - params : list of theano.tensor.var.TensorVariable
        Parameters to compute gradient against
    - learning_rate : float
        Gradient descent learning rate
    - momentum : float
        Momentum parameter, should be at least 0 (standard gradient descent) and less than 1

:returns:
    updates : list
        List of updates, one for each parameter
'''
# Make sure momentum is a sane value
assert momentum < 1 and momentum >= 0
# List of update steps for each parameter
updates = []
# Just gradient descent on cost
for param in params:
    # For each parameter, we'll create a param_update shared variable.
    # This variable will keep track of the parameter's update step across iterations.
    # We initialize it to 0
    param_update = theano.shared(param.get_value()*0., broadcastable=param.broadcastable)
    # Each parameter is updated by taking a step in the direction of the gradient.
    # However, we also "mix in" the previous step according to the given momentum value.
    # Note that when updating param_update, we are using its old value and also the new gradient step.
    updates.append((param, param - learning_rate*param_update))
    # Note that we don't need to derive backpropagation to compute updates - just use T.grad!
    updates.append((param_update, momentum*param_update + (1. - momentum)*T.grad(cost, param)))
return updates

下面两行的顺序不应该相反(互换)吗?

代码语言:javascript
复制
updates.append((param, param - learning_rate*param_update))

代码语言:javascript
复制
updates.append((param_update, momentum*param_update + (1. - momentum)*T.grad(cost, param)))

据我所知,在执行了列车方法并计算了成本后,才会运行更新,对吗?

这不意味着我们应该使用当前成本,对于现有的param_update值(来自上一次迭代),我们应该计算较新的param_update,从而更新当前的param值吗?

为什么它是相反的,为什么这是正确的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-13 13:05:16

提供给theano.function的更新列表中的更新顺序将被忽略。更新总是使用共享变量的旧值来计算。

这个代码片段显示更新的顺序被忽略了:

代码语言:javascript
复制
import theano
import theano.tensor

p = 0.5
param = theano.shared(1.)
param_update = theano.shared(2.)
cost = 3 * param * param
update_a = (param, param - param_update)
update_b = (param_update, p * param_update + (1 - p) * theano.grad(cost, param))
updates1 = [update_a, update_b]
updates2 = [update_b, update_a]
f1 = theano.function([], outputs=[param, param_update], updates=updates1)
f2 = theano.function([], outputs=[param, param_update], updates=updates2)
print f1(), f1()
param.set_value(1)
param_update.set_value(2)
print f2(), f2()

如果,逻辑上,你想

代码语言:javascript
复制
new_a = old_a + a_update
new_b = new_a + b_update

然后,您需要提供这样的更新:

代码语言:javascript
复制
new_a = old_a + a_update
new_b = old_a + a_update + b_update
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33103111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档