计算Hessian(nnet w.r.t权重)乘以keras中无后端的矢量(使用r-op)

计算Hessian(nnet w.r.t权重)乘以keras中无后端的矢量(使用r-op),keras,theano,hessian,Keras,Theano,Hessian,我正在尝试实现一个算法,该算法需要一个向量乘以神经网络输出的hessian值相对于它的权重。我很难找到语法,无法到达theano后端以成功执行计算 现在,如果需要的话,我愿意计算完整的Hessian,但是我更喜欢使用一种叫做R算子的优化,它使Hessian乘以向量更有效 我(想我)知道我想做的事情在Keras本身是做不到的,我发现Theano实现了R算子(Tensorflow似乎没有我发现的那么远),所以我试图访问Theano后端,提取输出张量和权重,并使用Theano的梯度函数来执行计算 我指

我正在尝试实现一个算法,该算法需要一个向量乘以神经网络输出的hessian值相对于它的权重。我很难找到语法,无法到达theano后端以成功执行计算

现在,如果需要的话,我愿意计算完整的Hessian,但是我更喜欢使用一种叫做R算子的优化,它使Hessian乘以向量更有效

我(想我)知道我想做的事情在Keras本身是做不到的,我发现Theano实现了R算子(Tensorflow似乎没有我发现的那么远),所以我试图访问Theano后端,提取输出张量和权重,并使用Theano的梯度函数来执行计算

我指的是以下链接:

我尝试了一些变化,例如:

# Calculate the full hessian manually with keras.backend
outputTensor = theta_nnet.model.output 
listOfVariableTensors = theta_nnet.model.trainable_weights
gradients = k.gradients(outputTensor[0,0], listOfVariableTensors)
hessian   = k.gradients( gradients, listOfVariableTensors )
# Errors out with "AttributeError" because the cost function on the hessian gradients call has no attribute named type.
另一个例子:

# Calculate the full hessian manually by using theano grad 
outputTensor = theta_nnet.model.output 
listOfVariableTensors = theta_nnet.model.trainable_weights
theano_grad = T.grad(outputTensor[0,0], listOfVariableTensors)
theano_Hv = T.grad(T.sum(theano_grad * w), listOfVariableTensors)
# AsTensorError exception trying to convert w, a numpy ndarray, to a tensor
我还尝试使用T.hessian和T.rop的一些变体。我认为我的问题更重要的是理解如何在经过训练的神经网络上执行导数。这一页非常有用,但它没有演示如何将其应用于神经网络的最后一步,我无法从其他来源(以及许多其他谷歌搜索)中找到它


如果有人能提供任何建议,我们将不胜感激。

您找到解决方案了吗?