利用tensorflow实现线性回归(cholesky分解)
2017-12-31 11:29
666 查看
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)
tA_A = tf.matmul(tf.transpose(A_tensor),A_tensor) #利用矩阵分解方法求解线性回归问题
L = tf.cholesky(tA_A) #cholesky 分解,返回的是下三角矩阵LL’x=b,返回L
#L 和L’是转置关系
tA_b = tf.matmul(tf.transpose(A_tensor),b)
sol1 = tf.matrix_solve(L,tA_b) # 利用tensorflow内置的方法解方程组
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))
best_fits = []
for i in x_vals:
best_fits.append(slope*i + y_intercept)
plt.plot(x_vals,y_vals,’^’,label=’Data’)
plt.plot(x_vals,best_fits,’-‘,color=’r’,label=’Best fit line’,linewidth=3)
plt.legend(loc=’upper left’)
plt.show()
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)
tA_A = tf.matmul(tf.transpose(A_tensor),A_tensor) #利用矩阵分解方法求解线性回归问题
L = tf.cholesky(tA_A) #cholesky 分解,返回的是下三角矩阵LL’x=b,返回L
#L 和L’是转置关系
tA_b = tf.matmul(tf.transpose(A_tensor),b)
sol1 = tf.matrix_solve(L,tA_b) # 利用tensorflow内置的方法解方程组
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))
best_fits = []
for i in x_vals:
best_fits.append(slope*i + y_intercept)
plt.plot(x_vals,y_vals,’^’,label=’Data’)
plt.plot(x_vals,best_fits,’-‘,color=’r’,label=’Best fit line’,linewidth=3)
plt.legend(loc=’upper left’)
plt.show()
相关文章推荐
- 利用tensorflow 一步一步实现一个简单神经网络,线性回归
- 基于Tensorflow实现基本的线性回归(Linear regression)
- 使用TensorFlow实现简单的线性回归(LinearRegression)
- 利用最小二乘法实现图片中多个点的一元线性回归
- 用TensorFlow实现iris数据集线性回归
- TensorFlow实现线性回归
- 利用 TensorFlow 实现排序和搜索算法
- Tensorflow 实现线性回归(迭代方法)
- 利用 TensorFlow 实现排序和搜索算法
- TensorFlow实现iris数据集线性回归
- tensorflow利用RNN和双向RNN实现MNIST分类问题
- Tensorflow实现线性回归
- 在Tensorflow环境下利用梯度下降法进行线性回归
- 利用 TensorFlow 实现上下文的 Chat-bots
- 用tensorflow实现线性回归问题
- 利用梯度下降法实现线性回归的算法及matlab实现
- Tensorflow实现线性回归
- 利用梯度下降法实现简单的线性回归
- 利用 TensorFlow 高级 API Keras 实现 MLP,CNN,LSTM
- Tensorflow实现线性回归