TensorFlow
梯度下降部分直接调用TensorFlow中 GradientDescentOptimizer()
函数. 首先生成1000个高斯分布的点, 然后定义函数模型, 调用梯度下降函数. 学习率设置为0.5, 最后进行20次迭代, 红色的线为结果, 基本符合.
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
num_point = 1000
vector = []
#随机生成1000个遵循高斯分布的点
for i in range(num_point):
x1 = np.random.normal(0.0, 0.55)
y1 = x1 * 0.1 + 0.3 + np.random.normal(0.0, 0.05)
vector.append([x1, y1])
x_data = [v[0] for v in vector]
y_data = [v[1] for v in vector]
#定义模型
w = tf.Variable(tf.random_uniform([1], -1, 1), name='w')
b = tf.Variable(tf.zeros([1]), name='b')
y = w * x_data + b
#定义梯度下降法
loss = tf.reduce_mean(tf.square(y - y_data), name='loss')
optimize = tf.train.GradientDescentOptimizer(0.5)
train = optimize.minimize(loss, name='train')
init = tf.global_variables_initializer()
with tf.Session () as sess:
sess.run(init)
for step in range(20):
sess.run(train)
print('w =', sess.run(w), 'b =', sess.run(b), 'loss =', sess.run(loss))
#画图
col = np.random.rand(1000)
plt.scatter(x_data, y_data, c=col, marker='.')
plt.plot(x_data, sess.run(w) * x_data + sess.run(b),c='r')
plt.show()
结果: