PyTorch中,關於model.eval()和torch.no_grad()


  • 一直對於model.eval()和torch.no_grad()有些疑惑
  • 之前看博客說,只用torch.no_grad()即可
  • 但是今天查資料,發現不是這樣,而是兩者都用,因為兩者有着不同的作用
  • 引用stackoverflow:

Use both. They do different things, and have different scopes.
with torch.no_grad: disables tracking of gradients in autograd.
model.eval(): changes the forward() behaviour of the module it is called upon. eg, it disables dropout and has batch norm use the entire population statistics


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM