如何获得theano共享变量的渐变值?即,
如何制作theano.function( outputs=TT.grad( shared vars ))
?
参加最小训练样例 Marek Rei's theano tutorial:
import theano
import theano.tensor as TT
import numpy as np
floatx = theano.config.floatX
#...............................................................................
x = TT.fvector('x')
target = TT.fscalar('target')
W = theano.shared(np.asarray([0.2, 0.7]), 'W') # state
y = (x * W).sum()
cost = TT.sqr(target - y)
gradients = TT.grad(cost, [W])
W_updated = W - (0.1 * gradients[0])
updates = [(W, W_updated)]
f = theano.function([x, target], y, updates=updates)
x0 = np.array( [1.0, 1.0] ).astype(floatx)
target0 = 20.0
for i in xrange(10):
output = f( x0, target0 )
Wval = W.get_value().astype(floatx)
grad = gradf( x0, Wval, target0 )[0] # <--- how to define gradf ?
print "f %-8.3g W %s grad %s" % (
output, Wval, grad )
>>>
f 0.9 W [4.02 4.52] grad [-22.9 -22.9]
f 8.54 W [6.31 6.81] grad [-13.8 -13.8]
...
一个人不能直接
gradf = theano.function( [x, W, target], TT.grad(...) )
因为theano.function
说
输入:Variable或In实例的列表。 函数参数,这些不允许是共享变量。
一个可以制作整个符号图的副本
gradients = TT.grad(cost, [W])
带输入变量,不共享;一定是更好的方式,
也许是givens=
?
相关:
[Theano]How to evaluate gradient based on shared variables
答案 0 :(得分:0)
只是不要将W
作为输入参数传递:
gradf = theano.function( [x, target], TT.grad(...) )
它只会使用W
的当前值。
如果你想让它计算W
的某个其他值的渐变而不是它的当前值,则更具挑战性,但看起来它并不是你想要的。