简体   繁体   English

启用急切执行时不支持 tf.gradients。 改用 tf.GradientTape

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

from tensorflow.keras.applications import VGG16
from tensorflow.keras import backend as K

model = VGG16(weights='imagenet',
              include_top=False)

layer_name = 'block3_conv1'
filter_index = 0

layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])

grads = K.gradients(loss, model.input)[0]

I am unable to execute grads = K.gradients(loss, model.input)[0] , it generates an error: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead我无法执行grads = K.gradients(loss, model.input)[0] ,它会生成错误: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead

You have two options too resolve this error:您也有两个选项可以解决此错误:

  1. .gradients is drepracted in TF2 - Replace gradients with GradientTape as suggested here https://github.com/tensorflow/tensorflow/issues/33135 .gradients 在 TF2 中使用 - 用 GradientTape 替换渐变,如此处的建议https://github.com/tensorflow/tensorflow/issues/33135

  2. Simply disable the eager-execution constrain form tf2 with the compat mode for tf1只需使用 tf1 的兼容模式禁用急切执行约束形式 tf2

Example running code for solution 2:解决方案 2 的示例运行代码:

from tensorflow.keras.applications import VGG16
from tensorflow.keras import backend as K
import tensorflow as tf
tf.compat.v1.disable_eager_execution()


model = VGG16(weights='imagenet',
              include_top=False)

layer_name = 'block3_conv1'
filter_index = 0

layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])

grads = K.gradients(loss, model.input)[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于启用 Eager Execution 时不支持 tf.gradients - About tf.gradients is not supported when eager execution is enabled tf.gradients 到 tf.GradientTape - tf.gradients to tf.GradientTape TF 2.0中的tf.GradientTape是否相当于tf.gradients? - Is tf.GradientTape in TF 2.0 equivalent to tf.gradients? 从 tf.gradients() 到 tf.GradientTape() 的转换返回 None - Conversion from tf.gradients() to tf.GradientTape() returns None 未启用急切执行时,张量对象不可迭代。 要迭代这个张量使用 tf.map_fn - Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn tf.GradientTape 不返回渐变 - tf.GradientTape doesn't return gradients "图形模式下的 tf.gradients() 与 tf.gradientTape.gradient()" - tf.gradients() vs tf.gradientTape.gradient() in graph mode 何时在编写自定义损失时使用 tf.GradientTape function - when to use tf.GradientTape during writing a custom loss function Tensorflow 错误。 TypeError:张量对象仅在启用急切执行时才可迭代。 要迭代此张量,请使用 tf.map_fn - Tensorflow error. TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn 在展平参数张量上使用tf.gradients或tf.hessians - Use tf.gradients or tf.hessians on flattened parameter tensor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM