I'm following the book "Deep Learning with PyTorch Step By Step" and I have a question about the predict method in the StepByStep class (from this repository: GitHub).
The current implementation is:
def predict(self, x):
self.model.eval()
x_tensor = torch.as_tensor(x).float()
y_hat_tensor = self.model(x_tensor.to(self.device))
self.model.train()
return y_hat_tensor.detach().cpu().numpy()
My question is: should I wrap the forward pass with torch.inference_mode() or torch.no_grad() even though I'm already calling self.model.eval() like :
def predict(self, x):
self.model.eval()
x_tensor = torch.as_tensor(x).float()
with torch.inference_mode():
y_hat_tensor = self.model(x_tensor.to(self.device))
self.model.train()
return y_hat_tensor.detach().cpu().numpy() # detach() is superfluous
I understand that model.eval() changes the behavior of layers like dropout and batch normalization, but I believe the context manager should still be added for better performance and memory efficiency.
However, I'm not sure why the original implementation doesn't include it. What are the best practices here?
model.eval()andno_grad()do different things and you should use both.inference_mode()has similar functionality tono_grad(), so you should still use it andmode.eval(). As for why the book doesn't use it, who knows. I don't know the book and make no assumptions about the author, but technically anyone can write a book, whether they fully grasp the material or not.