Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 符号变量会自动更新到序号中_Python_Logistic Regression_Theano - Fatal编程技术网

Python 符号变量会自动更新到序号中

Python 符号变量会自动更新到序号中,python,logistic-regression,theano,Python,Logistic Regression,Theano,我正在学习theano关于简单随机梯度下降的教程。然而,在这里,我无法理解在这个块中,给定的p_y_x和y_pred的值是如何根据W和b的值自动更新的,因为后来当我们运行test_logistic()时,我们只更新W和b的值? 谢谢 p_y_给定_x和y_pred是符号变量(仅来自Theano的python对象)。那些指向Theano对象的python变量不会得到更新。它们只是表示我们要进行的计算。像在伪代码中那样思考 它们将在编译Theano函数时使用。只有到那时,才会计算出该值。但这不会导致

我正在学习theano关于简单随机梯度下降的教程。然而,在这里,我无法理解在这个块中,给定的
p_y_x
y_pred
的值是如何根据
W
b
的值自动更新的,因为后来当我们运行test_logistic()时,我们只更新
W
b
的值? 谢谢


p_y_给定_x
y_pred
是符号变量(仅来自Theano的python对象)。那些指向Theano对象的python变量不会得到更新。它们只是表示我们要进行的计算。像在伪代码中那样思考

它们将在编译Theano函数时使用。只有到那时,才会计算出该值。但这不会导致指向对象
p_y_给定_x
y_pred
的python变量发生任何更改。对象不会更改


对一些人来说,理解这一区别需要时间。这是一种新的思维方式。所以,不要犹豫问问题。有一件事很有帮助,那就是经常问问自己,你是在符号世界还是在数字世界。数值世界只有编译过的Theano函数才能出现。

我仍然有点困惑,同意它都是在伪代码方面工作的,但是更新共享变量self.W和self.b如何自动导致由这些共享变量派生的符号变量表示的值的更新?Theano对此保持跟踪。调用ano.function()时,我们只保留对值所在的“容器”的引用。“容器”只是Theano中的内部内容,您不需要知道(只是一个包装值的普通python类)。它只允许我们让编译后的函数和共享变量访问相同的底层数据。函数和共享变量引用了同一个容器。所以两者在任何时候都看到相同的值。谢谢!因此,我得出结论,使用共享变量计算的每个张量变量都会在相应的共享变量值更新后自动更新。
class LogisticRegression(object):

    def __init__(self, input, n_in, n_out):
        self.W = theano.shared(
            value=numpy.zeros(
                (n_in, n_out),
                dtype=theano.config.floatX
            ),
            name='W',
            borrow=True
        )
        # initialize the baises b as a vector of n_out 0s
        self.b = theano.shared(
            value=numpy.zeros(
                (n_out,),
                dtype=theano.config.floatX
            ),
            name='b',
            borrow=True
        )
        self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)
        self.y_pred = T.argmax(self.p_y_given_x, axis=1)
        self.params = [self.W, self.b]

    def negative_log_likelihood(self, y):
        return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y])
        # end-snippet-2

    def errors(self, y):
        if y.ndim != self.y_pred.ndim:
            raise TypeError(
                'y should have the same shape as self.y_pred',
                ('y', y.type, 'y_pred', self.y_pred.type)
            )
        # check if y is of the correct datatype
        if y.dtype.startswith('int'):
            # the T.neq operator returns a vector of 0s and 1s, where 1
            # represents a mistake in prediction
            return T.mean(T.neq(self.y_pred, y))
        else:
            raise NotImplementedError()