本文参考
PyTorch optim文档

1 概述

1.1 PyTorch文档:torch.optim解读

下图是optim的文档

TORCH.OPTIM

torch.optim is a package implementing various optimization algorithms. Most commonly used methods are already supported, and the interface is general enough, so that more sophisticated ones can be also easily integrated in the future.

torch.optim简介
torch.optim是PyTorch实现的一个包,里面有各种各样的优化算法,大部分常用的优化算法都已经被支持,接口也十分通用,所以可以用来集成实现更加复杂的系统。

How to use an optimizer

To use torch.optim you have to construct an optimizer object, that will hold the current state and will update the parameters based on the computed gradients.

如何使用PyTorch提供的optimizer
通过torch.optim来创建一个Optimizer对象,这个对象中会保存当前的状态,并且会根据计算的梯度值更新参数。

Constructing it

To construct an Optimizer you have to give it an iterable containing the parameters (all should be Variables) to optimize. Then, you can specify optimizer-specific options such as the learning rate, weight decay, etc.

NOTE

If you need to move a model to GPU via .cuda(), please do so before constructing optimizers for it. Parameters of a model after .cuda() will be different objects with those before the call.
In general, you should make sure that optimized parameters live in consistent locations when optimizers are constructed and used.

构造Optimizer
构造Optimizer时,需要传入一个包含需要进行优化的所有参数的iterable对象,所有参数都必须是Variables类型。随后可以进一步设置optimizer的其他具体参数,如learning rate, weight decay, etc.
注意:
如果需要将模型移到cuda上(通过.cuda命令),那么必须先移动模型,再对模型构造optimizer。因为调用.cuda前的模型参数与调用.cuda后的模型参数不同。
通常来讲,在使用Optimizer对参数进行优化时,需要保证构造和使用时,被优化的参数保存在同一位置。

以下是实例:

Example:

optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
optimizer = optim.Adam([var1, var2], lr=0.0001)

Per-parameter options

Optimizers also support specifying per-parameter options. To do this, instead of passing an iterable of Variables, pass in an iterable of dicts. Each of them will define a separate parameter group, and should contain a params key, containing a list of parameters belonging to it. Other keys should match the keyword arguments accepted by the optimizers, and will be used as optimization options for this group.

NOTE

You can still pass options as keyword arguments. They will be used as defaults, in the groups that didn’t override them. This is useful when you only want to vary a single option, while keeping all others consistent between parameter groups.

Per-parameter option
我个人翻译为逐参数选项。Optimizer在构造的时候同样支持对每个参数进行指定。要实现这种功能,我们不再传入一个含有Variable类型参数的iterable对象,而是传入一个dict字典类型的iterable对象。每个字典都定义了一个参数组,该参数组的key值是"params",而对应的值为一个包含参数的列表。同样的可以利用字典的键值对Optimizer的其他参数进行指定,但是key必须与Optimizer构造器传参时的关键字一致。这些指定的Optimizer的参数会被单独应用于该字典中的params这些参数。
注意:你仍然可以在构造器中以关键字方式传入参数,这些参数将被当做默认值使用,如果一组参数没有override这个参数,那么就将自动使用默认的参数值。
以下是实例:

For example, this is very useful when one wants to specify per-layer learning rates:

optim.SGD([{'params': model.base.parameters()},{'params': model.classifier.parameters(), 'lr': 1e-3}], lr=1e-2, momentum=0.9)

This means that model.base’s parameters will use the default learning rate of 1e-2, model.classifier’s parameters will use a learning rate of 1e-3, and a momentum of 0.9 will be used for all parameters.

对于上面这个例子,首先我们可以看到,传入了一个dict的列表。列表中有两个dict,第一个的paramskey 对应的是model.base.parameters(),而没有对Optimizer的其他参数进行具体指定。第二个dict的paramskey对应的是model.classifier.parameters(),此外还有一个键值对,说明了lr的值为1e-3。而在列表之外同时又传入了lr=1e-2,momentum=0.9,这两个值将作为默认值来使用。所以整个Optimizer中,base's parameters将使用默认的学习率1e-2,默认的动量超参数0.9;而classifier.parameters()将使用其dict中提供的学习率1e-3,momentum仍然使用默认值。

Taking an optimization step

All optimizers implement a step() method, that updates the parameters. It can be used in two ways:

optimizer.step()

This is a simplified version supported by most optimizers. The function can be called once the gradients are computed using e.g. backward().

采取优化步骤
所有的Optimizer都实现了step()方法,该方法可以用于更新参数。可以通过两种方式使用.step()进行优化:

