-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_eval.py
More file actions
173 lines (147 loc) · 7.24 KB
/
test_eval.py
File metadata and controls
173 lines (147 loc) · 7.24 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from utils.eval import *
from model.dataset import *
import sys
import numpy as np
import torch
import time
def test_distances(vocab):
print ("-"*80)
print("Running Sanity Check: Distances Between Parens")
# Test 1
test = ['(a','a)', '(b', 'b)', '(a', 'a)', '(b', 'b)', '(a', 'a)']
test_idx = vocab.words2indices(test)
exp_dist = [None, 1, None, 1, None, 1, None, 1, None, 1]
dist = get_distances(y=test_idx)
assert(exp_dist == dist), "Expect {}, got {}".format(exp_dist, dist)
# Test 2
test = ['(b','(a','(b', '(a', '(b', 'b)', 'a)', 'b)', 'a)', 'b)']
test_idx = vocab.words2indices(test)
exp_dist = [None, None, None, None, None, 1, 3, 5, 7, 9]
dist = get_distances(y=test_idx)
assert(exp_dist == dist)
# Test 3
test = ['(a','(a', 'a)', '(b', '(a', 'a)', 'b)', '(b', 'b)', 'a)']
test_idx = vocab.words2indices(test)
exp_dist = [None, None, 1, None, None, 1, 3, None, 1, 9]
dist = get_distances(y=test_idx)
assert(exp_dist == dist)
print("All Sanity Checks Passed!")
print ("-"*80)
def test_ldpa(vocab):
print ("-"*80)
print("Running Sanity Check: get_LDPA_counts Calculation")
# indices of open and close in vocabulary
open_idx = [4, 5]
close_idx = [6, 7]
# create three test cases
test1 = vocab.words2indices(['(a','a)', '(b', 'b)', '(a', 'a)', '(b', 'b)', '(a', 'a)'])
test2 = vocab.words2indices(['(b','(a','(b', '(a', '(b', 'b)', 'a)', 'b)', 'a)', 'b)'])
test3 = vocab.words2indices(['(a','(a', 'a)', '(b', '(a', 'a)', 'b)', '(b', 'b)', 'a)'])
y = torch.tensor(test1 + test2 + test3)
sent_len = len(test1)
batch_size = int(len(y) / sent_len)
vocab_size = len(vocab)
y_pred = torch.ones((batch_size * sent_len, vocab_size)) / 4
y_pred[:, 0:open_idx[0]] = 0.0
start = time.time()
# Test 1: 0%
ldpa = get_LDPA_counts(y=y, y_pred=y_pred, batch_size=batch_size, max_dist=sent_len, thresh=0.8)
assert(all([ldpa[i][1] == 0 for i in range(len(ldpa))]))
# Test 2: 100%
ldpa = get_LDPA_counts(y=y, y_pred=y_pred, batch_size=batch_size, max_dist=sent_len, thresh=0.5)
assert(all([ldpa[i][1] / ldpa[i][0] == 1 for i in range(len(ldpa)) if ldpa[i][0]]))
# Test 3: mix
y_pred[:, -1] = 0.1
ldpa = get_LDPA_counts(y=y, y_pred=y_pred, batch_size=batch_size, max_dist=sent_len, thresh=0.5)
ratios = [ldpa[i][1] / ldpa[i][0] for i in range(len(ldpa)) if ldpa[i][0]]
assert(ratios == [5/9, 1/2, 0, 1, 1/2]), "Expected {} \n Got {}".format([5/9, 1/2, 0, 1, 1/2], ratios)
# Test 4: robust random
test1 = vocab.words2indices(['(a','a)', '(b', 'b)'])
test2 = vocab.words2indices(['(a','(b', 'b)', 'a)'])
y = torch.tensor(test1 + test2)
sent_len = len(test1)
batch_size = int(len(y) / sent_len)
y_pred = torch.ones((batch_size * sent_len, vocab_size)) * 0.001
y_pred[1, vocab.word2id['a)']] = 1 # at dist 1, 1 above thresh
y_pred[3, vocab.word2id['a)']] = 1 # at dist 1, 0 above thresh
y_pred[6, vocab.word2id['b)']] = 1 # at dist 1, 1 above thresh
y_pred[7, vocab.word2id['a)']] = 1 # at dist 3, 1 above thresh
ldpa = get_LDPA_counts(y=y, y_pred=np.log(y_pred), batch_size=batch_size, max_dist=sent_len, thresh=0.8)
assert(ldpa[1][0] == 3), "Expect 3 close at dist 1, got {}".format(ldpa[1][0]) # 3 closed at dist 1
assert(ldpa[1][1] == 2), "Expect 2 above thresh at dist 1, got {}".format(ldpa[1][1]) # 2 of which above threshold
assert(ldpa[3][0] == 1), "Expect 1 close at dist 3, got {}".format(ldpa[3][0]) # 1 closed at dist 3
assert(ldpa[3][1] == 1), "Expect 1 above thresh at dist 3, got {}".format(ldpa[3][1]) # 1 of which above threshold
# Test 5: validate wcpa
val_dataset = CustomDataset("./data/mbounded-dyck-k/m4/dev.formal.txt", json_path_override="./data/mbounded-dyck-k/m4/train.formal.json")
x, y = val_dataset[0]
max_sents_len = len(x)
total_ldpa_counts = np.zeros((max_sents_len, 2), dtype=np.int64)
# fabricate y_pred for 1st sample of batch 1
y_pred = np.ones((len(y), vocab_size)) * 0.001
y_pred[1, close_idx[0]] = 1 # dist 1 above thresh
y_pred[5, close_idx[1]] = 1 # dist 1 above thresh
y_pred[14, close_idx[1]] = 1 # dist 1 not above thresh
y = torch.tensor(y)
y_pred = torch.tensor(np.log(y_pred))
ldpa_counts = get_LDPA_counts(y=y, y_pred=y_pred, batch_size=1, max_dist=max_sents_len)
total_ldpa_counts += ldpa_counts
# ground truth counts
gt_ldpa_counts = np.zeros((max_sents_len, 2), dtype=np.int64)
gt_ldpa_counts[1, 0] = 32
gt_ldpa_counts[3, 0] = 11
gt_ldpa_counts[5, 0] = 3
gt_ldpa_counts[9, 0] = 2
gt_ldpa_counts[11, 0] = 2
gt_ldpa_counts[13, 0] = 1
gt_ldpa_counts[23, 0] = 1
gt_ldpa_counts[25, 0] = 1
gt_ldpa_counts[31, 0] = 1
gt_ldpa_counts[1, 1] = 2
assert(np.array_equal(gt_ldpa_counts[:, 0], total_ldpa_counts[:, 0])), \
"first field not equal! \n gt_ldpa_counts {} \n total_ldpa_counts {} \n".format(gt_ldpa_counts[:, 0], total_ldpa_counts[:, 0])
assert(np.array_equal(gt_ldpa_counts[:, 1], total_ldpa_counts[:, 1])), \
"second field not equal! \n gt_ldpa_counts {} \n total_ldpa_counts {} \n".format(gt_ldpa_counts[:, 1], total_ldpa_counts[:, 1])
# fabricate y_pred for 2nd sample of batch 1
x, y = val_dataset[1]
y_pred = np.ones((len(y), vocab_size)) * 0.001
y_pred[3, close_idx[0]] = 1 # dist 1 above thresh
y_pred[5, close_idx[0]] = 1 # dist 1 above thresh
y_pred[8, close_idx[1]] = 1 # dist 1 above thresh
y_pred[9, close_idx[0]] = 1 # dist 3 above thresh
y_pred[45, close_idx[0]] = 1 # dist 45 above thresh
y = torch.tensor(y)
y_pred = torch.tensor(np.log(y_pred))
ldpa_counts = get_LDPA_counts(y=y, y_pred=y_pred, batch_size=1, max_dist=max_sents_len)
total_ldpa_counts += ldpa_counts
# ground truth counts
gt_ldpa_counts[1, 0] += 28
gt_ldpa_counts[3, 0] += 6
gt_ldpa_counts[5, 0] += 5
gt_ldpa_counts[7, 0] += 1
gt_ldpa_counts[9, 0] += 2
gt_ldpa_counts[23, 0] += 1
gt_ldpa_counts[33, 0] += 1
gt_ldpa_counts[45, 0] += 1
gt_ldpa_counts[1, 1] += 3
gt_ldpa_counts[3, 1] += 1
gt_ldpa_counts[45, 1] += 1
assert(np.array_equal(gt_ldpa_counts[:, 0], total_ldpa_counts[:, 0])), \
"first field not equal! \n gt_ldpa_counts {} \n total_ldpa_counts {} \n".format(gt_ldpa_counts[:, 0], total_ldpa_counts[:, 0])
assert(np.array_equal(gt_ldpa_counts[:, 1], total_ldpa_counts[:, 1])), \
"second field not equal! \n gt_ldpa_counts {} \n total_ldpa_counts {} \n".format(gt_ldpa_counts[:, 1], total_ldpa_counts[:, 1])
valid_dist = np.where(total_ldpa_counts[:, 0] > 0)
ldpa = total_ldpa_counts[valid_dist, 1] / total_ldpa_counts[valid_dist, 0]
gt_ldpa = np.array([1/12, 1/17, 1])
ldpa = ldpa[np.nonzero(ldpa)]
assert(np.array_equal(ldpa, gt_ldpa)), "gt_ldpa {} \n ldpa {}".format(gt_ldpa, ldpa)
# time test
for i in range(2000):
ldpa_counts = get_LDPA_counts(y=y, y_pred=y_pred, batch_size=1, max_dist=max_sents_len)
print("All Sanity Checks Passed! Took time: ", time.time() - start)
print ("-"*80)
if __name__ == "__main__":
vocab = Vocab(file_path="data/mbounded-dyck-k/m4/train.formal.json")
if sys.argv[1] == 'dist':
test_distances(vocab)
if sys.argv[1] == 'ldpa':
test_ldpa(vocab)