MCPcopy Create free account
hub / github.com/Modulus-Labs/RockyBot / eval_model

Function eval_model

pytorch-model/regression_train.py:69–107  ·  view source on GitHub ↗

Evaluates model over validation set.

(model, val_dataloader, criterion, args)

Source from the content-addressed store, hash-verified

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

Callers 1

trainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected