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])