诚信为本,市场在变,诚信永远不变...

高德资讯

【Pytorch教程】使用lr_scheduler调整学习率

模块提供了一些根据 迭代次数来调整学习率 的方法。为了能够让损失函数最终达到收敛的效果,通常 随着迭代次数的增加而减小时能够得到较好的效果。 则提供了基于训练中某些测量值使学习率动态下降的方法。

学习率的调整应该放在optimizer更新之后,下面是Demo示例:

 

之前, 学习率更新操作 会在 操作之前调用; 修改了这种调用机制。如果在 之前调用 , 会自动跳过第一次 的更新。如果更新了 后您的结果不一样了,请确认是不是在这里的调用顺序有误。

为了进一步说明 的机制,我们首先需要了解一下 的结构,以 为例(所有 都继承自 类)。
对于

  • params (iterable):需要优化的网络参数,传进来的网络参数必须是Iterable。
  • 优化一个网络,网络的每一层看做一个parameter group,一整个网络就是parameter groups(一般给赋值为net.parameters()——generator的字典);
  • 优化多个网络,有两种方法:
  • 多个网络的参数合并到一起,形如[*net_1.parameters(), *net_2.parameters()]或itertools.chain(net_1.parameters(), net_2.parameters());
  • 当成多个网络优化,让多个网络的学习率各不相同,形如[{‘params’: net_1.parameters()}, {‘params’: net_2.parameters()}]
  • lr (float, optional):学习率;
  • betas (Tuple[float, float], optional) – coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999));
  • eps (float, optional) – term added to the denominator to improve numerical stability (default: 1e-8);
  • weight_decay (float, optional) – weight decay (L2 penalty) (default: 0);
  • amsgrad (boolean, optional) – whether to use the AMSGrad variant of this algorithm from the paper On the Convergence of Adam and Beyond (default: False)。

optimizer的属性

  • : 字典,存放这个优化器的一些初始参数,有:。
  • :列表,每个元素都是一个字典,每个元素包含的关键字有:,类是各个网络的参数放在了一起。
 

更新的,是更新的,而不是

以lambdaLR为例:

  • optimizer (Optimizer) – Wrapped optimizer.
  • lr_lambda (function or list) – A function which computes a multiplicative factor given an integer parameter epoch, or a list of such functions, one for each group in optimizer.param_groups.
  • last_epoch (int) – The index of last epoch. Default: -1.
  • verbose (bool) – If True, prints a message to stdout for each update. Default: False.

Demo示例:

 

实例函数

  • get_last_lr()

返回上次计算的

  • print_lr(is_verbose, group, lr, epoch=None)

打印当前

  • state_dict()

Returns the state of the scheduler as a dict.
It contains an entry for every variable in self.dict which is not the optimizer. The learning rate lambda functions will only be saved if they are callable objects and not if they are functions or lambdas.
When saving or loading the scheduler, please make sure to also save or load the state of the optimizer.

  • load_state_dict(state_dict)

Loads the schedulers state.
When saving or loading the scheduler, please make sure to also save or load the state of the optimizer.

后期会写一个完整的训练代码出来,给后面的项目进行参考

Pytorch官方文档
csdn中关于lr_scheduler用法的详细介绍

栏目导航

高德资讯

联系我们

CONTACT US

电话:13988888888

传 真:海南省海口市

手 机:0898-66889888

邮 箱:88889999

地 址:/public/upload/system/2018/07/28/2091301cca30ff8c6fd3ecd09c8d4b02.jpg

平台注册入口