我对使用 TensorFlow 计算矩阵行列式的导数很感兴趣。我通过实验可以看出,TensorFlow 并没有实现通过行列式求微分的方法:
LookupError: No gradient defined for operation 'MatrixDeterminant'
(op type: MatrixDeterminant)
进一步调查表明,实际上可以计算导数;参见例如 Jacobi's formula .我确定,为了实现这种通过行列式进行区分的方法,我需要使用函数装饰器,
@tf.RegisterGradient("MatrixDeterminant")
def _sub_grad(op, grad):
...
但是,我对 tensorflow 不够熟悉,无法理解这是如何实现的。有没有人对此事有任何见解?
这是我遇到此问题的示例:
x = tf.Variable(tf.ones(shape=[1]))
y = tf.Variable(tf.ones(shape=[1]))
A = tf.reshape(
tf.pack([tf.sin(x), tf.zeros([1, ]), tf.zeros([1, ]), tf.cos(y)]), (2,2)
)
loss = tf.square(tf.matrix_determinant(A))
optimizer = tf.train.GradientDescentOptimizer(0.001)
train = optimizer.minimize(loss)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for step in xrange(100):
sess.run(train)
print sess.run(x)
最佳答案
请查看“在 Python 中实现渐变”部分 here
具体来说,你可以如下实现
@ops.RegisterGradient("MatrixDeterminant")
def _MatrixDeterminantGrad(op, grad):
"""Gradient for MatrixDeterminant. Use formula from 2.2.4 from
An extended collection of matrix derivative results for forward and reverse
mode algorithmic differentiation by Mike Giles
-- http://eprints.maths.ox.ac.uk/1079/1/NA-08-01.pdf
"""
A = op.inputs[0]
C = op.outputs[0]
Ainv = tf.matrix_inverse(A)
return grad*C*tf.transpose(Ainv)
然后一个简单的训练循环来检查它是否有效:
a0 = np.array([[1,2],[3,4]]).astype(np.float32)
a = tf.Variable(a0)
b = tf.square(tf.matrix_determinant(a))
init_op = tf.initialize_all_variables()
sess = tf.InteractiveSession()
init_op.run()
minimization_steps = 50
learning_rate = 0.001
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train_op = optimizer.minimize(b)
losses = []
for i in range(minimization_steps):
train_op.run()
losses.append(b.eval())
然后你可以想象随着时间的推移你的损失
import matplotlib.pyplot as plt
plt.ylabel("Determinant Squared")
plt.xlabel("Iterations")
plt.plot(losses)
关于python - tensorflow 中的矩阵行列式微分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33714832/