亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

原因

對于一些含有batch normalization或者是Dropout層的模型來說,訓(xùn)練時(shí)的froward和驗(yàn)證時(shí)的forward有計(jì)算上是不同的,因此在前向傳遞過程中需要指定模型是在訓(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)路來說,必須要區(qū)分訓(xùn)練驗(yàn)證

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

相關(guān)文章

最新評論