当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
win10+Qt5.15.2实现低功耗蓝牙控制
线性代数的简单应用
百图生科卓越开发者计划全面升级暨《计算免疫问题白皮书》发布
CodeForces 570D Tree Requests
经纬信息IPO过会:年营收3.5亿 叶肖华控制46.3%股权
荣信文化通过注册:年营收3.8亿 王艺桦夫妇为实控人
mysql查询两个字段值相同的记录
Inflation continues, Kenya's food security a concern
2022年5月20日最全摸鱼游戏导航
Performance Optimization - Resource Optimization Notes
向mysql 传数据 这个字符串长度有限制么
LeetCode50天刷题计划(Day 6—— 整数反转 14.20-15.20)
产品力无提升的雷克萨斯新款ES ,为何敢于涨价?
Pytorch - Distributed Model Training
立新能源深交所上市:市值55亿 哈密国投与国有基金是股东
什么是闭包?
HTB-Mirai
DaemonSet of kubernetes and rolling update
第十三章 手动创建 REST 服务(一)
30分钟成为Contributor|如何多方位参与OpenHarmony开源贡献?









