Python 使用 Keras 获取模型输出 wrt 权重的梯度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39561560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting gradient of model output w.r.t weights using Keras
提问by Matt S
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))
我对使用 Keras API 的简单性构建强化学习模型很感兴趣。不幸的是,我无法提取关于权重的输出梯度(不是错误)。我发现以下代码执行类似的功能(神经网络的显着图(使用 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.
任何关于如何计算模型输出相对于每一层权重的梯度的想法都将不胜感激。
回答by Matt S
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:
要使用 Keras 获得模型输出相对于权重的梯度,您必须使用 Keras 后端模块。我创建了这个简单的例子来确切说明要做什么:
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'])
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
要计算梯度,我们首先需要找到输出张量。对于模型的输出(我最初提出的问题),我们只需调用 model.output。我们还可以通过调用 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)
To actually run the gradients given an input, we need to use a bit of Tensorflow.
要在给定输入的情况下实际运行梯度,我们需要使用一些 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})
And thats it!
就是这样!
回答by PRIYA JINDAL
The below answer is with the cross entropy function, feel free to change it your function.
以下答案与交叉熵函数有关,请随意更改您的函数。
outputTensor = model.output
listOfVariableTensors = model.trainable_weights
bce = keras.losses.BinaryCrossentropy()
loss = bce(outputTensor, labels)
gradients = k.gradients(loss, listOfVariableTensors)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
evaluated_gradients = sess.run(gradients,feed_dict={model.input:training_data1})
print(evaluated_gradients)