K.gradients(loss, input_img)[0] return “None”. (Keras CNN visualization with tensorflow backend)

后端 未结 3 473
猫巷女王i
猫巷女王i 2021-02-03 11:28

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

3条回答
  •  一向
    一向 (楼主)
    2021-02-03 11:50

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

提交回复
热议问题