使用tape.gradient计算梯度的时候报错
import keras
import numpy as np
import tensorflow as tf
from keras.applications import vgg16
from keras import backend as K
model=vgg16.VGG16(weights='imagenet',include_top=False)
with tf.GradientTape() as tape:
layer_dict=dict([(layer.name,layer) for layer in model.layers])
layer_output=layer_dict[layer_name].output
loss=K.mean(layer_output[:,:,:,filter_index])
grads=tape.gradient(loss,model.input)[0]
运行的时候提示以下错误信息,该怎么处理
AttributeError Traceback (most recent call last)
<ipython-input-17-c356f312b286> in <module>
13 layer_output=layer_dict[layer_name].output
14 loss=K.mean(layer_output[:,:,:,filter_index])
---> 15 grads=tape.gradient(loss,model.input)[0]
16
17 iterate=K.function([model.input],[loss,grads])
~\anaconda3\envs\py3.7\lib\site-packages\tensorflow\python\eager\backprop.py in gradient(self, target, sources, output_gradients, unconnected_gradients)
1088 output_gradients=output_gradients,
1089 sources_raw=flat_sources_raw,
-> 1090 unconnected_gradients=unconnected_gradients)
1091
1092 if not self._persistent:
~\anaconda3\envs\py3.7\lib\site-packages\tensorflow\python\eager\imperative_grad.py in imperative_grad(tape, target, sources, output_gradients, sources_raw, unconnected_gradients)
75 output_gradients,
76 sources_raw,
---> 77 compat.as_str(unconnected_gradients.value))
AttributeError: 'KerasTensor' object has no attribute '_id'