RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead
RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead
您好,我正在尝试重现此 link 的结果
Keras CNN Chexpert
我在 运行 这行代码
时遇到了这个错误 RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead
grads = K.gradients(class_output, last_conv_layer.output)[0]
然而当我改成这行代码时
grads = tf.GradientTape(class_output, last_conv_layer.output)[0]
我得到这个错误“----> 2 grads = tf.GradientTape(class_output, last_conv_layer.output)[0]
类型错误:'GradientTape' 对象不可订阅。
如果有人能指导我更正此问题,我将不胜感激。
下面是代码片段
# get the first image of the testing dataset
x = test_generator[0][0]
preds = model.predict(x)
preds = y_pred_keras[1,:]
class_idx = np.argmax(preds)
class_output = model.output[:, class_idx]
#import the last convolutional layer of the model, this depends on the model
last_conv_layer = model.get_layer("conv5_block16_concat")
# grads = K.gradients(class_output, last_conv_layer.output)[0]
grads = tf.GradientTape(class_output, last_conv_layer.output)[0]
pooled_grads = K.mean(grads, axis=(0, 1, 2))
iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])
pooled_grads_value, conv_layer_output_value = iterate([x])
for i in range(1024):
conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
因此,我设法通过这两个链接找到了解决问题的指南,并为 gradCAM 实施了 tensorflow v2 代码。希望对遇到同样问题的人有所帮助。
您好,我正在尝试重现此 link 的结果 Keras CNN Chexpert
我在 运行 这行代码
时遇到了这个错误RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead
grads = K.gradients(class_output, last_conv_layer.output)[0]
然而当我改成这行代码时
grads = tf.GradientTape(class_output, last_conv_layer.output)[0]
我得到这个错误“----> 2 grads = tf.GradientTape(class_output, last_conv_layer.output)[0] 类型错误:'GradientTape' 对象不可订阅。
如果有人能指导我更正此问题,我将不胜感激。
下面是代码片段
# get the first image of the testing dataset
x = test_generator[0][0]
preds = model.predict(x)
preds = y_pred_keras[1,:]
class_idx = np.argmax(preds)
class_output = model.output[:, class_idx]
#import the last convolutional layer of the model, this depends on the model
last_conv_layer = model.get_layer("conv5_block16_concat")
# grads = K.gradients(class_output, last_conv_layer.output)[0]
grads = tf.GradientTape(class_output, last_conv_layer.output)[0]
pooled_grads = K.mean(grads, axis=(0, 1, 2))
iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])
pooled_grads_value, conv_layer_output_value = iterate([x])
for i in range(1024):
conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
因此,我设法通过这两个链接找到了解决问题的指南,并为 gradCAM 实施了 tensorflow v2 代码。希望对遇到同样问题的人有所帮助。