I have CNN models trained using Keras with Tensorflow backend. And I want to visualize my CNN filters with this tutorial: https://blog.keras.io/how-convolutional-neural-networks
If you have a Model instance, then to take the gradient of the loss with respect to the input, you should do:
grads = K.gradients(loss, model.input)[0]
model.input
contains the symbolic tensor that represents the input to the model. Using a plain numpy array makes no sense because TensorFlow then has no idea how this connects to the computational graph, and returns None as the gradient.
Then you should also rewrite the iterate
function as:
iterate = K.function([model.input], [loss, grads])
Below, it's my example. Hope to help someone.
gradient = keras.backend.gradients(model.output, model.input)[2]
iterate = keras.backend.function(model.input, [gradient])
grad = iterate([patches, depthes, poses])
[patches, depthes, poses] is my model.input
I too had faced the same error @Jexus. For me the problem was:
loss variable was None object. I had used
loss.assign_add(....)
instead of
loss = loss + .....
After changing that as mentioned, the loss was returning a tensor and hence
grads = K.gradients(loss, model.input)[0]
wasn't returning None.