model.eval() 在 pytorch 中有什么作用? [英] What does model.eval() do in pytorch?

查看:64
本文介绍了model.eval() 在 pytorch 中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码,并在某些情况下看到 .eval().

I am using this code, and saw .eval() in some cases.

我知道它应该允许我评估我的模型",但我不明白什么时候应该和不应该使用它,或者如何关闭它.

I understand it is supposed to allow me to "evaluate my model", but I don't understand when I should and shouldn't use it, or how to turn it off.

我想运行上面的代码来训练网络,并且还能够在每个 epoch 运行验证.我还是做不到.

I would like to run the above code to train the network, and also be able to run validation every epoch. I wasn't able to do it still.

推荐答案

model.eval() 是模型的某些特定层/部分在训练和推理期间表现不同的一种开关(评估)时间.例如,Dropouts Layers、BatchNorm Layers 等.您需要在模型评估时将它们关闭,.eval() 会为您完成.此外,评估/验证的常见做法是使用 torch.no_grad()model.eval() 配对来关闭梯度计算:

model.eval() is a kind of switch for some specific layers/parts of the model that behave differently during training and inference (evaluating) time. For example, Dropouts Layers, BatchNorm Layers etc. You need to turn off them during model evaluation, and .eval() will do it for you. In addition, the common practice for evaluating/validation is using torch.no_grad() in pair with model.eval() to turn off gradients computation:

# evaluate model:
model.eval()

with torch.no_grad():
    ...
    out_data = model(data)
    ...

但是,不要忘记在 eval 步骤后回到 training 模式:

BUT, don't forget to turn back to training mode after eval step:

# training step
...
model.train()
...

这篇关于model.eval() 在 pytorch 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