从零开始学Pytorch(十三)之梯度下降

简介: 从零开始学Pytorch(十三)之梯度下降

梯度下降


%matplotlib inline
import numpy as np
import torch
import time
from torch import nn, optim
import math
import sys
sys.path.append('/home/input')
import d2lzh1981 as d2l

一维梯度下降


证明:沿梯度反方向移动自变量可以减小函数值

泰勒展开:

image.png

def f(x):
    return x**2  # Objective function
def gradf(x):
    return 2 * x  # Its derivative
def gd(eta):
    x = 10
    results = [x]
    for i in range(10):
        x -= eta * gradf(x)
        results.append(x)
    print('epoch 10, x:', x)
    return results
res = gd(0.2)
def show_trace(res):
    n = max(abs(min(res)), abs(max(res)))
    f_line = np.arange(-n, n, 0.01)
    d2l.set_figsize((3.5, 2.5))
    d2l.plt.plot(f_line, [f(x) for x in f_line],'-')
    d2l.plt.plot(res, [f(x) for x in res],'-o')
    d2l.plt.xlabel('x')
    d2l.plt.ylabel('f(x)')
show_trace(res)

5356fff406b23ad776e00cd70f6dc262.png

学习率


show_trace(gd(0.05))

fcc80ee559039aa69ec4b599b5e72fe0.png

局部极小值


c = 0.15 * np.pi
def f(x):
    return x * np.cos(c * x)
def gradf(x):
    return np.cos(c * x) - c * x * np.sin(c * x)
show_trace(gd(2))

4972178069a2a7362f9ab236104712a3.png

多维梯度下降


def train_2d(trainer, steps=20):
    x1, x2 = -5, -2
    results = [(x1, x2)]
    for i in range(steps):
        x1, x2 = trainer(x1, x2)
        results.append((x1, x2))
    print('epoch %d, x1 %f, x2 %f' % (i + 1, x1, x2))
    return results
def show_trace_2d(f, results): 
    d2l.plt.plot(*zip(*results), '-o', color='#ff7f0e')
    x1, x2 = np.meshgrid(np.arange(-5.5, 1.0, 0.1), np.arange(-3.0, 1.0, 0.1))
    d2l.plt.contour(x1, x2, f(x1, x2), colors='#1f77b4')
    d2l.plt.xlabel('x1')
    d2l.plt.ylabel('x2')
    eta = 0.1
def f_2d(x1, x2):  # 目标函数
    return x1 ** 2 + 2 * x2 ** 2
def gd_2d(x1, x2):
    return (x1 - eta * 2 * x1, x2 - eta * 4 * x2)
show_trace_2d(f_2d, train_2d(gd_2d))

d31c55c00a0034b84a6e0c018a4b2d42.png

自适应方法


牛顿法


image.png

c = 0.5
def f(x):
    return np.cosh(c * x)  # Objective
def gradf(x):
    return c * np.sinh(c * x)  # Derivative
def hessf(x):
    return c**2 * np.cosh(c * x)  # Hessian
# Hide learning rate for now
def newton(eta=1):
    x = 10
    results = [x]
    for i in range(10):
        x -= eta * gradf(x) / hessf(x)
        results.append(x)
    print('epoch 10, x:', x)
    return results
show_trace(newton())

fd44a98518e39342ec0d54426c292f78.png

收敛性分析


image.png

随机梯度下降参数更新


image.png

def f(x1, x2):
    return x1 ** 2 + 2 * x2 ** 2  # Objective
def gradf(x1, x2):
    return (2 * x1, 4 * x2)  # Gradient
def sgd(x1, x2):  # Simulate noisy gradient
    global lr  # Learning rate scheduler
    (g1, g2) = gradf(x1, x2)  # Compute gradient
    (g1, g2) = (g1 + np.random.normal(0.1), g2 + np.random.normal(0.1))
    eta_t = eta * lr()  # Learning rate at time t
    return (x1 - eta_t * g1, x2 - eta_t * g2)  # Update variables
eta = 0.1
lr = (lambda: 1)  # Constant learning rate
show_trace_2d(f, train_2d(sgd, steps=50))

b8704063735923b984c59a83e0cee6c4.png

动态学习率


def exponential():
    global ctr
    ctr += 1
    return math.exp(-0.1 * ctr)
