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

查看:35
本文介绍了使用 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天全站免登陆