深度学习记录1(线性回归的实现)

1、整体思路

根据线性回归的定义,

,建立线性回归模型,在损失函数的计算上,采用L2 Loss(均方误差)。同时,对于模型的优化采用随机梯度下降。

2、详细代码分析

import random
import torch
from d2l import torch as d2l
def synthetic_data(w, b, num_examples):
    X = torch.normal(0, 1, (num_examples, len(w)))
    y = torch.matmul(X, w) + b
    y += torch.normal(0, 0.01, y.shape)
    return X, y.reshape((-1, 1))
 
true_w = torch.tensor([2, -3.4])
true_b = 4.2
features, labels = synthetic_data(true_w, true_b, 1000)

由于未采用数据集,所以通过自己生成数据来验证模型的正确性。synthetic_data函数为实现的功能为根据相关参数,生成随机数据集,函数输入参数为真实的w,b以及所需要的样本数,通过该函数以及定义的w,b,可以按照

生成相关数据,样本总数为1000。X按照离散正态分布生成,它的每一行为一个样本,每一列为一个特征值。y根据X的值进行生成,为使数据集贴近真实情况,对其利用离散正态分布加上小扰动。

返回值为生成的样本以及对应的标签。(注:在此方法下,)

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

features,labels本质上来看就是列表,所以为了实现数据的随机读取,只需要将读取列表时的标号打乱即可,同时可以保证完成对所有数据的读取。indices中的数据为0到num_examples-1中的自然数顺序排列,利用random.shuffle()实现对indices进行随机打乱。使用for循环完成对indices的切割,达到每次输出batch_size样本数的目的。(注:该函数使用生成器,在每次调用时读取batch_size的数据,而不是一次性全部输出)

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

对待求参数进行初始化,由于待求的w有两个,输出结果一个,因此w的size为(2,1),由于之后需要进行梯度计算,因此requires_grad=True。

def linreg(X, w, b): 
    """线性回归模型。"""
    return torch.matmul(X, w) + b
 
def squared_loss(y_hat, y): 
    """均方损失。"""
    return (y_hat - y.reshape(y_hat.shape))**2 / 2
 
def sgd(params, lr, batch_size):  
    """小批量随机梯度下降。"""
    with torch.no_grad():
        for param in params:
            param -= lr * param.grad / batch_size
            param.grad.zero_()

定义线性回归模型,均方损失,随机梯度下降函数,在进行损失计算的时候,按照公式,应当除以batch_size,由于该计算是线性的,因此在进行损失计算时或是在更新参数时计算不影响最后结果。由于pytorch中对于梯度是累积的,因此在每一次更新参数后,应该将保存的梯度清零。

lr = 0.03 #学习率
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)  # `X`和`y`的小批量损失
        # 因为`l`形状是(`batch_size`, 1),而不是一个标量。`l`中的所有元素被加到一起,
        # 并以此计算关于[`w`, `b`]的梯度
        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}')

此处需要进行l.sum(),是由于在对每一个batch进行损失计算时得到的y为一个列向量,根据反向传播,需要将当前所有样本计算后得到的输出相加并除以样本数的结果进行计算。在该种方法下,更新参数的次数为3*(1000 / 10)。

猜你喜欢

转载自blog.csdn.net/zhaomengsen/article/details/131352563
今日推荐