ctr = 1
lr = exponential  # Set up learning rate
show_trace_2d(f, train_2d(sgd, steps=1000))

25a1f63d5f291a1ee119ae14c8db8457.png

小批量随机梯度下降


读取数据


读取数据

def get_data_ch7():  # 本函数已保存在d2lzh_pytorch包中方便以后使用
    data = np.genfromtxt('/home/kesci/input/airfoil4755/airfoil_self_noise.dat', delimiter='\t')
    data = (data - data.mean(axis=0)) / data.std(axis=0) # 标准化
    return torch.tensor(data[:1500, :-1], dtype=torch.float32), \
           torch.tensor(data[:1500, -1], dtype=torch.float32) # 前1500个样本(每个样本5个特征)
features, labels = get_data_ch7()
import pandas as pd
df = pd.read_csv('/home/kesci/input/airfoil4755/airfoil_self_noise.dat', delimiter='\t', header=None)
df.head(10)

347d3c15f8136095260c1760669ef478.png

从零开始实现


def sgd(params, states, hyperparams):
    for p in params:
        p.data -= hyperparams['lr'] * p.grad.data
# 本函数已保存在d2lzh_pytorch包中方便以后使用
def train_ch7(optimizer_fn, states, hyperparams, features, labels,
              batch_size=10, num_epochs=2):
    # 初始化模型
    net, loss = d2l.linreg, d2l.squared_loss
    w = torch.nn.Parameter(torch.tensor(np.random.normal(0, 0.01, size=(features.shape[1], 1)), dtype=torch.float32),
                           requires_grad=True)
    b = torch.nn.Parameter(torch.zeros(1, dtype=torch.float32), requires_grad=True)
    def eval_loss():
        return loss(net(features, w, b), labels).mean().item()
    ls = [eval_loss()]
    data_iter = torch.utils.data.DataLoader(
        torch.utils.data.TensorDataset(features, labels), batch_size, shuffle=True)
    for _ in range(num_epochs):
        start = time.time()
        for batch_i, (X, y) in enumerate(data_iter):
            l = loss(net(X, w, b), y).mean()  # 使用平均损失
            # 梯度清零
            if w.grad is not None:
                w.grad.data.zero_()
                b.grad.data.zero_()
            l.backward()
            optimizer_fn([w, b], states, hyperparams)  # 迭代模型参数
            if (batch_i + 1) * batch_size % 100 == 0:
                ls.append(eval_loss())  # 每100个样本记录下当前训练误差
    # 打印结果和作图
    print('loss: %f, %f sec per epoch' % (ls[-1], time.time() - start))
    d2l.set_figsize()
    d2l.plt.plot(np.linspace(0, num_epochs, len(ls)), ls)
    d2l.plt.xlabel('epoch')
    d2l.plt.ylabel('loss')
def train_sgd(lr, batch_size, num_epochs=2):
    train_ch7(sgd, None, {'lr': lr}, features, labels, batch_size, num_epochs)
train_sgd(1, 1500, 6)

d5b2d0f3ff4ba128f51cd884d6cd4a80.png

简洁实现


# 例如: optimizer_fn=torch.optim.SGD, optimizer_hyperparams={"lr": 0.05}
def train_pytorch_ch7(optimizer_fn, optimizer_hyperparams, features, labels,
                    batch_size=10, num_epochs=2):
    # 初始化模型
    net = nn.Sequential(
        nn.Linear(features.shape[-1], 1)
    )
    loss = nn.MSELoss()
    optimizer = optimizer_fn(net.parameters(), **optimizer_hyperparams)
    def eval_loss():
        return loss(net(features).view(-1), labels).item() / 2
    ls = [eval_loss()]
    data_iter = torch.utils.data.DataLoader(
        torch.utils.data.TensorDataset(features, labels), batch_size, shuffle=True)
    for _ in range(num_epochs):
        start = time.time()
        for batch_i, (X, y) in enumerate(data_iter):
            # 除以2是为了和train_ch7保持一致, 因为squared_loss中除了2
            l = loss(net(X).view(-1), y) / 2 
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            if (batch_i + 1) * batch_size % 100 == 0:
                ls.append(eval_loss())
    # 打印结果和作图
    print('loss: %f, %f sec per epoch' % (ls[-1], time.time() - start))
    d2l.set_figsize()
    d2l.plt.plot(np.linspace(0, num_epochs, len(ls)), ls)
    d2l.plt.xlabel('epoch')
    d2l.plt.ylabel('loss')