第一种方式:optimizer.step()
该方法是一个简化后的版本,被大多数optimizer所支持。该函数一般在所有梯度值被更新(或者被计算)后进行调用,如在.backward()后进行调用。

以下是例子:

Example:

for input, target in dataset:optimizer.zero_grad()output = model(input)loss = loss_fn(output, target)loss.backward()optimizer.step()

optimizer.step(closure)

Some optimization algorithms such as Conjugate Gradient and LBFGS need to reevaluate the function multiple times, so you have to pass in a closure that allows them to recompute your model. The closure should clear the gradients, compute the loss, and return it.

第二种方式:optimizer.step(closure)
有一些优化算法例如Conjugate GradientLBFGS等需要多次重新计算函数,所以需要传入一个闭包closure,闭包中应该实现的操作有:清零梯度,计算损失并返回。
以下是例子:

Example:

for input, target in dataset:def closure():optimizer.zero_grad()output = model(input)loss = loss_fn(output, target)loss.backward()return lossoptimizer.step(closure)

具体的各个优化算法的数学原理在此不表,详参手写的笔记本。

2 如何调整学习率

torch.optim.lr_scheduler模块,提供了一些根据训练次数来调整学习率(learning rate)的方法,一般情况下我们会设置随着epoch的增大而逐渐减小学习率,从而达到更好的训练效果。
torch.optim.lr_scheduler.ReduceLROnPlateau提供了一些基于训练中某些测量值使得学习率动态下降的办法。

学习率的调整应该放在optimizer更新之后,参考模板:

define scheduler
for epoch in range(1000):train(...)validate(...)scheduler.step()

注意: 在PyTorch 1.1.0之前的版本,学习率的调整应该被放在optimizer更新之前,如果我们1.1.0之后仍然将学习率的调整(即scheduler.step())放在optimizer’s update(即optimizer.step)之前,那么learning rate schedule的第一个值将被跳过。所以如果某个代码是在1.1.0之前的版本开发,移植到高版本进行运行,发现效果变差,可以检查是否将scheduler.step()放在了optimizer.step之前。

注:以上部分参考官方文档批示。

2.1 torch.optim.lr_scheduler.StepLR

首先贴上官方文档:

torch.optim.lr_scheduler.StepLR(optimizer, step_size, gamma=0.1, last_epoch=-1, verbose=False)

Decays the learning rate of each parameter group by gamma every step_size epochs. Notice that such decay can happen simultaneously with other changes to the learning rate from outside this scheduler. When last_epoch=-1, sets initial lr as lr.

StepLR可以根据超参数gamma每隔固定的step_size就衰减learning_rate一次。需要说明的是,这种对learning_rate的更新可以与外界的其他变化同时进行。当last_epoch = -1时,将lr置为初始值。

Parameters

  • optimizer (Optimizer) – Wrapped optimizer.
  • step_size (int) – Period of learning rate decay.
  • gamma (float) – Multiplicative factor of learning rate decay. Default: 0.1.
  • last_epoch (int) – The index of last epoch. Default: -1.
  • verbose (bool) – If True, prints a message to stdout for each update. Default: False.

参数说明

  • optimizer(Optimizer) -----用于指定scheduler的应用对象。
  • step_size(int)-----用于指定步长,即几次迭代之后进行一次decay
  • gamma(float)-----学习率衰减的乘法因子,默认值为0.1
  • last_epoch(int)-----更新的边界index,当等于这个值的时候,重置lr,默认为-1
  • verbose(bool)-----如果为True,每次decay会向stdout输出一条信息。默认为false.

以下是实例:

Example

# Assuming optimizer uses lr = 0.05 for all groups
# lr = 0.05     if epoch < 30
# lr = 0.005    if 30 <= epoch < 60
# lr = 0.0005   if 60 <= epoch < 90
# ...
scheduler = StepLR(optimizer, step_size=30, gamma=0.1)
for epoch in range(100):train(...)validate(...)scheduler.step()

可见:每经过一个step_size

lr=lr∗gammalr = lr*gammalr=lr∗gamma


如果觉得文章对您有帮助的话,可以点个赞,是对博主最大的肯定!

