欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch 模型的train模式與eval模式實(shí)例

 更新時(shí)間:2020年02月20日 16:14:57   作者:rasekk  
今天小編就為大家分享一篇pytorch 模型的train模式與eval模式實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

原因

對(duì)于一些含有batch normalization或者是Dropout層的模型來(lái)說(shuō),訓(xùn)練時(shí)的froward和驗(yàn)證時(shí)的forward有計(jì)算上是不同的,因此在前向傳遞過(guò)程中需要指定模型是在訓(xùn)練還是在驗(yàn)證。

源代碼

[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.
  """
  #該方法調(diào)用了nn.train()方法,把參數(shù)默認(rèn)值改為false. 增加聚合性
  return self.train(False)

在使用含有BN層,dropout層的神經(jīng)網(wǎng)路來(lái)說(shuō),必須要區(qū)分訓(xùn)練驗(yàn)證

以上這篇pytorch 模型的train模式與eval模式實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論