当前位置:网站首页>Why should model.eval() be added to the pytorch test?
Why should model.eval() be added to the pytorch test?
2022-08-01 15:00:00 【passion-ma】
Many machine learning tutorials have mentioned that when using pytorch for training and testing, you must specify eval for the instantiated model, so why do you need to set model.eval() when pytorch is testing?What is the function of model.eval()?The next article tells you.
When using PyTorch for training and testing, be sure to specify train/eval for the instantiated model. When eval(), the framework will automatically fix BN and DropOut.It will not take the average, but use the trained value, otherwise, once the batch_size of the test is too small, it will easily be caused by the BN layer to generate a large color distortion of the image!!!!!!
The difference between model.eval() and with torch.no_grad()
When validating in PyTorch, model.eval() is used to switch to test mode, in which mode,
Mainly used to notify the dropout layer and batchnorm layer to switch between train and val modes
In the train mode, the dropout network layer will set the probability of retaining the activation unit according to the set parameter p (retention probability = p); the batchnorm layer will continue to calculate and update the parameters such as the mean and var of the data.
In val mode, the dropout layer will allow all activation units to pass through, while the batchnorm layer will stop computing and update mean and var, directly using what has been learned in the training phaseout the mean and var values.
This mode does not affect the gradient calculation behavior of each layer, that is, the gradient calculation and storage are the same as the training mode, but no backprobagation is performed
And with torch.no_grad() is mainly used to stop the work of the autograd module to accelerate and save video memory. The specific behavior is to stop the gradient calculation, thereby saving GPU computing power and video memory, but it does notAffects the behavior of dropout and batchnorm layers.
I don't understand why there is a difference between model.eval() and model.train() in the training and testing functions. After reviewing, I will make the following arrangements
In general, our training process is as follows:
1. Train after getting the data. During the training process, use
model.train(): Tell our network that this stage is used for training and can update parameters.
2. Prediction after training is completed. During the prediction process, use
model.eval() : Tell our network that this stage is used for testing, so the parameters of the model are not updated in this stage.
边栏推荐
- 通胀持续 肯尼亚粮食安全引关注
- The problem that the column becomes indexed after pd groupby and the aggregation column has no column name
- The default database main key, foreign key, and the only key index
- SQL查询数据以及排序
- 给网站增加离开页面改变网站标题效果
- “查找附近的商铺”|Geohash+MySQL实现地理位置筛选
- 视频传输协议(常用的视频协议)
- VIM实用指南(0)基本概念与初次体验
- [Binary Tree] Path Sum II
- Stored procedures in MySQL (detailed)
猜你喜欢
随机推荐
HTB-Shocker
如何使用 Mashup 技术在 SAP Cloud for Customer 页面嵌入自定义 UI
c语言rand函数生成随机数,详解C语言生成随机数rand函数的用法[通俗易懂]
MySQL中字符串比较大小(日期字符串比较问题)
HTB-Shocker
hzero-resource秒退
Pytorch - Distributed Model Training
COPU 陆首群教授在 openEuler 社区首批高级顾问聘任仪式上发言
Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
2022-08-01 Daily: 18 graphs to intuitively understand neural networks, manifolds and topology
WPF如何自定义隐藏下拉框选项
测试工程师进阶必读书目
如何快速将Zabbix5.0升级至6.0?
利用UIRecorder做页面元素巡检
SSM入门
未来小间距竞争的着力点在哪里
经纬信息IPO过会:年营收3.5亿 叶肖华控制46.3%股权
CodeForces 570D Tree Requests
你真的会测试用户登录吗?
通胀持续 肯尼亚粮食安全引关注









