0
theano 공유 변수의 그라디언트 값을 어떻게 얻을 수 있습니까? 즉,
어떻게 theano.function(outputs=TT.grad(shared vars))
을 만드시겠습니까? theano 공유 변수의 그라디언트를 모니터링하는 방법
입력을 말한다
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
감사합니다. anotherW에서 그라디언트의 경우 Wsave = W.get_value(); W.set_value (anotherW); g = gradf (...); W.set_value (Wsave)'? 뒤얽힌 것 같다. – denis