我正在尝试实现一种算法,该算法要求向量乘以神经网络输出相对于其权重的粗糙度 . 我无法找到语法来到theano后端以成功执行计算 .

现在,如果需要的话,我愿意计算完整的Hessian,但是我更喜欢使用一个名为R-operator的优化,它使Hessian时间向量更有效 .

我(我想)知道我想要做什么不能在Keras本身完成,而且我发现Theano实现了R-operator(Tensorflow似乎没有我发现的那么多)所以我是试图访问Theano后端,提取输出张量和权重,并使用Theano的渐变函数来执行计算 .

我一直在指以下链接:Accessing gradient values of keras model outputs with respect to inputs

http://deeplearning.net/software/theano/tutorial/gradients.html

http://deeplearning.net/software/theano/tutorial/examples.html

http://deeplearning.net/software/theano/library/gradient.html

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

# 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时,我也有一些变化 . 我认为我的问题在理解如何在训练有素的神经网络上执行导数时更为基础 . 这个页面http://deeplearning.net/software/theano/tutorial/gradients.html非常有用,但它没有从其他来源(以及许多其他谷歌搜索)中找到它 .

如果有人能提供任何指示,我们将不胜感激 .