train_pytorch_ch7(optim.SGD, {"lr": 0.05}, features, labels, 10)

fd7a278fd617511293c7b864c9493cbb.png

参考文献


[1]《动手深度学习》李沐

[2]伯禹教育课程

相关文章
|
9月前
|
机器学习/深度学习 PyTorch 算法框架/工具
【从零开始学习深度学习】39. 梯度下降优化之动量法介绍及其Pytorch实现
【从零开始学习深度学习】39. 梯度下降优化之动量法介绍及其Pytorch实现
|
9月前
|
机器学习/深度学习 算法 PyTorch
【从零开始学习深度学习】38. Pytorch实战案例:梯度下降、随机梯度下降、小批量随机梯度下降3种优化算法对比【含数据集与源码】
【从零开始学习深度学习】38. Pytorch实战案例:梯度下降、随机梯度下降、小批量随机梯度下降3种优化算法对比【含数据集与源码】
|
9月前
|
机器学习/深度学习 算法 PyTorch
《PyTorch深度学习实践》--3梯度下降算法
《PyTorch深度学习实践》--3梯度下降算法
|
机器学习/深度学习 算法 PyTorch
【pytorch深度学习实践】笔记—03-1.梯度下降算法
【pytorch深度学习实践】笔记—03-1.梯度下降算法
248 0
【pytorch深度学习实践】笔记—03-1.梯度下降算法
|
机器学习/深度学习 并行计算 算法
【PyTorch基础教程3】梯度下降
在Pytorch基础教程1中我们用的是基于【穷举】的思想,但如果在多维的情况下(即多个参数),会引起维度诅咒现象。 现在我们利用【分治法】,先对整体采样分割,在相对最低点进一步采样。需要求解使loss最小时的参数取值:
146 0
【PyTorch基础教程3】梯度下降
|
机器学习/深度学习 并行计算 算法
【PyTorch基础教程3】梯度下降(学不会来打我啊)
在Pytorch基础教程1中我们用的是基于【穷举】的思想,但如果在多维的情况下(即多个参数),会引起维度诅咒现象。 现在我们利用【分治法】,先对整体采样分割,在相对最低点进一步采样。需要求解使loss最小时的参数取值:
289 0
【PyTorch基础教程3】梯度下降(学不会来打我啊)
|
2月前
|
机器学习/深度学习 搜索推荐 PyTorch
基于昇腾用PyTorch实现传统CTR模型WideDeep网络
本文介绍了如何在昇腾平台上使用PyTorch实现经典的WideDeep网络模型,以处理推荐系统中的点击率(CTR)预测问题。
220 66
|
15天前
|
机器学习/深度学习 算法 安全
用PyTorch从零构建 DeepSeek R1:模型架构和分步训练详解
本文详细介绍了DeepSeek R1模型的构建过程,涵盖从基础模型选型到多阶段训练流程,再到关键技术如强化学习、拒绝采样和知识蒸馏的应用。
145 3
用PyTorch从零构建 DeepSeek R1:模型架构和分步训练详解
|
5月前
|
算法 PyTorch 算法框架/工具
Pytorch学习笔记(九):Pytorch模型的FLOPs、模型参数量等信息输出(torchstat、thop、ptflops、torchsummary)
本文介绍了如何使用torchstat、thop、ptflops和torchsummary等工具来计算Pytorch模型的FLOPs、模型参数量等信息。
710 2
|
3月前
|
机器学习/深度学习 人工智能 PyTorch
Transformer模型变长序列优化:解析PyTorch上的FlashAttention2与xFormers
本文探讨了Transformer模型中变长输入序列的优化策略,旨在解决深度学习中常见的计算效率问题。文章首先介绍了批处理变长输入的技术挑战,特别是填充方法导致的资源浪费。随后,提出了多种优化技术,包括动态填充、PyTorch NestedTensors、FlashAttention2和XFormers的memory_efficient_attention。这些技术通过减少冗余计算、优化内存管理和改进计算模式,显著提升了模型的性能。实验结果显示,使用FlashAttention2和无填充策略的组合可以将步骤时间减少至323毫秒,相比未优化版本提升了约2.5倍。
113 3
Transformer模型变长序列优化:解析PyTorch上的FlashAttention2与xFormers

热门文章

最新文章