python - 评估pytorch模型: `with torch.no_grad` vs `model.eval()`

标签 python machine-learning deep-learning pytorch autograd

当我想评估模型在验证集上的性能时,是否首选使用 with torch.no_grad: 还是 model.eval()

最佳答案

TL;博士:

Use both 。他们做不同的事情,并且有不同的范围。

  • with torch.no_grad - 禁用 autograd 中的渐变跟踪。
  • model.eval() 更改调用它的模块的 forward() 行为
  • 例如,它禁用 dropout 并使用整个总体统计数据进行批量归一化
<小时/>

使用 torch.no_grad

torch.autograd.no_grad documentation说:

Context-manager that disabled [sic] gradient calculation.

Disabling gradient calculation is useful for inference, when you are sure that you will not call Tensor.backward(). It will reduce memory consumption for computations that would otherwise have requires_grad=True. In this mode, the result of every computation will have requires_grad=False, even when the inputs have requires_grad=True.

model.eval()

nn.Module.eval documentation说:

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. Dropout, BatchNorm, etc.

<小时/>

The creator of pytorch said the documentation should be updated to suggest the usage of both ,我提出了 pull request .

关于python - 评估pytorch模型: `with torch.no_grad` vs `model.eval()` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55627780/

相关文章:

python - 使用Python通过套接字发送文件仅在强制关闭套接字后起作用

machine-learning - 如何在caffe或DIGITS中实现L2正则化?

deep-learning - 为拥抱脸 (HF) ViT 模型创建特征提取器的正确方法是什么?

python - 将数据缩放到一定范围

python - ValueError : y has only 1 sample in class 0, 协方差定义不明确。而QDA分类

validation - 从图像二进制数据中提取特征的工具

deep-learning - RNN/LSTM单元解读

python - 在 wxPython/python 中的 while 无限循环中使用 wx.CallAfter() 导致应用程序崩溃

python - 在Python中循环遍历图像中每个像素的更快方法?

python - "Numpy not Available"安装Pytorch XLA后