欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

pytorch 模型的train模式与eval模式实例

时间:2021-04-15 11:18:39|栏目:Python代码|点击:

原因

对于一些含有batch normalization或者是Dropout层的模型来说,训练时的froward和验证时的forward有计算上是不同的,因此在前向传递过程中需要指定模型是在训练还是在验证。

源代码

[docs] def train(self, mode=True):
  r"""Sets the module in training mode.

  This has any effect only on certain modules. See documentations of
  particular modules for details of their behaviors in training/evaluation
  mode, if they are affected, e.g. :class:`Dropout`, :class:`BatchNorm`,
  etc.

  Returns:
   Module: self
  """
  self.training = mode
  for module in self.children():
   module.train(mode)
  return self

[docs] def eval(self):
  r"""Sets the module in evaluation mode.

  This has any effect only on certain modules. See documentations of
  particular modules for details of their behaviors in training/evaluation
  mode, if they are affected, e.g. :class:`Dropout`, :class:`BatchNorm`,
  etc.
  """
  #该方法调用了nn.train()方法,把参数默认值改为false. 增加聚合性
  return self.train(False)

在使用含有BN层,dropout层的神经网路来说,必须要区分训练验证

上一篇:python字符串的方法与操作大全

栏    目:Python代码

下一篇:django celery redis使用具体实践

本文标题:pytorch 模型的train模式与eval模式实例

本文地址:http://www.codeinn.net/misctech/101839.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有