Traceback (most recent call last):
File "/home/axoren1/SmokingDataProject/Rotation Test.py", line 40, in <module>
dJ = T.grad((R(n, t) - R(n, angles)).norm(2), t)
File "/usr/local/lib/python2.7/dist-packages/theano/gradient.py", line 529, in grad
handle_disconnected(elem)
File "/usr/local/lib/python2.7/dist-packages/theano/gradient.py", line 516, 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: Theta
这是什么意思?下面是我的代码,解释了为什么我认为这个错误是空洞的。
import numpy as np
import theano
import theano.tensor as T
import theano.tensor.nlinalg as Tn
n = 5
angles = 2 * np.pi * np.random.rand(n, 1)
def R(n, angles):
sines = T.sin(angles)
cosines = T.cos(angles)
def r(i, j):
sign = -1 * -1 ** ((i + j) % 2)
c = cosines[i - 1] * cosines[j]
s = T.prod(sines[i:j])
return sign * c * s
R = T.zeros((n, n))
for i in range(n):
for j in range(i, n):
T.inc_subtensor(R[i:i+1][j:j+1], r(i, j))
for i in range(0, n - 1):
T.inc_subtensor(R[i+1:i+2][i:i+1], sines[i])
return R
guess = np.random.rand(n, 1)
t = T.vector("Theta")
for i in range(100):
J = (R(n, t) - R(n, angles)).norm(2)
dJ = T.grad((R(n, t) - R(n, angles)).norm(2), t)
guess -= dJ.eval({t:guess})
print J.eval({t:guess}), guess
如您所见,Theta
节点由成本函数定义和使用。我根本不知道函数R
是如何不连续的。为什么这会破裂?
答案 0 :(得分:0)
问题是您需要将inc_subtensor
来电的结果分配回R
。
而不是
T.inc_subtensor(R[i:i+1][j:j+1], r(i, j))
和
T.inc_subtensor(R[i+1:i+2][i:i+1], sines[i])
使用
R = T.inc_subtensor(R[i:i+1][j:j+1], r(i, j))
和
R = T.inc_subtensor(R[i+1:i+2][i:i+1], sines[i])
inc_subtensor
是一个符号操作,它返回一个对象,表示按提供的值递增提供的子指标的符号结果。