当前位置:网站首页>Pytoch (VI) -- model tuning tricks
Pytoch (VI) -- model tuning tricks
2022-07-07 08:12:00 【CyrusMay】
Pytorch( 6、 ... and ) —— Model tuning tricks
1. Regularization Regularization
1.1 L1 Regularization
import torch
import torch.nn.functional as F
from torch import nn
device=torch.device("cuda:0")
MLP = nn.Sequential(nn.Linear(128,64),
nn.ReLU(inplace=True),
nn.Linear(64,32),
nn.ReLU(inplace=True),
nn.Linear(32,10)
)
MLP.to(device)
loss_classify = nn.CrossEntropyLoss().to(device)
# L1 norm
l1_loss = 0
for param in MLP.parameters():
l1_loss += torch.sum(torch.abs(param))
loss = loss_classify+l1_loss
1.2 L2 Regularization
import torch
import torch.nn.functional as F
from torch import nn
device=torch.device("cuda:0")
MLP = nn.Sequential(nn.Linear(128,64),
nn.ReLU(inplace=True),
nn.Linear(64,32),
nn.ReLU(inplace=True),
nn.Linear(32,10)
)
MLP.to(device)
# L2 norm
opt = torch.optim.SGD(MLP.parameters(),lr=0.001,weight_decay=0.1) # adopt weight_decay Realization L2
loss = nn.CrossEntropyLoss().to(device)
2 Momentum and learning rate decay
2.1 momentum
opt = torch.optim.SGD(model.parameters(),lr=0.001,momentum=0.78,weight_decay=0.1)
2.2 learning rate tunning
- torch.optim.lr_scheduler.ReduceLROnPlateau() Use when the value of the loss function does not decrease
- torch.optim.lr_scheduler.StepLR() Reduce the learning rate according to a certain number of steps
opt = torch.optim.SGD(net.parameters(),lr=1)
lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer=opt,mode="min",factor=0.1,patience=10)
for epoch in torch.arange(1000):
loss_val = train(...)
lr_scheduler.step(loss_val) # monitor loss
opt = torch.optim.SGD(net.parameters(),lr=1)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer=opt,step_size=30,gamma=0.1)
for epoch in torch.arange(1000):
lr_scheduler.step() # monitor loss
train(...)
3. Early Stopping
4. Dropout
model = nn.Sequential(
nn.Linear(256,128),
nn.Dropout(p=0.5),
nn.ReLu(),
)
by CyrusMay 2022 07 03
边栏推荐
- JS copy picture to clipboard read clipboard
- 柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
- Pytorch(六) —— 模型调优tricks
- 复杂网络建模(三)
- 漏洞複現-Fastjson 反序列化
- Linux Installation MySQL 8.0 configuration
- JS quick start (I)
- [quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
- 复杂网络建模(一)
- Bayes' law
猜你喜欢
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
探索STEAM艺术设计中的创造力
机器人教育在动手实践中的真理
漏洞复现-easy_tornado
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
Application of slip ring of shipborne radar antenna
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
发挥创客教育空间的广泛实用性
复杂网络建模(一)
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
随机推荐
QT learning 26 integrated example of layout management
通俗易懂单点登录SSO
CDC (change data capture technology), a powerful tool for real-time database synchronization
ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
Recursive method to construct binary tree from preorder and inorder traversal sequence
运放电路的反馈电阻上并联一个电容是什么作用
在Rainbond中实现数据库结构自动化升级
利用 Helm 在各类 Kubernetes 中安装 Rainbond
漏洞复现-Fastjson 反序列化
Qinglong panel -- finishing usable scripts
Complex network modeling (III)
Hisense TV starts the developer mode
一文了解如何源码编译Rainbond基础组件
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
Avatary的LiveDriver试用体验
JSON data flattening pd json_ normalize
B. Value sequence thinking
Recursive method to verify whether a tree is a binary search tree (BST)
在Rainbond中一键部署高可用 EMQX 集群
uniapp 移动端强制更新功能