得到“ValueError:磁带仍在录制,如果您在计算梯度磁带中的雅可比数时尝试重新输入一个alreadyactive磁带,则可能会发生这种情况”

2024-04-16 05:27:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我的损失函数需要计算雅可比矩阵行列式的对数。因此,我编写了以下代码

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.