沐神深度学习笔记6

下面提供简单的线性回归的代码实现:

%matplotlib inline
import random
import torch
from d2l import torch as d2l

这一块没啥好说的

def synthetic_data(w, b, num_examples):  #@save
    """生成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)
    return X, y.reshape((-1, 1))

这一块,可以看到,定义了synthetic_data函数来生成数据集,其中,x由均值为0,方差为1的正态分布生成(这也是normal函数的作用),x的形状为num_examples*len(w),然后y是x和w的内积+b,再给y加上一个正态分布取值的噪音。最后通过reshape函数,返回列向量x,y。

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

把人工数据集w和b带入,即可得到features和labels的数据集。(即返回了x和y的线性关系)

 

 下面是读取数据集的代码:

def data_iter(batch_size, features, labels):
    num_examples = len(features)  
    #此处取features的一维的长度作为num_examples
    indices = list(range(num_examples))
    #此处使用range函数随机迭代,并且用list函数表示出来
    random.shuffle(indices)
    #使用此函数将list中信息打乱
    for i in range(0, num_examples, batch_size):
     #循环开始,i取0-num-1中隔batch_size取值
        batch_indices = torch.tensor(
            indices[i:min(i + batch_size, num_examples)])
            #从i开始,每次取一个i+batch_size,最大取到examples,化为一个向量,构成随 
            #机的batch_indices
        yield features[batch_indices], labels[batch_indices]
            #通过这些随机,产生特征(features)和标号(labels),yield类似于renturn

上面的就是一个迭代器的封装。(indices[i:i + batch_size]一次取batch_size个数)

下面开始初始化参数模型:

requires_grad=True用于计算梯度,偏差b直接给标量1。

定义模型:

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 ):
    #params:list(包含了w,b),lr:学习率,batch_size:采样间隔
    """小批量随机梯度下降"""
    with torch.no_grad():
         #此处不参与梯度计算,此处计算出的值显然不用计算梯度
         for param in params:
             param -= lr * param.grad / batch_size
             #之前损失函数没球均值,所以这里除以batch_size
             param.grad.zreo_()
             #pytorch不会清空梯度,要手动清空
​

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值