Eval Mode
For model training, it’s common to see below:
1
2
3
4
5
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Encoder(...).to(device).eval() # to(device) is always a safer alternative than .cuda() because GPU might not be available
with torch.no_grad():
out = model(x.to(device), feats.to(device))
eval()would disable Batch Norm andDropoutcalculation. This is very important for inferencing. In the meantime,eval()does not turn off gradient tracking. So we needwith torch.no_grad()for this purpose.
- Recall thatDropoutrandomly zeros out activations