【问题标题】:I want to understand theano function(given,updates)我想了解 theano 函数(给定,更新)
【发布时间】:2015-10-20 14:14:29
【问题描述】:

我想问几个关于theano函数的问题。

1.我看到没有分配输入变量的脚本。如果是这样,它是如何工作的?

import theano.tensor as T
import theano
# Define symbolic variables
X = T.matrix('X')
w = theano.shared([0.1, 0.1], name='w')
t = T.vector('t')
# Define Loss Expression
L = (t-X*w)**2
# Calculate Gradient Expression
dLdw = T.grad(L, w)
# Compile the training function
lr = 0.1
data_X = theano.shared([[0.1, 0.2], [0.2, 0.3], [0.1, 0.4], [0.2, 0.4]])
data_t = theano.shared([3, 3.5, 4, 4.2])
calc_output = theano.function([], L, 
    updates=[(w, w - lr*dLdw)], givens=[(X,data_X), (t,data_t)] )
for epoch in xrange(100):
calc_output()

如上所示,输入方括号为空。那么这种情况下的输入是什么?

2.对于函数中的'given'参数,有点难以理解。人们说这是为了提升 GPU 进程,但我想知道应该为“给定”分配哪些变量。 请看下面的脚本。

index = T.scalar('index')
test_model = theano.function(inputs=[index],
outputs=classifier.errors(y),
    givens={
        x: test_set_x[index * batch_size: (index + 1) * batch_size],
        y: test_set_y[index * batch_size: (index + 1) * batch_size]})

validate_model = theano.function(inputs=[index],
    outputs=classifier.errors(y),
    givens={
        x: valid_set_x[index * batch_size:(index + 1) * batch_size],
        y: valid_set_y[index * batch_size:(index + 1) * batch_size]})

在给定的情况下,x 和 y 做什么?冒号(:) 是什么意思? 据我所知,(theano主页说:给定(可迭代变量对(Var1,Var2)。列表,元组或字典。每对中的Var1和Var2必须具有相同的类型。) - 要进行的特定替换计算图(Var2 替换 Var1)。)它需要 2 个变量,但在第一个示例中似乎有 4 个变量,而第二个对我来说非常复杂。谁能告诉我给定的具体细节是什么?并解释给定变量中的第二个脚本发生了什么。

此外,在主页上,它说“您可以使用函数的给定参数来替换图中的特定节点以实现特定功能。”我没有得到它替换的图中的哪个特定节点。

请帮帮我!!

【问题讨论】:

    标签: python function theano


    【解决方案1】:

    首先,给定的意思是 T 变量的实际值是多少。如果在给定中有 (x,a) 并且 a 是一个 np.array,那么它将在计算时使用 a 来替换 x,如果有 x:a 在给定中也是一样的,这意味着同样的事情。 在第一个例子中,data_X 是输入,只是没有在第一个参数中给出,而是在 givens 中,这是相同的。 在第二个例子中, test_set_x 是 theano.shared ,这意味着它的值已经存在,它是一个矩阵。但是您将使用它的哪一部分,您将通过索引选择输入。

    【讨论】:

    • 感谢您的评论,这真的很有帮助,请检查我是否理解您所说的。在第一个示例中,将输入给定的 data_x 并将 X 替换为其输出(data_x 通过 'L' 方程:(t-X*w)**2),然后将 x 替换为其输出)并且此替换也发生在 data_t,因为 L 需要 data_x 和 data_t。
    • 在第二个例子中,有两个冒号。在第一个给定的 'x: valid_set_x[index * batch_size:(index + 1) * batch_size' 中,这是 (valid_set_x[index * batch_size:(index + 1) * batch_size) 输入和 'x' 被替换了吗?这很令人困惑,因为输入已经分配:input=['index']。我不明白这个 'index * batch_size: (index + 1) * batch_size' 部分的作用......假设 batch_sizeis 是 100 那么它的意思是 'test_set_x[100:200 ]' 所以在它结束后将这个 test_set_x 替换为 x通过输出:classifier.errors?
    • 我还看到只有 1 个像 given=[x,a] 这样的集合变量,这意味着任意数量的集合(3 个或更多集合,例如 given=[x,a],[y, b],[z,c]) 可能吗?
    • 在第二个例子中,当计算正在执行时,x 将被替换为 valid_set_x[index * batch_size:(index + 1) * batch_size]。如果给出包括 x:valid_set_x[index * batch_size:(index + 1) * batch_size]。但确切的索引是什么?是 0 还是 100?该索引是您的输入。我没见过“given=[x,a],[y,b],[z,c]”,我见过两种表达方式,包括givens = {x:X,y:Y}或givens = { (x,X),(y,Y)} 是等价的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多