2016-11-02 4 views
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

答えて

0

だけ入力引数としてWに合格しない:

gradf = theano.function([x, target], TT.grad(...)) 

それはちょうどWの現在の値を使用します。 Wの他の値(現在の値とは異なる値)でグラデーションを計算したいのであれば、それは挑戦的ですが、望みのものとは違うようです。

+0

よろしくお願いいたします。別のものでは、Wsave = W.get_value(); W.set_value(anotherW); g = gradf(...); W.set_value(Wsave) '?畳み込まれているようだ。 – denis

関連する問題