当前位置:网站首页>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
边栏推荐
- Make LIVELINK's initial pose consistent with that of the mobile capture actor
- Four items that should be included in the management system of integral mall
- Recursive method to construct binary tree from preorder and inorder traversal sequence
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- Empire CMS collection Empire template program general
- 解读创客思维与数学课程的实际运用
- Register of assembly language by Wang Shuang
- Implementation of replacement function of shell script
- Yugu p1020 missile interception (binary search)
猜你喜欢
使用 Nocalhost 开发 Rainbond 上的微服务应用
运放电路的反馈电阻上并联一个电容是什么作用
Myabtis_ Plus
Hisense TV starts the developer mode
uniapp 移动端强制更新功能
在 Rainbond 中一键安装高可用 Nacos 集群
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
Jmeter 的使用
Notes on PHP penetration test topics
随机推荐
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
积分商城管理系统中应包含的四大项
jeeSite 表单页面的Excel 导入功能
青龙面板--花花阅读
offer收割机:两个长字符串数字相加求和(经典面试算法题)
Myabtis_Plus
Content of string
Avatary的LiveDriver试用体验
buureservewp(2)
Bugku CTF daily one question chessboard with only black chess
Blob object introduction
Zcmu--1492: problem d (C language)
Implementation of replacement function of shell script
基于Pytorch 框架手动完成线性回归
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
Binary tree and heap building in C language
buureservewp(2)
UnityHub破解&Unity破解
Recursive method to verify whether a tree is a binary search tree (BST)
The largest 3 same digits in the string of leetcode simple question