What does model.train() do in PyTorch?(model.train() 在 PyTorch 中做什么?)
问题描述
它是否在nn.Module
中调用了forward()
?我想当我们调用模型时,正在使用 forward
方法.为什么需要指定train()?
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()?
推荐答案
model.train()
告诉您的模型您正在训练模型.如此有效的层,如 dropout、batchnorm 等,在训练和测试过程中表现不同,知道发生了什么,因此可以相应地表现.
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.eval()
或 model.train(mode=False)
来告诉您正在测试.期望 train
函数来训练模型有点直观,但它并没有这样做.它只是设置模式.
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.train() 在 PyTorch 中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!