在函数中使用共享变量

Using a shared variable in a function

您好,我正在学习神经网络教程,作者似乎到处都在使用共享变量。根据我的理解,theanos 中的共享变量只是内存中的 space,可以由 gpu 和 cpu 堆共享。无论如何,我有两个矩阵,我声明为共享变量,我想使用函数对它们执行一些操作。 (问题 1)如果有人能解释为什么函数比常规 def 函数有用,我会很高兴。无论如何,我正在这样设置我的定义:

import theano
import theano.tensor as T
from theano import function
import numpy as np

class Transform:
    def __init__(self, dimg):
        dimg = dimg.astype(theano.config.floatX)
        self.in_t = theano.shared(dimg, name='dimg', borrow=True)

    def rotate(self, ox, oy, radians):
        value = np.zeros((2 * self.in_t.get_value().shape[0],
                          2 * self.in_t.get_value().shape[1]))
        out_t = theano.shared(value,
                              name='b',
                              dtype=theano.config.floatX),
                              borrow=True)    
        din = theano.tensor.dmatrix('a')
        dout = theano.tensor.dmatrix('b')

        def atest():
            y = x + y
            return y

        f = function(inputs=[],
                     givens={x: self.in_t,
                             y: self.out_t},
                     outputs=atest)    
        return f()

问题是我不知道如何在常规函数输出调用中使用共享变量。我知道我可以通过 function([],..update=(shared_var_1, upate_function)) 进行更新。但是如何在我的常规函数​​中访问它们?

这里是 Theano 初学者,所以我不确定我的回答是否涵盖所有技术方面。

回答你的第一个问题:你需要声明 theano 函数而不是 def 函数,因为 theano 就像 python 中的 "language" 并调用 theano.function 您正在编译一些特殊的 C 代码来执行您的任务。这就是 Theano 快速的原因。 来自 documentation:

It is good to think of theano.function as the interface to a compiler which builds a callable object from a purely symbolic graph. One of Theano’s most important features is that theano.function can optimize a graph and even compile some or all of it into native machine instructions.

关于你的第二个问题,为了访问存储在你的共享变量中的内容,你应该使用

shared_var.get_value()

检查 these 个示例:

The value can be accessed and modified by the .get_value() and .set_value() methods.

此代码:

a = np.array([[1,2],[3,4]], dtype=theano.config.floatX)
x = theano.shared(a)
print(x)

会输出

<CudaNdarrayType(float32, matrix)>

但使用 get_value():

print(x.get_value())

输出

[[ 1.  2.]
 [ 3.  4.]]

编辑: 在函数中使用共享变量

import theano
import numpy
a = numpy.int64(2)
y = theano.tensor.scalar('y',dtype='int64')
z = theano.tensor.scalar('z',dtype='int64')
x = theano.shared(a)
plus = y + z
theano_sum = theano.function([y,z],plus)
# Using shared variable in a function
print(theano_sum(x.get_value(),3))
# Changing shared variable value using a function
x.set_value(theano_sum(2,2))
print(x.get_value())
# Update shared variable value
x.set_value(x.get_value(borrow=True)+1)
print(x.get_value())

将输出:

5
4
5