Hello every one.
I’m trying to write a function that does both training and validation since like 90% of the code for them is the same .  But I’m facing a slight problem here. I don’t know where this should be placed so the gradient is not calculated for the tensors.
Should I place with torch.no_grad() before the operations such as model(imgs), criterion(preds, labels) or
Should I be using it Like this e.g.
for imgs, labels in dataloader:
    imgs = imgs.to(device) 
    labels = labels.to(device)
    with torch._nograd():
            model.eval()
            preds = mode(imgs)
   # the rest 
   loss = criterion(preds, labels) 

or
for imgs, labels in dataloader:
    with torch._nograd():
           imgs = imgs.to(device) 
           labels = labels.to(device)
           model.eval()
           preds = mode(imgs)
   # the rest 
   loss = criterion(preds, labels)
   # acc, etc