pytorch给同一个layer的weight和bias设置不同的学习速率
2018-03-19 16:53
309 查看
经过在pytorch论坛的提问https://discuss.pytorch.org/t/how-to-set-different-learning-rate-for-weight-and-bias-in-one-layer/13450,现在总结如下:
1.使用dict简单粗暴设置,适用于层数较少的模型
若使用nn.Sequential可能会出现问题net.layer1.0.weight报语法错误,这时候使用net.layer1[0].weight代替即可。
2.使用代码生成dict列表,适用于层数多的layer
例子参考github上wkentaro/pytorch-fcn/blob/master/examples/voc/train_fcn32s.py#L105
其中函数get_parameter()来生成dict
应该注意根据情况调整modules_skipped的内容,应用方法如下:
但这两个方法都有一个问题,当我的学习速率要随着训练改变的时候,比如step,还不知道要怎么重新加载学习速率,现在是简单粗暴手动修改+load前一次的训练结果来进行的,如何实现类似caffe自动step学习速率的功能,目前没有头绪,如果谁有头绪,求教。
1.使用dict简单粗暴设置,适用于层数较少的模型
import torch import torch.nn as nn import torch.optim as optim from torch.autograd import Variable class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.layer = nn.Linear(1, 1) self.layer.weight.data.fill_(1) self.layer.bias.data.fill_(1) def forward(self, x): return self.layer(x) if __name__=="__main__": net = Net() optimizer = optim.Adam([ {'params': net.layer.weight}, {'params': net.layer.bias, 'lr': 0.01} ], lr=0.1, weight_decay=0.0001) out = net(Variable(torch.Tensor([[1]]))) out.backward() optimizer.step() print("weight", net.layer.weight.data.numpy(), "grad", net.layer.weight.grad.data.numpy()) print("bias", net.layer.bias.data.numpy(), "grad", net.layer.bias.grad.data.numpy())
若使用nn.Sequential可能会出现问题net.layer1.0.weight报语法错误,这时候使用net.layer1[0].weight代替即可。
2.使用代码生成dict列表,适用于层数多的layer
例子参考github上wkentaro/pytorch-fcn/blob/master/examples/voc/train_fcn32s.py#L105
其中函数get_parameter()来生成dict
def get_parameters(model, bias=False): import torch.nn as nn modules_skipped = ( nn.ReLU, nn.MaxPool2d, nn.Dropout2d, nn.Sequential, torchfcn.models.FCN32s, torchfcn.models.FCN16s, torchfcn.models.FCN8s, ) for m in model.modules(): if isinstance(m, nn.Conv2d): if bias: yield m.bias else: yield m.weight elif isinstance(m, nn.ConvTranspose2d): # weight is frozen because it is just a bilinear upsampling if bias: assert m.bias is None elif isinstance(m, modules_skipped): continue else: raise ValueError('Unexpected module: %s' % str(m))
应该注意根据情况调整modules_skipped的内容,应用方法如下:
optimizer = optim.SGD([ {'params': get_parameters(model, bias=False)}, {'params': get_parameters(model, bias=True), 'lr': opt.lr * 0.1} ], lr=opt.lr, weight_decay=opt.weight_decay)
但这两个方法都有一个问题,当我的学习速率要随着训练改变的时候,比如step,还不知道要怎么重新加载学习速率,现在是简单粗暴手动修改+load前一次的训练结果来进行的,如何实现类似caffe自动step学习速率的功能,目前没有头绪,如果谁有头绪,求教。
相关文章推荐
- 深入理解TextView实现Rich Text--在同一个TextView设置不同字体风格
- Ubuntu系统学习笔记-7.Tomcat下设置https的一个小问题
- Deep Learning 32: 自己写的keras的一个callbacks函数,解决keras中不能在每个epoch实时显示学习速率learning rate的问题
- 对同一个TextView设置不同的颜色、字体
- 关于一个Textview内的文字设置不同大小和颜色
- 一个算法习题学习的好去处-有不同语言的实现
- 一个长文档里,包括封面、不同的章节,如果我想封面不设置页眉页脚,每个章节的页眉都不同,请问应该如何设置页眉页脚?
- android学习中关于Textview,Button,EditText,连接的设置,Intent,Activity不同状态等的一系列基础知识点
- 如何给一个Label设置不同的文字属性
- [转]Android中的一个TextView中的字体设置不同大小
- 一个Text,设置不同的颜色,有颜色部分添加点击事件
- iOS同一个label中设置不同的字体及颜色
- 同一个tomcat APP中的 Log4j 的App root System property 不能相同,需要设置不同,不然会出现以下错误。解决方法在错误中标明了。
- ServerSocket的一个简单例子,学习backlog参数的设置,即可以接受客户端的数量
- 分享一个NS3学习思路(ARP发送时间间隔、发送次数修改设置)
- Android学习之将一个Activity设置main Activity
- NS2学习笔记 在一个节点上同时使用不同的应用层协议
- Android之同一个TextView设置不同样式的文字
- 操作系统学习笔记(19)--贴一个内存设置的代码
- 用TextView实现Rich Text---在同一个TextView中设置不同的字体风格