文章来源 https://www.cnblogs.com/king-lps/p/8570021.html
1. PyTorch进行训练和测试时指定实例化的model模式为:train/eval
eg:
![[Pytorch]Pytorch 细节记录(转) [Pytorch]Pytorch 细节记录(转)](https://image.miaokee.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700&webp=1)
class VAE(nn.Module):
def __init__(self):
super(VAE, self).__init__()
...
def reparameterize(self, mu, logvar):
if self.training:
std = logvar.mul(0.5).exp_()
eps = Variable(std.data.new(std.size()).normal_())
return eps.mul(std).add_(mu)
else:
return mu model = VAE()
...
def train(epoch):
model.train()
...
def test(epoch):
model.eval()
![[Pytorch]Pytorch 细节记录(转) [Pytorch]Pytorch 细节记录(转)](https://image.miaokee.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700&webp=1)
eval即evaluation模式,train即训练模式。仅仅当模型中有Dropout
和BatchNorm
是才会有影响。因为训练时dropout和BN都开启,而一般而言测试时dropout被关闭,BN中的参数也是利用训练时保留的参数,所以测试时应进入评估模式。
(在训练时,