测试结果比训练结果差很多
在深度学习中,我们训练完一个模型,利用起测试的时候,发现效果下降了好多。这里面有一部分原因可能是模型本身设计不好导致的过拟合。但是当你用的网络没问题,那还有可能是哪里的问题了。
我也是经过了一段时间排查,发现忘记写net.eval()
了,这个非常重要。我们可以看一下其源码里面如何描述的:
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.
当你的网络里面有dropout或者批归一化,一定要记着加入.eval()
,这样才会使得测试的结果是正确的。