【Pytorch学习笔记1】线性回归的从0开始实现

【Pytorch学习笔记1】线性回归的从0开始实现,第1张

备注:李沐大神源代码是用Jupyter写的,笔者想用Pycharm实现并作为学习笔记,如有侵权,请联系我删除。

前言

最近在学习李沐大神的深度学习课程,不得不说,李沐大神教的真好,下面的代码也是跟着他的视频一步一步敲出来的,也添加了自己的一些理解。因为我习惯用Pycharm,所以用Pycharm实现的,也给其他想用Pycharm实现沐神代码的小伙伴一点点小帮助,随着后面的学习,我也会陆续更新的。

Python版本:3.8.6
torch版本:1.11.0
d2l版本:0.17.5

一、先上源码
import random
import torch
from d2l import torch as d2l

# 构造人造数据集:
def synthetic_data(w, b, num_examples):
    """生成 y = Xw + b + 噪声"""
    X = torch.normal(0, 1, (num_examples, len(w)))
    y = torch.matmul(X, w) + b
    y += torch.normal(0, 0.01, y.shape) # 均值为0,方差为0.01的噪声
    return X, y.reshape((-1, 1))  # y作为列向量返回

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

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

d2l.set_figsize()
d2l.plt.scatter(features[:,1].detach().numpy(),labels.detach().numpy(),1)
d2l.plt.show()

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]

batch_size = 10

for X, y in data_iter(batch_size,features,labels):
   print(X, '\n', y)
   break

w = torch.normal(0, 0.01, size=(2, 1), requires_grad=True)
b = torch.zeros(1,requires_grad=True)

def linreg(X, w, b):
    """线性回归模型"""
    return torch.matmul(X, w) + b

def squard_loss(y_hat, y):
    """均方损失"""
    return (y_hat - y.reshape(y_hat.shape))**2 / 2  # reshape 避免一个是行向量,一个是列向量

def sgd(params, lr, batch_size):
    """小批量梯度下降"""
    with torch.no_grad():
        for param in params:
            param += -lr * param.grad / batch_size  # 因为后面的损失是一个batch_size的总损失,所以这里需要除batch_size
            param.grad.zero_()

lr = 0.03  # 学习率
num_epochs = 3 # 整个数据集扫3遍
net = linreg
loss = squard_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) # 批量梯度下降,算完了一个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之后显示loss

print(f'w的估计误差:{true_w - w.reshape(true_w.shape)}')
print(f'b的估计误差:{true_b - b}')


二、输出

人造数据集分布如下:

其他输出如下:

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/734660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存