python解线性方程组Ax=b的两种方法

Notes

做 label propagation 时涉及到 Ax = b 的求解,有两种方式:

  • 解析解: x = A − 1 b x=A^{-1}b x=A1b
  • 迭代求近似解:共轭梯度法(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)

References

  1. Label Propagation for Deep Semi-supervised Learning
  2. Learning with Local and Global Consistency
  3. Semi-Supervised Learning with Graphs
  4. 标签传播算法(Label Propagation)及Python实现
  5. TensorFlow函数:tf.matrix_solve
  6. NumPy 线性代数
  7. scipy.linalg.solve
  8. scipy.sparse.linalg.cg
  9. 利用TensorFlow和Numpy求解线性方程组
  10. sfujiwara/cg.py
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值