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 中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:model.train() 在 PyTorch 中做什么?


基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01