Evaluates model over validation set.
(model, val_dataloader, criterion, args)
| 68 | |
| 69 | |
| 70 | def eval_model(model, val_dataloader, criterion, args): |
| 71 | """ |
| 72 | Evaluates model over validation set. |
| 73 | """ |
| 74 | print("\n" + ("-" * 30) + " Evaluating model! " + ("-" * 30)) |
| 75 | total_loss = 0 |
| 76 | total_correct = 0 |
| 77 | total_examples = 0 |
| 78 | |
| 79 | avg_loss = None |
| 80 | avg_acc = None |
| 81 | |
| 82 | model.eval() |
| 83 | |
| 84 | with torch.no_grad(): |
| 85 | for idx, (x, y) in tqdm(enumerate(val_dataloader), total=len(val_dataloader)): |
| 86 | |
| 87 | # --- Move to GPU --- |
| 88 | # x = x.cuda(constants.GPU, non_blocking=True) |
| 89 | # y = y.cuda(constants.GPU, non_blocking=True) |
| 90 | |
| 91 | # --- Compute logits --- |
| 92 | logits = model(x) |
| 93 | loss = criterion(logits, y) |
| 94 | |
| 95 | # --- Bookkeeping --- |
| 96 | _, predicted = torch.max(logits.data, 1) |
| 97 | total_correct += (predicted == y).sum().item() |
| 98 | |
| 99 | total_loss += loss.item() |
| 100 | total_examples += y.shape[0] |
| 101 | # total_examples += y.shape[0] * y.shape[1] |
| 102 | |
| 103 | avg_loss = total_loss / total_examples |
| 104 | avg_acc = total_correct / total_examples |
| 105 | |
| 106 | # if idx % args.print_every_eval_minibatch == 0: |
| 107 | # tqdm.write(f"Val minibatch number: {idx} | Avg loss: {avg_loss} | Avg acc: {avg_acc}") |
| 108 | |
| 109 | return avg_loss, avg_acc, total_examples |
| 110 | |
| 111 | |
| 112 | def train(args, model, train_dataloader, val_dataloader, criterion, opt): |