我的损失函数需要计算雅可比矩阵行列式的对数。所以,我写了下面的代码
def gradient_update(self, X, optimizer):
with tf.GradientTape() as t, tf.GradientTape() as j:
Y = self(X)
out = -self.distribution(Y) - tf.math.log(tf.linalg.det(j.jacobian(Y, X)))
Loss = tf.math.reduce_sum(out)
grads = t.gradient(Loss, self.trainable_weights)
optimizer.apply_gradients(zip(grads, self.trainable_weights))
return Loss
这给了我以下错误
ValueError: Tape is still recording, This can happen if you try to re-enter an already-active tape.