MCPcopy Create free account
hub / github.com/pytorch/examples / get_accuracy

Function get_accuracy

distributed/rpc/parameter_server/rpc_parameter_server.py:199–216  ·  view source on GitHub ↗
(test_loader, model)

Source from the content-addressed store, hash-verified

197
198
199def get_accuracy(test_loader, model):
200 model.eval()
201 correct_sum = 0
202 # Use GPU to evaluate if possible
203 if torch.accelerator.is_available() and model.num_gpus > 0:
204 acc = torch.accelerator.current_accelerator()
205 device = torch.device(f'{acc}:0')
206 else:
207 device = torch.device("cpu")
208 with torch.no_grad():
209 for i, (data, target) in enumerate(test_loader):
210 out = model(data)
211 pred = out.argmax(dim=1, keepdim=True)
212 pred, target = pred.to(device), target.to(device)
213 correct = pred.eq(target.view_as(pred)).sum().item()
214 correct_sum += correct
215
216 print(f"Accuracy {correct_sum / len(test_loader.dataset)}")
217
218
219# Main loop for trainers.

Callers 1

run_training_loopFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected