keras(tensorflow后台)中计算梯度时出现错误 | 您所在的位置:网站首页 › 求梯度的公式 › keras(tensorflow后台)中计算梯度时出现错误 |
我试图使用tensorflow-gpu 2.4.1版和Keras 2.4.3版计算VGG16中的一个CNN过滤器的梯度,并使用以下代码计算图像输入。 from keras.applications import VGG16 from 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]this results in the following error: RuntimeError: tf.gradients在启用急切执行时不被支持。请使用tf.GradientTape代替。 此外,试图使用tf.GradientTape也会引发另一个错误。 with tf.GradientTape() as gtape: grads = gtape.gradient(loss, model.input)AttributeError: 'KerasTensor'对象没有'_id'属性 试图禁用急切执行也不起作用。 tf.compat.v1.disable_eager_execution()因为它返回的梯度是 None。 我希望得到任何有关解决这个问题的方法的信息。 预先感谢。 |
CopyRight 2018-2019 实验室设备网 版权所有 |