我正在尝试为简单的密集自动编码器计算输入层的梯度
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.place_holder = Dense(21393, activation='relu')
self.dense1 = Dense(1500, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.3), name='first')
self.dense2 = Dense(500, activation='relu')
self.dense3= Dense(1500, activation='relu')
self.out = Dense(21393, activation='relu')
def call(self, inp):
inp = tf.reshape(inp,[-1, 21393])
d1 = self.dense1(inp)
d2 = self.dense2(d1)
d3 = self.dense3(d2)
out = self.out(d3)
return out
我将损失设置为网络输出中的特定指标,并尝试访问在任意输入上一步计算的梯度:
optimizer = tf.keras.optimizers.Adam()
print(ind)
with tf.GradientTape() as tape:
predictions = model(input_dummy)
loss = predictions[0, ind]
gradients = tape.gradient(loss, model.input)
但是,出现以下错误: AttributeError:my_model_7层未连接,无输入返回。
在Tensorflow 2.x上是否有更好的方法来计算网络输入上的梯度?