Keras: calculating derivatives of model output wrt input returns [None]

我与影子孤独终老i 提交于 2019-12-03 20:58:34

You are computing the gradients respect to X_train, which is not an input variable to the computation graph. Instead you need to get the symbolic input tensor to the model, so try something like:

grads = K.gradients(model.output, model.input)

In order to calculate the gradients, you need to first figure out the trainable variables. Here is how you do it:

outputs = model.output
trainable_variables = model.trainable_weights

Now calculate the gradients as:

gradients = K.gradients(outputs, trainable_variables)

As a side note, the gradients are a part of your computational graph, the execution of which depends on your backend. If you are using tf, you might require to initialize a session and pass the gradients variable to the session for its evaluation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!