首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法理解theano扫描适用于毕业生的行为

无法理解theano扫描适用于毕业生的行为
EN

Stack Overflow用户
提问于 2016-05-30 23:10:47
回答 1查看 54关注 0票数 0

所以事情是这样的:我必须在下面编写代码,当我应用grad wrt A时,一切都运行得很好,它正确地计算了梯度。但是,如果我使用wrt=i,那么它会给我一个DisconnectedInputError。为什么会这样,我该如何去区分i呢?

代码语言:javascript
复制
def step(i, A):
    return A*i, i

A = T.scalar("A")
outputs, _ = theano.scan(step, sequences=T.arange(2,6), non_sequences=A)
res, i = outputs
grad = T.grad(cost=res[3], wrt=A)
func = theano.function([A],[grad, res, i])

print func(3.0)

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    grad = T.grad(cost=res[3], wrt=i)
  File "/usr/local/lib/python2.7/dist-packages/theano/gradient.py", line 545, in grad
    handle_disconnected(elem)
  File "/usr/local/lib/python2.7/dist-packages/theano/gradient.py", line 532, in handle_disconnected
    raise DisconnectedInputError(message)
theano.gradient.DisconnectedInputError: grad method was asked to compute the gradient with respect to a variable that is not part of the computational graph of the cost, or is used only by a non-differentiable operator: for{cpu,scan_fn}.1
Backtrace when the node is created:
  File "test.py", line 15, in <module>
    outputs, _ = theano.scan(step, sequences=T.arange(2,6), non_sequences=A)
EN

回答 1

Stack Overflow用户

发布于 2016-05-31 22:18:06

为了区分wrt和I,您需要在扫描操作之前声明整个I数组,并使用整个数组来计算它的梯度wrt。

代码语言:javascript
复制
i_array = T.arange(2,6)

def step(i, A):
    return A*i, i

A = T.scalar("A")
A.tag.test_value = 5.0

outputs, _ = theano.scan(step, sequences=i_array, non_sequences=A)
res, i = outputs
grad = T.grad(cost=res[3], wrt=i_array)
func = theano.function([A],[grad, res, i])

print func(3.0)

如果需要i的特定元素,则应在计算i_array的grad后使用索引选择该元素

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37528925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档