1. 程式人生 > >TensorFlow 實現線性回歸

TensorFlow 實現線性回歸

com 坐標 lib bubuko pre oba spl ide alt

1、生成高斯分布的隨機數

導入numpy模塊,通過numpy模塊內的方法生成一組在方程

y = 2 * x + 3

周圍小幅波動的隨機坐標。代碼如下:

 1 import numpy as np
 2 import matplotlib.pyplot as plot
 3 
 4 
 5 def getRandomPoints(count):
 6     xList = []
 7     yList = []
 8     for i in range(count):
 9         x = np.random.normal(0, 0.5)
10         y = 2 * x + 3 + np.random.normal(0, 0.3)
11 xList.append(x) 12 yList.append(y) 13 return xList, yList 14 15 16 if __name__ == __main__: 17 X, Y = getRandomPoints(1000) 18 plot.scatter(X, Y) 19 plot.show()

運行上述代碼,輸出圖形如下:

技術分享圖片

2、采用TensorFlow來獲取上述方程的系數

  首先搭建基本的預估模型y = w * x + b,然後再采用梯度下降法進行訓練,通過最小化損失函數的方法進行優化,最終訓練得出方程的系數。

  在下面的例子中,梯度下降法的學習率為0.2,訓練叠代次數為100次。

 1 def train(x, y):
 2     # 生成隨機系數
 3     w = tf.Variable(tf.random_uniform([1], -1, 1))
 4     # 生成隨機截距
 5     b = tf.Variable(tf.random_uniform([1], -1, 1))
 6     # 預估值
 7     preY = w * x + b
 8 
 9     # 損失值:預估值與實際值之間的均方差
10     loss = tf.reduce_mean(tf.square(preY - y))
11 # 優化器:梯度下降法,學習率為0.2 12 optimizer = tf.train.GradientDescentOptimizer(0.2) 13 # 訓練:最小化損失函數 14 trainer = optimizer.minimize(loss) 15 16 with tf.Session() as sess: 17 sess.run(tf.global_variables_initializer()) 18 # 打印初始隨機系數 19 print(init w:, sess.run(w), b:, sess.run(b)) 20 # 先訓練個100次: 21 for i in range(100): 22 sess.run(trainer) 23 # 每10次打印下系數 24 if i % 10 == 9: 25 print(w:, sess.run(w), b:, sess.run(b)) 26 27 28 if __name__ == __main__: 29 X, Y = getRandomPoints(1000) 30 train(X, Y)

  運行上面的代碼,某次的最終結果為:

w = 1.9738449
b = 3.0027733

僅100次的訓練叠代,得出的結果已十分接近方程的實際系數。

  某次模擬訓練中的輸出結果如下:

init w: [-0.6468966] b: [0.52244043]
w: [1.0336646] b: [2.9878206]
w: [1.636582] b: [3.0026987]
w: [1.8528996] b: [3.0027785]
w: [1.930511] b: [3.0027752]
w: [1.9583567] b: [3.0027738]
w: [1.9683474] b: [3.0027735]
w: [1.9719319] b: [3.0027733]
w: [1.9732181] b: [3.0027733]
w: [1.9736794] b: [3.0027733]
w: [1.9738449] b: [3.0027733]

3、完整代碼和結果

完整測試代碼:

技術分享圖片
 1 import numpy as np
 2 import matplotlib.pyplot as plot
 3 import tensorflow as tf
 4 
 5 
 6 def getRandomPoints(count, xscale=0.5, yscale=0.3):
 7     xList = []
 8     yList = []
 9     for i in range(count):
10         x = np.random.normal(0, xscale)
11         y = 2 * x + 3 + np.random.normal(0, yscale)
12         xList.append(x)
13         yList.append(y)
14     return xList, yList
15 
16 
17 def train(x, y, learnrate=0.2, cycle=100):
18     # 生成隨機系數
19     w = tf.Variable(tf.random_uniform([1], -1, 1))
20     # 生成隨機截距
21     b = tf.Variable(tf.random_uniform([1], -1, 1))
22     # 預估值
23     preY = w * x + b
24 
25     # 損失值:預估值與實際值之間的均方差
26     loss = tf.reduce_mean(tf.square(preY - y))
27     # 優化器:梯度下降法
28     optimizer = tf.train.GradientDescentOptimizer(learnrate)
29     # 訓練:最小化損失函數
30     trainer = optimizer.minimize(loss)
31 
32     with tf.Session() as sess:
33         sess.run(tf.global_variables_initializer())
34         # 打印初始隨機系數
35         print(init w:, sess.run(w), b:, sess.run(b))
36         for i in range(cycle):
37             sess.run(trainer)
38             # 每10次打印下系數
39             if i % 10 == 9:
40                 print(w:, sess.run(w), b:, sess.run(b))
41         return sess.run(w), sess.run(b)
42 
43 
44 if __name__ == __main__:
45     X, Y = getRandomPoints(1000)
46     w, b = train(X, Y)
47     plot.scatter(X, Y)
48     plot.plot(X, w * X + b, c=r)
49     plot.show()
View Code

  最終效果圖如下,藍色為高斯隨機分布數據,紅色為最終得出的直線:

技術分享圖片

本文地址:https://www.cnblogs.com/laishenghao/p/9571343.html

TensorFlow 實現線性回歸