文章首发及后续更新:https://mwhls.top/3629.html
新的更新内容请到mwhls.top查看。
无图/无目录/格式错误/更多相关请到上方的文章首发页面查看。
目录
2. What does model.train() do in PyTorch?
PyTorch的model.train()有什么用?
aerin asked:
- 它是在
nn.Module里调用forward()?调用模型的时候不是会使用forward方法吗,为什么还需要指定用train()?
- 它是在
Answers:
Umang Gupta – vote: 192
model.train()让你的模型知道现在正在训练。像 dropout、batchnorm 层在训练和测试时的作用不同,所以需要使它们运行在对应的模式中。更多细节:设置模式以训练(见源码)。调用
model.eval()或model.train(mode=False)启用测试。train不只用来训练,还用来测试,区别在于mode的不同设置。
译者注:最后一句有点看不懂,我按我理解的来,有误请留言:It is somewhat intuitive to expecttrainfunction to train model but it does not do that. It just sets the mode.prosti – vote: 75
module.train()的源码:def train(self, mode=True):
r""“Sets the module in training mode.”""
self.training = mode
for module in self.children():
module.train(mode)
return self
module.eval的源码:def eval(self): r"""Sets the module in evaluation mode.""" return self.train(False)train与eval是 module 中对立的两种模式。目前只有
Dropout和BatchNorm关心self.training标志。标志默认为
True。iacob – vote: 14
model.train()model.eval()设置模型为训练模式,即
•BatchNorm层利用每个 batch 来统计
•Dropout层激活,例。设置模型为评估/推理模式,即
•BatchNormlayers use running statistics
•Dropout层取消。
等效于model.train(False)。- 注意:每个方法都不调用前向与后向传递,它们只用来告诉模型如何运行以及何时运行。
- 这很重要,一些模块(层) 在训练与推理时的设计不同,如果运行在错误的模式下,会产生意料之外的结果。
What does model.train() do in PyTorch?
aerin asked:
- Does it call
forward()innn.Module? I thought when we call the model,forwardmethod is being used. Why do we need to specify train()?
它是在nn.Module里调用forward()?调用模型的时候不是会使用forward方法吗,为什么还需要指定用train()?
- Does it call
Answers:
Umang Gupta – vote: 192
model.train()tells your model that you are training the model. So effectively layers like dropout, batchnorm etc. which behave different on the train and test procedures know what is going on and hence can behave accordingly.model.train()让你的模型知道现在正在训练。像 dropout、batchnorm 层在训练和测试时的作用不同,所以需要使它们运行在对应的模式中。More details: It sets the mode to train (see source code). You can call either
model.eval()ormodel.train(mode=False)to tell that you are testing. It is somewhat intuitive to expecttrainfunction to train model but it does not do that. It just sets the mode.
更多细节:设置模式以训练(见源码)。调用model.eval()或model.train(mode=False)启用测试。train不只用来训练,还用来测试,区别在于mode的不同设置。
译者注:最后一句有点看不懂,我按我理解的来,有误请留言:It is somewhat intuitive to expecttrainfunction to train model but it does not do that. It just sets the mode.prosti – vote: 75
Here is the code of
module.train():module.train()的源码:def train(self, mode=True): r"""Sets the module in training mode.""" self.training = mode for module in self.children(): module.train(mode) return selfAnd here is the
module.eval.module.eval的源码:def eval(self): r"""Sets the module in evaluation mode.""" return self.train(False)Modes
trainandevalare the only two modes we can set the module in, and they are exactly opposite.train与eval是 module 中对立的两种模式。That\’s just a
self.trainingflag and currently onlyDropoutandBatchNormcare about that flag.
目前只有Dropout和BatchNorm关心self.training标志。By default, this flag is set to
True.
标志默认为True。iacob – vote: 14
model.train()model.eval()Sets your model in training mode i.e.
设置模型为训练模式,即
•BatchNormlayers use per-batch statistics
•BatchNorm层利用每个 batch 来统计
•Dropoutlayers activated etc
•Dropout层激活,例。Sets your model in evaluation (inference) mode i.e.
设置模型为评估/推理模式,即
•BatchNormlayers use running statistics
•Dropoutlayers de-activated etc.
•Dropout层取消。
Equivalent tomodel.train(False).
等效于model.train(False)。- Note: neither of these function calls run forward / backward passes. They tell the model how to act when run.
注意:每个方法都不调用前向与后向传递,它们只用来告诉模型如何运行以及何时运行。 - This is important as some modules (layers) (e.g.
Dropout,BatchNorm) are designed to behave differently during training vs inference, and hence the model will produce unexpected results if run in the wrong mode.
这很重要,一些模块(层) 在训练与推理时的设计不同,如果运行在错误的模式下,会产生意料之外的结果。










