tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.

  • Post author:
  • Post category:其他


在学习《Deep Learning with Python》第五章深度学习用于计算机视觉

5.4.3可视化类激活的热力图

在Tensorflow2.0环境中运行一下代码时

grads = K.gradients(african_elephant_output, last_conv_layer.output)[0]

替换成

grads = tf.keras.backend.gradients(african_elephant_output, last_conv_layer.output)[0]

仍然会出现如下错误

tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.

解决方案

with tf.GradientTape() as gtape:
    grads = gtape.gradient(african_elephant_output, last_conv_layer.output)


完整代码参考

参考:

https://stackoverflow.com/questions/58322147/how-to-generate-cnn-heatmaps-using-built-in-keras-in-tf2-0-tf-keras



版权声明:本文为Forrest97原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。