当前位置:网站首页>pytorch 笔记:validation ,model.eval V.S torch.no_grad
pytorch 笔记:validation ,model.eval V.S torch.no_grad
2022-06-30 10:03:00 【UQI-LIUWJ】
1 validation的一般框架
模型为model,优化器为optimizer
min_val_loss = np.inf
for epoch in range(1, epochs + 1):
############################训练部分开始#############################
model.train()
train_losses = []
for (batch_x, batch_y) in train_loader:
output = model(batch_x)
loss = criterion(output, batch_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
#pytorch 老三样
############################训练部分结束#############################
############################验证部分开始#############################
model.eval()
for (batch_x, batch_y) in val_loader:
with torch.no_grad():
output = model(batch_x)
loss = criterion(output, batch_y)
val_losses.append(loss.item())
val_loss = np.mean(val_losses)
if val_loss < min_val_loss:
min_val_loss = val_loss
torch.save(model.state_dict(), save_path)
#保存最优模型
############################验证部分结束#############################
test的时候,就可以加载这个最佳模型对应的参数(model.load_state_dict),进行测试
2 model.eval() V,S with torch.no_grad()
2.1 相同点
在PyTorch中进行validation时,使用他们均可切换到测试模式。
eg,对于dropout层和batchnorm层:
- 在train模式下,dropout网络层会按照设定的参数p设置保留激活单元的概率(保留概率=p); batchnorm层会继续计算数据的mean和var等参数并更新。
- 在val模式下,dropout层会让所有的激活单元都通过,而batchnorm层会停止计算和更新mean和var,直接使用在训练阶段已经学出的mean和var值。
2.2 不同点
- model.eval()仍然会涉及gradient的计算和存储,与training模式一样,只是不进行反向传播。
**with torch.zero_grad()**则停止autograd模块的工作,也就是停止gradient计算,以起到加速和节省显存的作用,从而节省了GPU算力和显存,但是并不会影响dropout和batchnorm层的行为。(这俩还是train模式下的样子)
——>二者搭配使用
边栏推荐
- 苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...
- Circuit breaker hystrixcircuitbreaker
- Migrate full RT thread to gd32f4xx (detailed)
- CSDN blog operation team 2022 H1 summary
- Yixian e - commerce publie un rapport trimestriel: adhérer à la R & D et à l’investissement de la marque, réaliser un développement durable et de haute qualité
- MATLAB image histogram equalization, namely spatial filtering
- A brief introduction to database mysql
- The performance of arm's new CPU has been improved by 22%, up to 12 cores can be combined, and the GPU is first equipped with hardware optical tracking. Netizen: the gap with apple is growing
- Apple's 5g chip was revealed to have failed in research and development, and the QQ password bug caused heated discussion. Wei Lai responded to the short selling rumors. Today, more big news is here
- The programmer was beaten.
猜你喜欢
苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...
MySQL advanced SQL statement of database (2)
Yixian e - commerce publie un rapport trimestriel: adhérer à la R & D et à l’investissement de la marque, réaliser un développement durable et de haute qualité
Configure Yii: display MySQL extension module verification failed
Auto Seg-Loss: 自动损失函数设计
从0使用keil5软件仿真调试GD32F305
19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
记一次实习的经历,趟坑必备(一)
MySQL log management, backup and recovery of databases (1)
Deployment of efficient and versatile clusters lvs+kept highly available clusters
随机推荐
Typescript – classes in Es5, inheritance, static methods
Implementation of monitor program with assembly language
7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
超长干货 | Kubernetes命名空间详解
Gd32 RT thread flash driver function
Leetcode question brushing (IV) -- greedy thought (go Implementation)
Compare the maximum computing power of the Cenozoic top ant s19xp and the existing s19pro in bitland
ArcGIS Pro + PS 矢量化用地规划图
mysql数据库基础:视图、变量
R语言plotly可视化:使用plotly可视化多分类模型的预测置信度、模型在2D网格中每个数据点预测的置信度、置信度定义为在某一点上最高分与其他类别得分之和之间的差值
Smith chart view of semi steel coaxial RF line and RF line matching calibration of network analyzer e5071c
What is the real performance of CK5, the king machine of CKB?
透过华为军团看科技之变(五):智慧园区
ArcGIS Pro scripting tool (5) - delete duplicates after sorting
CSDN博客运营团队2022年H1总结
GD32 RT-Thread RTC驱动函数
Apple's 5g chip was revealed to have failed in research and development, and the QQ password bug caused heated discussion. Wei Lai responded to the short selling rumors. Today, more big news is here
我在鹅厂淘到了一波“炼丹神器”,开发者快打包
【Rust日报】2021-01-22 首份Rust月刊杂志邀请大家一起参与
59 websites programmers need to know