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

后端 未结 3 472
猫巷女王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])
    
    0 讨论(0)
  • 2021-02-03 11:50

    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

    0 讨论(0)
  • 2021-02-03 11:56

    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.

    0 讨论(0)
提交回复
热议问题