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

Function eval_model

pytorch-model/regression_eval.py:23–89  ·  view source on GitHub ↗

Evaluates model over validation set.

(model, val_dataloader, criterion, args)

Source from the content-addressed store, hash-verified

21
22
23def eval_model(model, val_dataloader, criterion, args):
24 """
25 Evaluates model over validation set.
26 """
27
28 # --- Buckets for seeing how close the classification *would* be ---
29 bins = [-1800, -15, -5, 0, 5, 15, 1800]
30 total_correct = 0
31
32 print("\n" + ("-" * 30) + " Evaluating model! " + ("-" * 30))
33 total_loss = 0
34 total_residual = 0
35 total_examples = 0
36
37 avg_loss = None
38 avg_residual = None
39
40 model.eval()
41
42 with torch.no_grad():
43 for idx, (x, y) in tqdm(enumerate(val_dataloader), total=len(val_dataloader)):
44
45 # --- Move to GPU ---
46 # x = x.cuda(constants.GPU, non_blocking=True)
47 # y = y.cuda(constants.GPU, non_blocking=True)
48
49 # --- Compute logits ---
50 print(x.shape)
51 print(x[0])
52 print(x)
53 print(y)
54 logits = model(x)
55 loss = criterion(y, logits)
56
57 # --- Bookkeeping ---
58 residuals = torch.abs(y - logits)
59
60 total_loss += loss.item()
61 total_residual += torch.sum(residuals).item()
62 total_examples += y.shape[0]
63
64 avg_loss = total_loss / total_examples
65 avg_residual = total_residual / total_examples
66
67 # --- Computing the presumed classification accuracy ---
68 label_diff_buckets = list()
69 for label in y:
70 for bin_idx in range(len(bins) - 1):
71 if label >= bins[bin_idx] and label < bins[bin_idx + 1]:
72 label_diff_buckets.append(bin_idx)
73 break
74 label_diff_buckets = torch.from_numpy(np.asarray(label_diff_buckets))
75
76 output_diff_buckets = list()
77 for pred_price_diff in logits:
78 print(pred_price_diff)
79 for bin_idx in range(len(bins) - 1):
80 if pred_price_diff >= bins[bin_idx] and pred_price_diff < bins[bin_idx + 1]:

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected