使用Tensorflow实现线性回归

该代码示例展示了如何用TensorFlow进行线性回归建模。首先,生成随机数据点,然后初始化模型参数w和b。定义线性回归模型和均方误差损失函数。接着,使用梯度下降法优化模型参数,并在训练过程中打印损失值和参数变化。最后,对比TensorFlow模型与sklearn的线性回归结果。
摘要由CSDN通过智能技术生成

 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)  

运行结果截图

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值