Notes
做 label propagation 时涉及到 Ax = b 的求解,有两种方式:
- 解析解: x = A − 1 b x=A^{-1}b x=A−1b
- 迭代求近似解:共轭梯度法(conjugate gradient),要求 A 对称正定。
Closed Form Solution
求闭解的方式,TensorFlow、numpy、scipy 都有相应的函数可以调
import tensorflow as tf
import numpy as np
import scipy
A = np.array([
[1, 4, 7],
[5, 2, 8],
[9, 6, 3]
])
b = np.array([21, 24, 39])
# numpy
print(np.linalg.solve(A, b))
# scipy
print(scipy.linalg.solve(A, b))
# tensorflow
with tf.Session() as sess:
A = tf.constant(A, dtype='float64')
b = tf.constant(b.reshape(-1, 1), dtype='float64') # b 的秩要是 2
x = tf.matrix_solve(A, b)
print('A:', sess.run(A))
print('b:', sess.run(b))
print(sess.run(x))
# 验算
b_ = tf.matmul(A, x)
print('b_:', sess.run(b_))
Conjugate Gradient
共轭梯度法可以调 scipy 的包
import numpy as np
from scipy.sparse.linalg import cg
A = np.array([
[1, 4, 7],
[5, 2, 8],
[9, 6, 3]
])
b = np.array([21, 24, 39])
x, info = cg(A, b) # info 是关于迭代收敛的信息,int
print(x)