1.生成数据点集
#获得想生成的随机点的个数
dot=int(input('想获得的随机点个数为:'))
# 生成数据
x = np.linspace(0, 10, dot) + np.random.rand(dot)
y = np.linspace(0, 10, dot) + np.random.rand(dot)
plt.scatter(x, y)
2.初始化模型参数
w = tf.Variable(np.random.randn() * 0.02)
b = tf.Variable(0.)
3.定义模型
def linear_regression(x):
return w * x +b
4.定义损失函数
def mean_square_loss(y_pred, y_true):
return tf.reduce_mean(tf.square(y_pred - y_true))
5.定义优化算法
# 优化器
optimizer = tf.optimizers.SGD()
# 优化过程
def optimization():
with tf.GradientTape() as g:
pred = linear_regression(x)
loss = mean_square_loss(pred, y)
# 计算梯度
gradients = g.gradient(loss, [w, b])
# 更新w, b
optimizer.apply_gradients(zip(gradients, [w, b]))
6.训练
for step in range(5000):
optimization() # 迭代w, b
# 每迭代500次展示结果
if step % 500 == 0:
pred = linear_regression(x)
loss = mean_square_loss(pred, y)
print(f'step:{step}, loss:{loss}, w:{w.numpy()}, b: {b.numpy()}')
完整代码以及运行结果
代码
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
#获得想生成的随机点的个数
dot=int(input('想获得的随机点个数为:'))
# 生成数据
x = np.linspace(0, 10, dot) + np.random.rand(dot)
y = np.linspace(0, 10, dot) + np.random.rand(dot)
plt.scatter(x, y)
# 设置初始w,b
w = tf.Variable(np.random.randn() * 0.02)
b = tf.Variable(0.)
# 线性模型
def linear_regression(x):
return w * x +b
# 损失函数
def mean_square_loss(y_pred, y_true):
return tf.reduce_mean(tf.square(y_pred - y_true))
# 优化器
optimizer = tf.optimizers.SGD()
# 优化过程
def optimization():
with tf.GradientTape() as g:
pred = linear_regression(x)
loss = mean_square_loss(pred, y)
# 计算梯度
gradients = g.gradient(loss, [w, b])
# 更新w, b
optimizer.apply_gradients(zip(gradients, [w, b]))
# 训练
for step in range(5000):
optimization() # 迭代w, b
# 每迭代500次展示结果
if step % 500 == 0:
pred = linear_regression(x)
loss = mean_square_loss(pred, y)
print(f'step:{step}, loss:{loss}, w:{w.numpy()}, b: {b.numpy()}')
linear = LinearRegression() # 线性回归
linear.fit(x.reshape(-1, 1), y)
plt.scatter(x, y)
x_test = np.linspace(0, 10, 20).reshape(-1, 1)
plt.plot(x_test, linear.coef_ * x_test + linear.intercept_, c='r')
plt.plot(x_test, w.numpy() * x_test + b.numpy(), c='g', lw=10, alpha=0.5)
运行结果截图