首页 > 解决方案 > 得到“ValueError:磁带仍在录制,如果您在计算梯度磁带内的雅可比时尝试重新输入已经激活的磁带,可能会发生这种情况”

问题描述

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

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.

标签: pythontensorflowmachine-learningdeep-learninggradienttape

解决方案


推荐阅读