2017-05-11 1 views
0

난 추적 문제를 해결하는 매있어하십시오 theano 기능을 갖는 클래스 방법 복귀 후의 파라미터 갱신되는 내부 while 루프 이루어질 것으로 값 출력으로서 :왜 더 이상 업데이트하지 않습니까?

import theano 
import theano.tensor as T 
import numpy as np 
import copy 
theano.config.exception_verbosity = 'high' 

class Test(object): 
    def __init__(self): 
     self.rate=0.01 
     W_val=40.00 
     self.W=theano.shared(value=W_val, borrow=True) 
    def start(self, x, y): 
     for i in range(5): 
      z=T.mean(x*self.W/y) 
      gz=T.grad(z, self.W) 
      self.W-=self.rate*gz 
     return z 

x_set=np.array([1.,2.,1.,2.,1.,2.,1.,2.,1.,2.]) 
y_set=np.array([1,2,1,2,1,2,1,2,1,2]) 
x_set = theano.shared(x_set, borrow=True) 
y_set = theano.shared(y_set, borrow=True) 
y_set=T.cast(y_set, 'int32') 
batch_size=2 

x = T.dvector('x') 
y = T.ivector('y') 
index = T.lscalar() 

test = Test() 
cost=test.start(x,y) 

train = theano.function(
    inputs=[index], 
    outputs=cost, 
    givens={ 
     x: x_set[index * batch_size: (index + 1) * batch_size], 
     y: y_set[index * batch_size: (index + 1) * batch_size] 
    } 
) 

for i in range(5): 
    result=train(i) 
    print(result) 

는이 결과를 인쇄 중 :

39.96000000089407 
39.96000000089407 
39.96000000089407 
39.96000000089407 
39.96000000089407 

지금 평균 (X *에서의 W/Y)의 기울기가 (X 및 Y는 항상 같은 값을 갖기 때문에) (1)과 동일하다. 그래서 처음 39.95, 39.90 등등을 가져야합니다 ... 왜 나는 항상 같은 결과가 있습니까 ??

감사

+0

. 반복 할 때마다 z 변수를 덮어 씁니다. 이유가 될 수 있습니까? –

+0

@PepaKorbel : z를 덮어 써야하지만 W 값을 업데이트하므로 z 값을 변경해야합니다. 그렇습니까? –

+0

오, 너희는 내가 그 z 변수를 사용하여 가중치를 업데이트한다는 것을 두 번 확인했다. 그건 다른 곳에서해야만합니다 –

답변

0

나는 google groups에서 친구 파스칼의 도움으로 초래했습니다. theano 기능을

class Test(object): 
    def __init__(self): 
     self.rate=0.01 
     W_val=40.00 
     self.W=theano.shared(value=W_val, borrow=True) 
    def start(self, x, y): 
     new_W=self.W 
     for i in range(5): 
      z=T.mean(x*new_W/y) 
      gz=T.grad(z, new_W) 
      new_W-=self.rate*gz 
     return z, (self.W, new_W) 

수정 :이 솔루션은 다른 상징적 인 변수를 만드는 것입니다

test = Test() 
cost, updates=test.start(x,y) 

train = theano.function(
    inputs=[index], 
    outputs=cost, 
    updates=[updates], 
    givens={ 
     x: x_set[index * batch_size: (index + 1) * batch_size], 
     y: y_set[index * batch_size: (index + 1) * batch_size] 
    } 
) 

출력 : 당신의 시작 루프에서

39.96000000089407 
39.91000000201166 
39.860000003129244 
39.81000000424683 
39.76000000536442 
관련 문제