线性回归的从零搭建

  1. import
%matplotlib inline
import torch
import random
  1. 人造带有噪声的线性数据集

使用线性模型参数 w = [2, -3.4]T、b = 4.2和噪声项noise生成数据集及其表现:
y = Wx + b + noise

def synthetic_data(w, b, num_examples):
    """生成 y = wx + b + noise"""
    X = torch.normal(0,1, (num_examples, len(w))) # 第三个parameter,是out的shape,指人造的X的格式
    y = torch.matmul(X, w) + b   ## matmul 比起mm或者mv,会自己判断是m还是v,所以用matmul比较通用
    y += torch.normal(0, 0.01, y.shape) ## 制造噪声
    return X, y.reshape((-1,1))

true_w = torch.tensor([2, -3.4])
true_b = 4.3
features, labels = synthetic_data(true_w, true_b, 1000)

将结果打印出来看一下:

# features中的每一行都包含一个二维数据样本,labels中的每一行都包含一维标签值(一个标量)

print('features', features[0], '\nlabel:', labels[0])

features tensor([ 1.1656, -2.7100])
label: tensor([15.8300])

将结果可视化

import matplotlib.pyplot as plt
#plt.figure()
plt.scatter(features[:,0].detach().numpy(), labels.detach().numpy(), 1)  ## detach可以不加,目的是将feature与label变成常数,与变量无关
plt.show()

在这里插入图片描述

  1. 将数据分割,使其变成批数据
  • 定义一个data_iter函数,该函数接受批量大小、特征矩阵和标签向量作为输入,
  • 生成大小为batch_size的小批量
def data_iter(batch_size, features, labels):
    num_examples = len(features)
    indices = list(range(num_examples))
    
    #随机取样
    random.shuffle(indices)
    
    for i in range(0, num_examples,batch_size):
        batch_indices = torch.tensor(
            indices[i:min(i+batch_size, num_examples)]) ## 最后一组,如果超过了总数,那就取到总数最后一个
        yield features[batch_indices], labels[batch_indices]
  1. 模型参数初始化
w = torch.normal(0, 0.01, size = (2,1), requires_grad=True)  ## w的初始化需要随机
b = torch.zeros(1, requires_grad = True)  ## b的初始化可以为0
  1. 定义模型
def linreg(X, w, b):
    """线性回归模型"""
    return torch.matmul(X, w) + b
  1. 定义损失函数
def squared_loss(y_hat, y):
    """均方损失"""
    return (y_hat-y.reshape(y_hat.shape))**2 /2  ## 损失函数通常需要计算平均值,这里的平均值在梯度下降中计算
  1. 定义优化算法 - 随机梯度下降(Stochastic Gradient Descent, SGD)
def sgd(params, lr, batch_size):
    """小批量随机梯度下降 MBGD"""
    with torch.no_grad():
        for param in params:
            param -= lr * param.grad / batch_size  
            param.grad.zero_()

BGD,SGD,MBGD区别

  1. BGD 批量梯度下降 Batch Gradient Descent
    每个epoch过程中把所有的样本数据集都迭代了一遍
    ![[Pasted image 20211220082917.png]]
    m:样本数量,n:样本维度
  2. SGD 随机梯度下降 Stochastic Gradient Descent
    每次的权重更新只利用数据集中的一个样本来完成,也即一个epoch过程只有以此迭代和一个更新数据
    ![[Pasted image 20211220083415.png]]
  3. MBGD 小批量梯度下降法 Mini-batch Gradient Descent
    是在BGD和SGD算法中找到了一个trade-off,即加快更新速度,并减少噪声的影响,从而减少训练时间和提高准确率
    每次随机选择一些个样本
    ![[Pasted image 20211220083640.png]]
    l:随机数量
  1. 开始学习
lr = 0.01
num_epochs = 3
net = linreg
loss = squared_loss

for epoch in range(num_epochs):
    for X, y in data_iter(batch_size, features, labels):
        l = loss(net(X, w, b), y)
        l.sum().backward()
        sgd([w, b], lr, batch_size)
    with torch.no_grad():
        train_l = loss(net(features, w, b), labels)
        print(f'epoch {epoch +1}, loss {float(train_l.mean()):f}')

epoch 1, loss 2.124873
epoch 2, loss 0.251395
epoch 3, loss 0.030194

  1. 比较真实参数和通过训练学到的参数来评估训练的成功程度
print(f'w的估算误差:{true_w - w.reshape(true_w.shape)}')
print(f'b的估算误差:{true_b - b}')

w的估算误差:tensor([ 0.0488, -0.0874], grad_fn=)
b的估算误差:tensor([0.0927], grad_fn=)



路遥知马力,
更应砥砺前行
mingxin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值