机器学习入门笔记(一)-多项式回归

一、生成数据,生成100个点,x是从-3到3之间均匀分布的,y是x的正弦函数值加上-0.5~0.5之间的一个随机值得到的结果

1
2
3
4
5
6
7
8
9
10
%matplotlib inline
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (14,8)

n_insp = 100
x_set = np.linspace(-3, 3, n_insp)
y_set = np.sin(x_set) + np.random.uniform(-0.5, 0.5, n_insp)
plt.scatter(x_set, y_set)
<matplotlib.collections.PathCollection at 0x7f4022ac3390>

png

二、定义参数输入参数的占位符

1
2
X = tf.placeholder(tf.float32, name = "X")
Y = tf.placeholder(tf.float32, name= "Y")

三、定义运算图,假定特征有x,x^2,x^3,三项,则整个函数的计算为 y=wx + W1x^2 + w2*x^3 + b

1
2
3
4
5
6
7
w = tf.Variable(tf.random_normal([1]), name = "weight")
w1= tf.Variable(tf.random_normal([1]), name = "weight_1")
w2 = tf.Variable(tf.random_normal([1]), name = "weight_2")
b = tf.Variable(tf.random_normal([1]), name= "bias")
Y_predict = tf.add(tf.multiply(X, w), tf.multiply(w1, tf.pow(X,2)))
Y_predict = tf.add(Y_predict, tf.multiply(w2, tf.pow(X,3)))
Y_predict = tf.add(Y_predict, b)

四、定义损失函数和初始化optimizer,这里损失函数是计算y与预测值的方差均值

1
x_set.shape
(100,)
1
2
3
4
loss = tf.reduce_sum(tf.pow(Y_predict - Y, 2)) / x_set.shape[0]
# 定义学习率为 0.01
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

五、训练迭代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
with tf.Session() as sess:
# 初始化所有变量
sess.run(tf.global_variables_initializer())

# 定义tensorboard的log保存路径
writer = tf.summary.FileWriter("./graphs/polynomial_regulation", sess.graph)
for i in range(1000):
# 每一对数据进行一次迭代,遍历完数据集为一轮,共训练1000轮
sum_loss = 0
for x, y in zip(x_set, y_set):
_, l = sess.run([optimizer, loss],feed_dict = {X:x,Y:y})
sum_loss += l
if i%20 == 0:
# 每二十轮打印一次当前准确率
print('Epoch {0}: {1}'.format(i, sum_loss/x_set.shape[0]))
writer.close()
# 取出最后的权重和偏移值
w, w1, w2, b = sess.run([w,w1,w2,b])
Epoch 0: 0.011287849849201415
Epoch 20: 0.002904312098070534
Epoch 40: 0.0022930659861960124
Epoch 60: 0.001861640907111166
Epoch 80: 0.0015570845219596663
Epoch 100: 0.0013421906849065124
Epoch 120: 0.0011906639028636335
Epoch 140: 0.0010839080285924751
Epoch 160: 0.0010087724971023703
Epoch 180: 0.0009559617677211918
Epoch 200: 0.0009189029343284005
Epoch 220: 0.0008929493658629184
Epoch 240: 0.0008748186269485813
Epoch 260: 0.0008621914480389414
Epoch 280: 0.0008534308153672754
Epoch 300: 0.0008473811643942852
Epoch 320: 0.0008432286334247863
Epoch 340: 0.0008403999191202516
Epoch 360: 0.0008384917434211502
Epoch 380: 0.0008372206560037965
Epoch 400: 0.00083638845353045
Epoch 420: 0.0008358570021426814
Epoch 440: 0.000835529392748171
Epoch 460: 0.0008353382271738141
Epoch 480: 0.0008352376613193435
Epoch 500: 0.0008351957923265729
Epoch 520: 0.0008351906345031778
Epoch 540: 0.0008352075220511918
Epoch 560: 0.0008352370859906216
Epoch 580: 0.0008352726358773677
Epoch 600: 0.0008353100529581625
Epoch 620: 0.0008353470957054076
Epoch 640: 0.0008353820821457792
Epoch 660: 0.0008354142016842925
Epoch 680: 0.0008354429479974713
Epoch 700: 0.0008354686113952425
Epoch 720: 0.0008354910716359676
Epoch 740: 0.000835510656680416
Epoch 760: 0.0008355276462786598
Epoch 780: 0.0008355419888481741
Epoch 800: 0.000835554459651977
Epoch 820: 0.0008355653869491241
Epoch 840: 0.0008355743816347427
Epoch 860: 0.0008355819873166936
Epoch 880: 0.0008355887906360771
Epoch 900: 0.000835594305444296
Epoch 920: 0.0008355990350996834
Epoch 940: 0.0008356031159648758
Epoch 960: 0.0008356063359790334
Epoch 980: 0.0008356092827835581
1
2
3
4
print("w:"+str(w[0]))
print("w1:"+str(w1[0]))
print("w2:"+str(w2[0]))
print("b:"+str(b[0]))
w:0.84994864
w1:0.018252745
w2:-0.09445592
b:-0.07383199

六、结果展示

1
2
3
4
plt.plot(x_set, y_set, 'bo', label='Real data')
plt.plot(x_set, x_set*w+ np.power(x_set,2)*w1 + np.power(x_set,3)*w2 + b, 'r', label='Predicted data')
plt.legend()
plt.show()

png

-------------本文结束感谢您的阅读-------------

本文标题:机器学习入门笔记(一)-多项式回归

文章作者:fengxi

发布时间:2019年03月08日 - 18:03

最后更新:2019年04月04日 - 21:04

原始链接:https://super-storm.github.io/2019/03/08/机器学习入门笔记(一)-多项式回归/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。