【发布时间】: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 个变量,而第二个对我来说非常复杂。谁能告诉我给定的具体细节是什么?并解释给定变量中的第二个脚本发生了什么。
此外,在主页上,它说“您可以使用函数的给定参数来替换图中的特定节点以实现特定功能。”我没有得到它替换的图中的哪个特定节点。
请帮帮我!!
【问题讨论】: