0
theano共有変数のグラジエントの値を取得するにはどうすればよいですか?つまり、
どうすればtheano.function(outputs=TT.grad(shared vars))
になりますか?theano共有変数のグラディエントを監視する方法
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
が
入力言うので:変数やインスタンスのいずれかのリストを。 関数のパラメータ。これらは共有変数ではありません。
一つは全体シンボリックグラフのコピーを作ることができ
入力変数とgradients = TT.grad(cost, [W])
、共有されません。より良い方法である必要があります おそらくgivens=
と?
関連:
[Theano]How to evaluate gradient based on shared variables
よろしくお願いいたします。別のものでは、Wsave = W.get_value(); W.set_value(anotherW); g = gradf(...); W.set_value(Wsave) '?畳み込まれているようだ。 – denis