无法理解应用于grad behaviou的无扫描

2024-10-05 13:19:20 发布

您现在位置:Python中文网/ 问答频道 /正文

所以这里有一件事:我必须在下面编写代码,当我应用grad wrt时,一切都运行良好,它正确地计算了梯度。但是,如果我这样做wrt=i,那么它会给我一个DisconnectedInputError。为什么会这样,我如何才能区分我?你知道吗

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)

Tags: theinpyscansteplinerestheano
1条回答
网友
1楼 · 发布于 2024-10-05 13:19:20

要区分wrt到i,您需要在扫描操作之前声明整个i数组,并使用整个数组计算wrt到它的梯度。你知道吗

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的梯度之后使用索引来选择它

相关问题 更多 >

    热门问题