MCPcopy Create free account
hub / github.com/AtlasAnalyticsLab/AdaFisher / evaluate

Function evaluate

Language_Model/run_exp.py:61–92  ·  view source on GitHub ↗
(epoch, model, dataloader, args, mode="val")

Source from the content-addressed store, hash-verified

59
60
61def evaluate(epoch, model, dataloader, args, mode="val"):
62 model.eval()
63 losses = []
64 total_loss = 0.0
65 total_iters = 0
66 start_time = time.time()
67 with torch.no_grad():
68 for idx, batch in enumerate(
69 tqdm(dataloader, desc="Evaluation", disable=(not args.progress_bar))
70 ):
71 batch = to_device(batch, args.device)
72 log_probas = model(batch["source"])
73
74 loss = model.loss(log_probas, batch["target"], batch["mask"])
75 losses.append(loss.item() * batch["mask"].sum().item())
76
77 total_loss += loss.item()
78 total_iters += batch["source"].shape[1]
79
80 if idx % args.print_every == 0:
81 tqdm.write(
82 f"[{mode.upper()}] Epoch: {epoch}, Iter: {idx}, Loss: {loss.item():.5f}"
83 )
84
85 mean_loss = np.mean(losses)
86 mean_loss /= args.batch_size * dataloader.dataset.max_length
87 perplexity = math.exp(mean_loss)
88 tqdm.write(
89 f"=== [{mode.upper()}] Epoch: {epoch}, Iter: {idx}, Perplexity: {perplexity:.3f} ===>"
90 )
91
92 return mean_loss, perplexity, time.time() - start_time
93
94
95def main(args):

Callers 1

mainFunction · 0.85

Calls 2

to_deviceFunction · 0.90
lossMethod · 0.80

Tested by

no test coverage detected