用TensorFlow内建的Cholesky矩阵分解法实现矩阵分解的线性回归
2018-02-24 11:08
513 查看
用户对分解一个矩阵为多个矩阵的方法感兴趣的原因是,结果矩阵的特性使得其在应用中更高效。
结果:
# 线性回归:矩阵分解法 # 通过分解矩阵的方法求解有时更高效并且数值稳定 #---------------------------------- # # This function shows how to use TensorFlow to # solve linear regression via the matrix inverse. # # Given Ax=b, and a Cholesky decomposition such that # A = L*L' then we can get solve for x via # 1) L*y=t(A)*b # 2) L'*x=y import matplotlib.pyplot as plt import numpy as np import tensorflow as tf from tensorflow.python.framework import ops ops.reset_default_graph() # Create graph sess = tf.Session() # Create the data x_vals = np.linspace(0, 10, 100) y_vals = x_vals + np.random.normal(0, 1, 100) # Create design matrix 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)) # Create b matrix b = np.transpose(np.matrix(y_vals)) # Create tensors A_tensor = tf.constant(A) b_tensor = tf.constant(b) # 找到方阵的Cholesky矩阵分解 tA_A = tf.matmul(tf.transpose(A_tensor), A_tensor) # TensorFlow的cholesky()函数仅仅返回矩阵分解的下三角矩阵, # 因为上三角矩阵是下三角矩阵的转置矩阵。 L = tf.cholesky(tA_A) # Solve L*y=t(A)*b tA_b = tf.matmul(tf.transpose(A_tensor), b) sol1 = tf.matrix_solve(L, tA_b) # Solve L' * y = sol1 sol2 = tf.matrix_solve(tf.transpose(L), sol1) solution_eval = sess.run(sol2) # 抽取系数 slope = solution_eval[0][0] y_intercept = solution_eval[1][0] print('slope: ' + str(slope)) print('y_intercept: ' + str(y_intercept)) # Get best fit line best_fit = [] for i in x_vals: best_fit.append(slope*i+y_intercept) # Plot the results plt.plot(x_vals, y_vals, 'o', label='Data') plt.plot(x_vals, best_fit, 'r-', label='Best fit line', linewidth=3) plt.legend(loc='upper left') plt.show()
结果:
slope: 0.998921420857 y_intercept: 0.0799919541665
相关文章推荐
- [深度学习] (2):实现简单的线性回归(使用TensorFlow优化)
- 用TensorFlow实现iris数据集线性回归
- Tensorflow学习笔记(三)——用Tensorflow实现线性回归和逻辑回归
- 基于Tensorflow实现基本的线性回归(Linear regression)
- 使用TensorFlow实现简单的线性回归(LinearRegression)
- 用tensorflow实现线性回归问题
- TensorFlow实现iris数据集线性回归
- 用tensorflow实现线性回归和相应的损失函数的运用
- Tensorflow学习(一)--使用TensorFlow实现线性回归
- Tensorflow实现线性回归
- Keras上基于TensorFlow实现简单线性回归模型
- tensorflow 实现 正规方程求解线性回归
- tensorflow实现线性回归的完整程序
- 线性回归(linear_regression),多项式回归(polynomial regression)(Tensorflow实现)
- tensorflow实现线性回归(矩阵解法)
- tensorflow 学习(一)实现线性回归
- Tensorflow实现线性回归
- 线性回归、梯度下降以及运用TensorFlow进行简单实现
- Tensorflow学习笔记(一):初识TensorFlow——实现线性回归