在theano中,为什么明明是连接的我却得到这个theano.gradient.DisconnectedInputError?
In theano, why am I getting this theano.gradient.DisconnectedInputError when it's clearly connected?
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
是如何不连续的。为什么会这样?
问题是您需要将 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
是一个符号操作,returns 一个对象,表示将提供的子张量递增提供的值的符号结果。
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
是如何不连续的。为什么会这样?
问题是您需要将 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
是一个符号操作,returns 一个对象,表示将提供的子张量递增提供的值的符号结果。