PyTorch学习笔记:torch.optim---Optimizer以及lr_scheduler相关推荐

  1. pytorch torch.optim.Optimizer

    API 1)Optimizer CLASS torch.optim.Optimizer(params, defaults) 参数 描述 params (iterable) an iterable of ...

  2. PyTorch学习笔记2:nn.Module、优化器、模型的保存和加载、TensorBoard

    文章目录 一.nn.Module 1.1 nn.Module的调用 1.2 线性回归的实现 二.损失函数 三.优化器 3.1.1 SGD优化器 3.1.2 Adagrad优化器 3.2 分层学习率 3 ...

  3. PyTorch学习笔记(二)——回归

    PyTorch学习笔记(二)--回归 本文主要是用PyTorch来实现一个简单的回归任务. 编辑器:spyder 1.引入相应的包及生成伪数据 import torch import torch.nn ...

  4. pytorch 学习笔记目录

    1 部分内容 pytorch笔记 pytorch模型中的parameter与buffer_刘文巾的博客-CSDN博客 pytorch学习笔记 torchnn.ModuleList_刘文巾的博客-CSD ...

  5. 深度学习入门之PyTorch学习笔记:多层全连接网络

    深度学习入门之PyTorch学习笔记 绪论 1 深度学习介绍 2 深度学习框架 3 多层全连接网络 3.1 PyTorch基础 3.2 线性模型 3.2.1 问题介绍 3.2.2 一维线性回归 3.2 ...

  6. pytorch学习笔记(2):在MNIST上实现一个CNN

    参考文档:https://mp.weixin.qq.com/s/1TtPWYqVkj2Gaa-3QrEG1A 这篇文章是在一个大家经常见到的数据集 MNIST 上实现一个简单的 CNN.我们会基于上一 ...

  7. Pytorch学习笔记总结

    往期Pytorch学习笔记总结: 1.Pytorch实战笔记_GoAI的博客-CSDN博客 2.Pytorch入门教程_GoAI的博客-CSDN博客 Pytorch系列目录: PyTorch学习笔记( ...

  8. PyTorch学习笔记(六):PyTorch进阶训练技巧

    PyTorch实战:PyTorch进阶训练技巧 往期学习资料推荐: 1.Pytorch实战笔记_GoAI的博客-CSDN博客 2.Pytorch入门教程_GoAI的博客-CSDN博客 本系列目录: P ...

  9. PyTorch学习笔记(四):PyTorch基础实战

    PyTorch实战:以FashionMNIST时装分类为例: 往期学习资料推荐: 1.Pytorch实战笔记_GoAI的博客-CSDN博客 2.Pytorch入门教程_GoAI的博客-CSDN博客 本 ...

  10. PyTorch学习笔记(三):PyTorch主要组成模块

    往期学习资料推荐: 1.Pytorch实战笔记_GoAI的博客-CSDN博客 2.Pytorch入门教程_GoAI的博客-CSDN博客 本系列目录: PyTorch学习笔记(一):PyTorch环境安 ...

最新文章

  1. 接口测试——postman
  2. flink scala shell命令行使用示例
  3. php采集url,PHP-如何采集这个url跳转内容呢
  4. php mysql 函数总结
  5. python一款神器:ptpython 安装
  6. 如何修改influxdb表结构_influxdb的基本使用
  7. Using Flume要点
  8. MySQL数据库优化的八种方式(经典必看)
  9. 新版万能声卡驱动-VoodooHDA-2.8.5
  10. Clang checker类总结
  11. vs2019专业版本 vtk安装
  12. 人脸识别门禁系统有哪些功能作用
  13. 编程初学者如何缓解迷茫和焦虑?墙裂推荐此文,助你赢在起跑线
  14. CDQZ多校集训题目--盛夏
  15. CGT Asia 2022第二届亚洲细胞与基因治疗创新峰会将于2022年7月10日-111日在上海举办
  16. Visual Studio Enterprise 2015简体中文版(企业版)秘钥
  17. 大数据时代的营销思维
  18. 产品经理的一些常用术语
  19. 本地存储-cookie|localStorage|sessionStorage|indexedDB
  20. php dsa算法,openssl_private_encrypt使用DSA算法生成的密钥无法加密

热门文章

  1. AJAX 状态值(readyState)与状态码(status)详解
  2. 打造×××互连无极限,多WAN口×××防火墙
  3. Spring ORM示例 - 带有AOP事务管理
  4. LSA类型讲解——LSA-2(第二类LSA——Network LSA)、LSA-3(第三类LSA——Network Summary LSA)详解
  5. npm切换淘宝源,yarn切换淘宝源
  6. centos7搭建easy-mock服务
  7. 高性能异步批量ping的golang实现
  8. AOP Aspect Oriented Programming 面向切面编程 Spring
  9. 【C语言】写一个函数,并调用该函数求两个整数的最大公约数和最小公倍数
  10. C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)