Python model.train() 在 pytorch 中有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51433378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does model.train() do in pytorch?
提问by Aerin
Does it call forward()
in nn.Module
? I thought when we call the model, forward
method is being used.
Why do we need to specify train()?
它打forward()
进来了nn.Module
吗?我想当我们调用模型时,forward
正在使用方法。为什么我们需要指定train()?
回答by Umang Gupta
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()
or model.train(mode=False)
to tell that you are testing.
It is somewhat intuitive to expect train
function to train model but it does not do that. It just sets the mode.
更多细节:它设置训练模式(见源代码)。您可以致电model.eval()
或model.train(mode=False)
告知您正在测试。期望train
函数来训练模型有点直观,但它并没有这样做。它只是设置模式。
回答by prosti
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 self
And here is the module.eval
.
这是module.eval
.
def eval(self):
r"""Sets the module in evaluation mode."""
return self.train(False)
Modes train
and eval
are the only two modes we can set the module in, and they are exactly opposite.
Modestrain
和eval
是我们可以设置模块的仅有的两种模式,它们完全相反。
That's just a self.training
flag and currently onlydropoutand bachnormcare about that flag.
这只是一个self.training
标志,目前只有辍学和bachnorm关心这个标志。
By default, this flag is set to True
.
默认情况下,此标志设置为True
。
回答by kelam gautam
There are two ways of letting the model know your intention i.e do you want to train the model or do you want to use the model to evaluate. In case of model.train() the model knows it has to learn the layers and when we use model.eval() it indicates the model that nothing new is to be learnt and the model is used for testing. model.eval() is also necessary because in pytorch if we are using batchnorm and during test if we want to just pass a single image, pytorch throws an error if model.eval() is not specified.
有两种方法可以让模型知道您的意图,即您是要训练模型还是要使用模型进行评估。在 model.train() 的情况下,模型知道它必须学习层,当我们使用 model.eval() 时,它表明模型不需要学习任何新内容,并且模型用于测试。model.eval() 也是必要的,因为在 pytorch 中,如果我们使用的是 batchnorm,而在测试期间如果我们只想传递单个图像,如果没有指定 model.eval(),pytorch 会抛出错误。