forked from gregorbachmann/Next-Token-Failures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
89 lines (69 loc) · 3.19 KB
/
evaluate.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import torch
from tqdm import tqdm
from utils.training_utils import AverageMeter
# Function to evaluate performance when generating
@torch.no_grad()
def evaluate(model, loader, ctx, temperature, top_k, results=None, mode='test'):
"""
Generates sequences (without teacher-forcing) and calculates accuracies
"""
num_prefix_tokens = loader.dataset.num_prefix_tokens
num_target_tokens = loader.dataset.num_target_tokens
# Switch dataset and model to "eval" mode
loader.dataset.eval()
model.eval()
total_acc = AverageMeter()
tokens_corr = {i: AverageMeter() for i in range(num_target_tokens)}
bar = tqdm(loader)
#model.set_cache(loader.dataset.device)
for x in bar:
y = x[:, num_prefix_tokens:].clone()
x = x[:, :num_prefix_tokens].clone()
with ctx:
y_pred = model.generate(x, num_target_tokens, temperature=temperature, top_k=top_k)
#model.reset_cache()
# Check how many tokens we get right and how many predictions are completely correct
correct = y.eq(y_pred[:, -num_target_tokens:]).float()
# Completely correct
completely_correct = torch.mean(correct.sum(dim=1).eq(num_target_tokens).to(torch.float))
total_acc.update(completely_correct.item(), x.shape[0])
# Individual token accuracy
per_token_acc = correct.mean(dim=0)
for i in range(num_target_tokens):
tokens_corr[i].update(per_token_acc[i].item(), x.shape[0])
bar.set_description(f'{mode} accuracy: {total_acc.get(percentage=True):.2f}')
#model.empty_cache()
# Switch back to train mode
loader.dataset.train()
model.train()
if results is not None:
results[mode + '/accuracy'] = total_acc.get(percentage=True)
for i in range(num_target_tokens):
results[mode + '/token_' + str(i + 1)] = tokens_corr[i].get(percentage=True)
return results
# Function to evaluate performance when applying teacher forcing
@torch.no_grad()
def evaluate_forced(model, loader, ctx, results=None, mode='test'):
"""
Generates sequences with teacher-forcing and calculates accuracies
"""
num_target_tokens = loader.dataset.num_target_tokens
total_acc, total_loss = AverageMeter(), AverageMeter()
tokens_corr = {i: AverageMeter() for i in range(num_target_tokens)}
bar = tqdm(loader)
for x, y in bar:
# Produce logits with teacher-forcing (i.e. like during training)
with ctx:
logits, loss, accs = model(x, y)
total_acc.update(val=accs['acc'], num=x.shape[0])
total_loss.update(val=loss, num=x.shape[0])
for i in range(num_target_tokens):
tokens_corr[i].update(accs['token_acc'], x.shape[0])
bar.set_description('Forced Loss: {:.4f} Forced Acc: {:.2f}'.format(total_loss.get(),
total_acc.get(percentage=True)))
if results is not None:
results[mode + '/forced loss'] = total_loss.get()
results[mode + '/forced accuracy'] = total_acc.get(percentage=True)
for i in range(num_target_tokens):
results[mode + '/token_' + str(i + 1)] = tokens_corr[i].get(percentage=True)
return results