为什么thano不更新?

Why thano does not update?

我想解决以下问题:theano 函数的输出值是 class 方法 return 在进行 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

现在 mean(x*W/y) 的梯度等于 1(因为 x 和 y 始终具有相同的值)。所以第一次我应该有 39.95,而不是 39.90 等等...... 为什么我总是得到相同的结果??

谢谢

我在 google groups 的朋友 Pascal 的帮助下得出了结果。解决方案是创建一个其他符号变量:

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)

并修改theano函数:

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