「Deep Learning」Adam
2017-12-09 14:42
555 查看
Sina Weibo:小锋子Shawn
Tencent E-mail:403568338@qq.com
http://blog.csdn.net/dgyuanshaofeng/article/details/78759165
Adam,随机优化的算法之一,在TensorFlow和Pytorch中常用,在早期深度学习里面,我们使用Caffe还是常用SGD。也有道听途说,Adam跑通网络之后,应该使用SGD再跑一次,也就是认为SGD收敛的解好于Adam的解,但是Adam可以快速验证网络是否可用。
Pytorch使用的Adam,其默认参数和论文给出的推荐参数基本一致。也就是,学习率lr为0.001,beta1为0.9,beta2为0.999,eps为1e-08。另外,默认不使用L2惩罚,也就是不使用weight
decay。bete1为计算运行平均梯度的系数,而beta1为计算这个梯度的平方(square)的系数。
torch.optim.adam的源代码。
Tencent E-mail:403568338@qq.com
http://blog.csdn.net/dgyuanshaofeng/article/details/78759165
Adam,随机优化的算法之一,在TensorFlow和Pytorch中常用,在早期深度学习里面,我们使用Caffe还是常用SGD。也有道听途说,Adam跑通网络之后,应该使用SGD再跑一次,也就是认为SGD收敛的解好于Adam的解,但是Adam可以快速验证网络是否可用。
Pytorch使用的Adam,其默认参数和论文给出的推荐参数基本一致。也就是,学习率lr为0.001,beta1为0.9,beta2为0.999,eps为1e-08。另外,默认不使用L2惩罚,也就是不使用weight
decay。bete1为计算运行平均梯度的系数,而beta1为计算这个梯度的平方(square)的系数。
torch.optim.adam的源代码。
import math import torch from .optimizer import Optimizer class Adam(Optimizer): """Implements Adam algorithm. It has been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) 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) .. _Adam\: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 """ def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0): defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) super(Adam, self).__init__(params, defaults) def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data if grad.is_sparse: raise RuntimeError('Adam does not support sparse gradients, please consider SparseAdam instead') state = self.state[p] # State initialization if len(state) == 0: state['step'] = 0 # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p.data) # Exponential moving average of squared gradient values state['exp_avg_sq'] = torch.zeros_like(p.data) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] state['step'] += 1 if group['weight_decay'] != 0: grad = grad.add(group['weight_decay'], p.data) # Decay the first and second moment running average coefficient exp_avg.mul_(beta1).add_(1 - beta1, grad) exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) denom = exp_avg_sq.sqrt().add_(group['eps']) bias_correction1 = 1 - beta1 ** state['step'] bias_correction2 = 1 - beta2 ** state['step'] step_size = group['lr'] * math.sqrt(bias_correction2) / bias_correction1 p.data.addcdiv_(-step_size, exp_avg, denom) return loss源代码的说明。继承父类Optimizer。__init__方法为默认初始化,可见这里说明了如何使用Adam。其中,defaults将参数打包了,params为需要优化的参数列表/矩阵。
相关文章推荐
- Deep Learning 最优化方法之Adam
- 深度学习(Deep Learning)综述
- 浅谈深度学习(Deep Learning)的基本思想和方法
- Deep learning:三(Multivariance Linear Regression练习)
- 机器学习——深度学习(Deep Learning)
- Deep Learning(深度学习)学习笔记整理系列之(五)
- Deep learning:二十八(使用BP算法思想求解Sparse coding中矩阵范数导数)
- Deep Learning(深度学习)学习笔记整理系列之(七)
- Deep learning:四(logistic regression练习)
- Deep learning:十八(关于随机采样)
- deep learning 资料
- (转)机器学习——深度学习(Deep Learning)
- 【deep learning学习笔记】最近读的几个ppt(四)
- 【deep learning学习笔记】注释yusugomori的RBM代码 --- 头文件
- 【deep learning学习笔记】注释yusugomori的DA代码 --- dA.cpp -- 模型测试
- 【deep learning学习笔记】注释yusugomori的SDA代码 -- 准备工作
- 深度学习(Deep Learning)算法简介
- Deep Learning(深度学习)学习笔记整理系列之(三)
- Deep Learning
- Deep Learning论文笔记之(八)Deep Learning最新综述