(原)模型的参数初始化
转载请注明出处:
http://www.cnblogs.com/darkknightzh/p/8297793.html
参考网址:
http://pytorch.org/docs/0.3.0/nn.html?highlight=kaiming#torch.nn.init.kaiming_normal
https://github.com/prlz77/ResNeXt.pytorch/blob/master/models/model.py
https://github.com/facebookresearch/ResNeXt/blob/master/models/resnext.lua
https://github.com/bamos/densenet.pytorch/blob/master/densenet.py
https://github.com/szagoruyko/wide-residual-networks/blob/master/models/utils.lua
说明:暂时就这么多吧,错误之处请见谅。前两个初始化的方法见pytorch官方文档
1. xavier初始化
torch.nn.init.xavier_uniform(tensor, gain=1)
对于输入的tensor或者变量,通过论文Understanding the difficulty of training deep feedforward neural networks” - Glorot, X. & Bengio, Y. (2010)的方法初始化数据。初始化服从均匀分布$U(-a,a)$,其中$a=gain\times \sqrt{2/(fan\_in+fan\_out)}\times \sqrt{3}$,该初始化方法也称Glorot initialisation。
参数:
tensor:n维的 torch.Tensor 或者 autograd.Variable类型的数据
a:可选择的缩放参数
例如:
w = torch.Tensor(3, 5) nn.init.xavier_uniform(w, gain=nn.init.calculate_gain('relu'))
torch.nn.init.xavier_normal(tensor, gain=1)
对于输入的tensor或者变量,通过论文Understanding the difficulty of training deep feedforward neural networks” - Glorot, X. & Bengio, Y. (2010)的方法初始化数据。初始化服从高斯分布$N(0,std)$,其中$std=gain\times \sqrt{2/(fan\_in+fan\_out)}$,该初始化方法也称Glorot initialisation。
参数:
tensor:n维的 torch.Tensor 或者 autograd.Variable类型的数据
a:可选择的缩放参数
例如:
w = torch.Tensor(3, 5)
nn.init.xavier_normal(w)
2. kaiming初始化
torch.nn.init.kaiming_uniform(tensor, a=0, mode='fan_in')
对于输入的tensor或者变量,通过论文“Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification” - He, K. et al. (2015)的方法初始化数据。初始化服从均匀分布$U(-bound,bound)$,其中$bound=\sqrt{2/((1+{{a}^{2}})\times fan\_in)}\times \sqrt{3}$,该初始化方法也称He initialisation。
参数:
tensor:n维的 torch.Tensor 或者 autograd.Variable类型的数据
a:该层后面一层的激活函数中负的斜率(默认为ReLU,此时a=0)
mode:‘fan_in’ (default) 或者 ‘fan_out’. 使用fan_in保持weights的方差在前向传播中不变;使用fan_out保持weights的方差在反向传播中不变。
例如:
w = torch.Tensor(3, 5) nn.init.kaiming_uniform(w, mode='fan_in')
torch.nn.init.kaiming_normal(tensor, a=0, mode='fan_in')
对于输入的tensor或者变量,通过论文“Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification” - He, K. et al. (2015)的方法初始化数据。初始化服从高斯分布$N(0,std)$,其中$std=\sqrt{2/((1+{{a}^{2}})\times fan\_in)}$,该初始化方法也称He initialisation。
参数:
tensor:n维的 torch.Tensor 或者 autograd.Variable类型的数据
a:该层后面一层的激活函数中负的斜率(默认为ReLU,此时a=0)
mode:‘fan_in’ (default) 或者 ‘fan_out’. 使用fan_in保持weights的方差在前向传播中不变;使用fan_out保持weights的方差在反向传播中不变。
例如:
w = torch.Tensor(3, 5) nn.init.kaiming_normal(w, mode='fan_out')
使用的例子(具体参见原始网址):
https://github.com/prlz77/ResNeXt.pytorch/blob/master/models/model.py
from torch.nn import init self.classifier = nn.Linear(self.stages[3], nlabels) init.kaiming_normal(self.classifier.weight) for key in self.state_dict(): if key.split('.')[-1] == 'weight': if 'conv' in key: init.kaiming_normal(self.state_dict()[key], mode='fan_out') if 'bn' in key: self.state_dict()[key][...] = 1 elif key.split('.')[-1] == 'bias': self.state_dict()[key][...] = 0
3. 实际使用中看到的初始化
3.1 ResNeXt,densenet中初始化
https://github.com/facebookresearch/ResNeXt/blob/master/models/resnext.lua
https://github.com/bamos/densenet.pytorch/blob/master/densenet.py
conv
n = kW* kH*nOutputPlane weight:normal(0,math.sqrt(2/n)) bias:zero()
batchnorm
weight:fill(1) bias:zero()
linear
bias:zero()
3.2 wide-residual-networks中初始化(MSRinit)
https://github.com/szagoruyko/wide-residual-networks/blob/master/models/utils.lua
conv
n = kW* kH*nInputPlane weight:normal(0,math.sqrt(2/n)) bias:zero()
linear
bias:zero()
相关文章
- 并发编程的七个模型
- 基于事件驱动的领域模型实现框架 - 分析框架如何解决各种典型业务逻辑场景
- JVM调优:运行参数,内存模型,mat、jps、jstat、jmap、jstack、jvisualvm工具的使用
- DL之模型调参:深度学习算法模型优化参数之对LSTM算法进行超参数调优
- ML之ME/LF:机器学习中回归预测模型评估指标之“调整确定系数R2”的简介、代码实现之详细攻略
- ML之回归预测:利用九大类机器学习算法对无人驾驶系统参数(2018年的data,18+2)进行回归预测+评估九种模型性能
- EL之Boosting之GB(DTR):简单回归问题使用梯度提升法(DIY数据集+DTR模型+调两参)
- DL之模型调参:深度学习算法模型优化参数之对LSTM算法进行超参数调优
- 风电随机性动态经济调度模型(Python&Matlab代码)
- 网格搜索多个监督学习模型上的超参数,包括神经网络、随机森林和树集合模型(Matlab代码实现)
- 【华为云技术分享】从软件开发到 AI 领域工程师:模型训练篇
- 30亿参数,华为云发布全球最大预训练模型,开启工业化AI开发新模式
- Python实现贝叶斯优化器(Bayes_opt)优化Catboost分类模型(CatBoostClassifier算法)项目实战
- Python实现基于Optuna超参数自动优化的LightGBM分类模型(LGBMClassifier算法)项目实战
- Python实现基于Optuna超参数自动优化的xgboost分类模型(XGBClassifier算法)项目实战
- Python实现基于Optuna超参数自动优化的Catboost回归模型(CatBoostRegressor算法)项目实战
- Python实现ISSA融合反向学习与Levy飞行策略的改进麻雀优化算法优化支持向量机回归模型(SVR算法)项目实战
- C++ MVC模型的简单实现
- PyTorch查看模型和数据是否在GPU上
- Pytorch网络模型转Onnx格式,多种方法(opencv、onnxruntime、c++)调用Onnx
- 使用TPT对ASCET模型测试及代码覆盖度测试
- SMOTE RF MLP demo use cross_val_score to find best argument 处理不平衡数据的demo代码 先做smote处理 再用交叉验证找到最好的模型参数 实践表明MLP更好
- 【元胞自动机】格子波尔兹曼模型研究(Matlab代码实现)
- 机器人C++库(4)Robotics Library 例程一之DH模型参数及四元数、变换矩阵