使用Keras获取w.r.t权重的模型输出的梯度 [英] Getting gradient of model output w.r.t weights using Keras

查看:1006
本文介绍了使用Keras获取w.r.t权重的模型输出的梯度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用Keras API的简单性来构建强化学习模型感兴趣.不幸的是,我无法提取相对于权重的输出梯度(不是误差).我发现以下代码执行类似的功能(神经网络的显着性映射(使用Keras))

I am interested in building reinforcement learning models with the simplicity of the Keras API. Unfortunately, I am unable to extract the gradient of the output (not error) with respect to the weights. I found the following code that performs a similar function (Saliency maps of neural networks (using Keras))

get_output = theano.function([model.layers[0].input],model.layers[-1].output,allow_input_downcast=True)
fx = theano.function([model.layers[0].input] ,T.jacobian(model.layers[-1].output.flatten(),model.layers[0].input), allow_input_downcast=True)
grad = fx([trainingData])

任何关于如何计算模型输出相对于每一层权重的梯度的想法都会受到赞赏.

Any ideas on how to calculate the gradient of the model output with respect to the weights for each layer would be appreciated.

推荐答案

要使用Keras获取相对于权重的模型输出梯度,您必须使用Keras后端模块.我创建了这个简单的示例来确切说明该怎么做:

To get the gradients of model output with respect to weights using Keras you have to use the Keras backend module. I created this simple example to illustrate exactly what to do:

from keras.models import Sequential
from keras.layers import Dense, Activation
from keras import backend as k


model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

要计算梯度,我们首先需要找到输出张量.对于模型的输出(我最初的问题是什么),我们仅将其称为model.output.我们还可以通过调用model.layers [index] .output

To calculate the gradients we first need to find the output tensor. For the output of the model (what my initial question asked) we simply call model.output. We can also find the gradients of outputs for other layers by calling model.layers[index].output

outputTensor = model.output #Or model.layers[index].output

然后,我们需要选择与渐变有关的变量.

Then we need to choose the variables that are in respect to the gradient.

  listOfVariableTensors = model.trainable_weights
  #or variableTensors = model.trainable_weights[0]

我们现在可以计算梯度.它很简单,如下所示:

We can now calculate the gradients. It is as easy as the following:

gradients = k.gradients(outputTensor, listOfVariableTensors)

要实际运行给定输入的梯度,我们需要使用一些Tensorflow.

To actually run the gradients given an input, we need to use a bit of Tensorflow.

trainingExample = np.random.random((1,8))
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
evaluated_gradients = sess.run(gradients,feed_dict={model.input:trainingExample})

就这样!

这篇关于使用Keras获取w.r.t权重的模型输出的梯度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