将Keras模型wrt的导数作为输入将返回全零 [英] Taking derivative of Keras model wrt to inputs is returning all zeros
本文介绍了将Keras模型wrt的导数作为输入将返回全零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个Keras模型.我想将模型wrt的梯度引入其输入.这就是我要做的
So I have a Keras model. I want to take the gradient of the model wrt to its inputs. Here's what I do
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from keras import backend as K
num_features = 5
model = Sequential()
model.add(Dense(60, input_shape=(num_features,), activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1, activation='softmax'))
model.compile(optimizer='adam', loss='binary_crossentropy')
#Run predict to initialize weights
model.predict(np.random.rand(1, num_features))
x = tf.random_uniform(shape=(1, num_features))
model_grad = tf.gradients(model(x), x)[0]
但是,当我打印出dmodel_dx的值时,我得到的全为0.
However when I print out the value of dmodel_dx I get all 0's.
sess = K.get_session()
print( model_grad.eval(session=sess) )
>>>array([[ 0., 0., 0., 0., 0.]], dtype=float32)
有人知道我在做什么错吗?
Anyone know what I'm doing wrong?
推荐答案
检查softmax是否饱和,从而为您提供非常小的渐变-试试
Check if the softmax is saturated and therefore giving you very small gradients--try
model_grad = K.gradients(K.dot(model.layers[-1].input,model.layers[-1].kernel)+model.layers[-1].bias, model.input)
这篇关于将Keras模型wrt的导数作为输入将返回全零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文