in if(self.fedtype == 'scaffold'): with torch.nograd(): for name,param in…
- User
in if(self.fed_type == 'scaffold'): with torch.no_grad(): for name,param in net.named_parameters(): # print(f"nome data {name} valore {param.data}") # if torch.isnan(param.grad).any() or torch.isnan(self.ci[name]).any() or torch.isnan(self.c[name]).any(): # print(f"NaN detected in gradients or control variates for parameter {name}") if param.grad is not None: param.data = param.data - self.args.lr*(param.grad-self.ci[name].data+self.c[name].data) # print(f"nome data {name} valore {param.data}") # if torch.isnan(param.data).any(): # print(f"NaN found in parameter {name} after update") in cui aggiorno i valori dei parametri di un modello, dovrei tenere tutto sotto no grad oppure no?
- Assistant
In PyTorch, the
torch.no_grad()context is used to prevent the tracking of operations for autograd, which is useful to reduce memory consumption during model inference or when you are explicitly manipulating parameters outside of the normal training flow. In the snippet you posted, you are manually updating model parameters, which can be a scenario where you'd want to usetorch.no_grad(), as it prevents PyTorch from storing gradients for those operations.Here's a brief explanation of why you might use or not use
torch.no_grad()in your scenario: