pytorch 模型的train模式與eval模式實例
原因
對于一些含有batch normalization或者是Dropout層的模型來說,訓(xùn)練時的froward和驗證時的forward有計算上是不同的,因此在前向傳遞過程中需要指定模型是在訓(xù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)練和驗證
以上這篇pytorch 模型的train模式與eval模式實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python2和Python3讀取文本文件的區(qū)別及說明
這篇文章主要介紹了Python2和Python3讀取文本文件的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02python實現(xiàn)郵件循環(huán)自動發(fā)件功能
這篇文章主要介紹了python實現(xiàn)郵件循環(huán)自動發(fā)件功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Python實現(xiàn)統(tǒng)計代碼行的方法分析
這篇文章主要介紹了Python實現(xiàn)統(tǒng)計代碼行的方法,結(jié)合實例形式分析了Python針對代碼行數(shù)的計算實現(xiàn)步驟與操作技巧,需要的朋友可以參考下2017-07-07Keras在mnist上的CNN實踐,并且自定義loss函數(shù)曲線圖操作
這篇文章主要介紹了Keras在mnist上的CNN實踐,并且自定義loss函數(shù)曲線圖操作,具有很好的參考價值,希望對大家有所幫助。2021-05-05使用matplotlib實現(xiàn)在同一個窗口繪制多個圖形
這篇文章主要介紹了使用matplotlib實現(xiàn)在同一個窗口繪制多個圖形問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08