我需要通过扫描多次执行theano函数,以便总结成本函数并在梯度计算中使用它.我熟悉这方面的深度学习教程,但是我的数据切片和其他一些复杂性意味着我需要做一些不同的事情.下面是我正在尝试做的简化版本..
tn = testnet()
cost = tn.single_cost( )
x = theano.shared(numpy.asarray([7.1,2.2,3.4], dtype='float32'))
index = T.lscalar('index')
test_fn = theano.function(inputs=[index], outputs=cost,
givens={tn.x:x[index:index+1]} )
def step(curr):
return T.constant( test_fn( curr ) )
outs,_ = theano.scan(step, T.arange(2))
out_fn = theano.function(inputs=[], outputs=outs)
print out_fn()
Run Code Online (Sandbox Code Playgroud)
在scan函数中,对test_fn(curr)的调用给出了错误...期望一个类似于数组的对象,但是找到了一个变量:也许你试图在一个(可能是共享的)变量而不是数字上调用一个函数阵列?')
即使我传入一个值数组而不是将T.arrange(2)放到位,我仍然会得到相同的错误.有没有理由你不能通过扫描调用函数?
一般来说,我想知道是否有办法用一系列索引调用这样的函数,以便输出可以输入T.grad()计算(未显示).
不要让两个不同theano.functions
。
Atheano.function
采用符号关系,对其进行优化并编译。您在这里所做的是要求theano.scan
(并因此out_fn
)将编译函数视为符号关系。我不确定你是否可以在技术上让它发挥作用,但这违背了 Theano 的想法。
因为我不知道你的成本函数在这里做什么,所以我无法给出一个确切的例子,但这里有一个简单的例子,它确实有效,并且应该与我认为你想要做的事情足够相似。
x = theano.shared(np.asarray([7.1,2.2,3.4], dtype = np.float32))
v = T.vector("v")
def fv(v):
res,_ = theano.scan(lambda x: x ** 2, v)
return T.sum(res)
def f(i):
return fv(x[i:i+2])
outs,_ = theano.scan(
f,
T.arange(2)
)
fn = theano.function(
[],
outs,
)
fn()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
943 次 |
最近记录: |