我现在正在学习Theano库,我对Theano共享变量感到困惑。通过阅读教程,我想我不明白它的详细含义。以下是教程中Theano共享变量的定义:
“存储的变量,它在它出现的函数之间共享。这些变量应由注册的共享构造函数创建。”
另外,我想知道Theano共享变量是否可以是python类数据成员。例如:
class A(object):
data = None
...
可以将“数据”作为Theano共享变量进行初始化吗?如果有人能帮助我,我真的很感激。
答案 0 :(得分:14)
Theano shared variables behave more like ordinary Python variables. They have an explicit value that is persistent. In contrast, symbolic variables are not given an explicit value until one is assigned on the execution of a compiled Theano function.
Symbolic variables can be thought of as representing state for the duration of a single execution. Shared variables on the other hand represent state that remains in memory for the lifetime of the Python reference (often similar to the lifetime of the program).
Shared variables are usually used to store/represent neural network weights because we want these values to remain around across many executions of a Theano training or testing function. Often, the purpose of a Theano training function is to update the weights stored in a shared variable. And a testing function needs the current weights to perform the network's forward pass.
As far as Python is concerned Theano variables (shared or symbolic) are just objects -- instances of classes defined within the Theano library. So, yes, references to shared variables can be stored in your own classes, just like any other Python object.
答案 1 :(得分:4)
共享变量有助于简化对预定义变量的操作。 @ danien-renshaw的答案的一个例子,假设我们想要添加两个矩阵,让我们说a和b,其中b矩阵的值在程序的整个生命周期内保持不变,我们可以将b矩阵作为共享变量并执行所需的操作。
不使用共享变量的代码:
a = theano.tensor.matrix('a')
b = theano.tensor.matrix('b')
c = a + b
f = theano.function(inputs = [a, b], outputs = [c])
output = f([[1, 2, 3, 4]], [[5, 5, 6, 7]])
使用共享变量的代码:
a = theano.tensor.matrix('a')
b = theano.tensor.shared( numpy.array([[5, 6, 7, 8]]))
c = a + b
f = theano.function(inputs = [a], outputs = [c])
output = f([[1, 2, 3, 4]])