我正在尝试使用Theano来计算关于向量以及几个标量的函数的粗糙度(编辑:也就是说,我基本上希望附加到我正在计算粗麻布的向量的标量) .这是一个最小的例子:
import theano import theano.tensor as T A = T.vector('A') b,c = T.scalars('b','c') y = T.sum(A)*b*c
我的第一次尝试是:
hy = T.hessian(y,[A,b,c])
哪个失败了 AssertionError: tensor.hessian expects a (list of) 1 dimensional variable as 'wrt'
我的第二次尝试是将A,b和c与:
wrt = T.concatenate([A,T.stack(b,c)]) hy = T.hessian(y,[wrt])
哪个失败了 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: Join.0
在这种情况下计算粗麻线的正确方法是什么?
更新:为了澄清我在寻找什么,假设A是2元素向量.然后黑森州将是:
[[d2y/d2A1, d2y/dA1dA2, d2y/dA1dB, d2y/dA1dC], [d2y/dA2dA1, d2y/d2A2, d2y/dA2dB, d2y/dA2dC], [d2y/dBdA1, d2y/dBdA2, d2y/d2B, d2y/dABdC], [d2y/dCdA1, d2y/dCdA2, d2y/dCdB, d2y/d2C]]
对于示例函数y
应该是:
[[0, 0, C, B], [0, 0, C, B], [C, C, 0, A1+A2], [B, B, A1+A2, 0]]
所以,如果我们要定义一个函数:
f = theano.function([A,b,c], hy)
那么,假设我们可以hy
成功计算,我们会期望输出:
f([1,1], 4, 5) = [[0, 0, 5, 4], [0, 0, 5, 4], [5, 5, 0, 2], [4, 4, 2, 0]]
在我的实际应用中,A有25个元素y
,而且更复杂,但想法是一样的.