使用tensorflow实现矩阵分解方式

Veronica ·
更新时间:2024-09-21
· 837 次阅读

采用最小二乘的求逆方法在大部分情况下是低效率的。特别地,当局镇非常大时效率更低。另外一种实现方法是矩阵分解,此方法使用tensorflow内建的Cholesky矩阵分解法。Cholesky矩阵分解法把一个矩阵分解为上三角矩阵和下三角矩阵,L和L'。求解Ax=b,改写成LL'=b。首先求解Ly=b,然后求解L'x=y得到系数矩阵。

1. 导入编程库,初始化计算图,生成数据集。接着获取矩阵A和b。

>>> import matplotlib.pyplot as plt >>> import numpy as np >>> import tensorflow as tf >>> from tensorflow.python.framework import ops >>> ops.reset_default_graph() >>> sess=tf.Session() >>> x_vals=np.linspace(0,10,100) >>> y_vals=x_vals+np.random.normal(0,1,100) >>> x_vals_column=np.transpose(np.matrix(x_vals)) >>> ones_column=np.transpose(np.matrix(np.repeat(1,100))) >>> A=np.column_stack((x_vals_column,ones_column)) >>> b=np.transpose(np.matrix(y_vals)) >>> A_tensor=tf.constant(A) >>> b_tensor=tf.constant(b)

2. 找到方阵的Cholesky矩阵分解。

注意:tensorflow的cholesky()函数仅仅返回矩阵分解的下三角矩阵,因为上三角矩阵是下三角矩阵的转置矩阵。

>>> tA_A=tf.matmul(tf.transpose(A_tensor),A_tensor) >>> L=tf.cholesky(tA_A) >>> tA_b=tf.matmul(tf.transpose(A_tensor),b) >>> sol1=tf.matrix_solve(L,tA_b) >>> sol2=tf.matrix_solve(tf.transpose(L),sol1)

3. 抽取系数

>>> solution_eval=sess.run(sol2) >>> solution_eval array([[1.01379067], [0.02290901]]) >>> slope=solution_eval[0][0] >>> y_intercept=solution_eval[1][0] >>> print('slope:'+str(slope)) slope:1.0137906744047482 >>> print('y_intercept:'+str(y_intercept)) y_intercept:0.022909011828880693 >>> best_fit=[] >>> for i in x_vals: ... best_fit.append(slope*i+y_intercept) ... >>> plt.plot(x_vals,y_vals,'o',label='Data') [<matplotlib.lines.Line2D object at 0x000001E0A58DD9B0>] >>> plt.plot(x_vals,best_fit,'r-',label='Best fit line',linewidth=3) [<matplotlib.lines.Line2D object at 0x000001E0A2DFAF98>] >>> plt.legend(loc='upper left') <matplotlib.legend.Legend object at 0x000001E0A58F03C8> >>> plt.show()

以上这篇使用tensorflow实现矩阵分解方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。

您可能感兴趣的文章:详解Numpy数组转置的三种方法T、transpose、swapaxesTensorFlow损失函数专题详解关于TensorFlow新旧版本函数接口变化详解Tensorflow:转置函数 transpose的使用详解



矩阵分解 tensorflow 矩阵

需要 登录 后方可回复, 如果你还没有账号请 注册新账号