[d129b2]: / medicalbert / evaluator / sequence_evaluator.py

Download this file

52 lines (38 with data), 1.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import logging
import os
import numpy as np
import pandas as pd
import torch
from tqdm import tqdm
from evaluator.standard_evaluator import StandardEvaluator
class SequenceEvaluator(StandardEvaluator):
def __init__(self, results_path, config, datareader, best_model_selector):
self.datareader = datareader
self.result_dir = results_path # path to the results directory
self.config = config
self.model_selector = best_model_selector
def run(self, classifier, data, output_dir):
logging.info("Running Evaluations")
# Put the classifier in training mode.
device = torch.device(self.config['device'])
classifier.set_eval_mode()
classifier.model.to(device)
all_logits = None
all_labels = None
for step, batch in enumerate(tqdm(data, desc="evaluating")):
batch = tuple(t.to(device) for t in batch)
features, label_ids = batch
with torch.no_grad():
out = classifier.model(features, labels=label_ids)
logits = out[1]
logits = logits.detach().cpu().numpy()
labels = label_ids.detach().cpu().numpy()
if all_logits is not None:
all_labels = np.concatenate([all_labels, labels])
all_logits = np.concatenate([all_logits, logits])
else:
all_labels = labels
all_logits = logits
summary, output = StandardEvaluator.condense_output(all_logits, all_labels)
self.save(summary, output, output_dir)
return summary