-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
365 lines (316 loc) · 12.2 KB
/
Copy pathutils.py
File metadata and controls
365 lines (316 loc) · 12.2 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from collections import Counter
from collections import defaultdict
import time
import json
import pickle
import time
import tensorflow as tf
import numpy as np
import pickle as pkl
import csv
import inflect
import sys
import os
infl_eng = inflect.engine()
def set_up_environment(mem_frac=None, visible_devices=None, min_log_level='3'):
"""
A helper function to set up a tensorflow environment.
Args:
mem_frac: Fraction of memory to limit the gpu to. If set to None,
turns on memory growth instead.
visible_devices: A string containing a comma-separated list of
integers designating the gpus to run on.
min_log_level: One of 0, 1, 2, or 3.
"""
if visible_devices is not None:
os.environ['CUDA_VISIBLE_DEVICES'] = visible_devices
os.environ['TF_CPP_MIN_LOG_LEVEL'] = str(min_log_level)
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
if mem_frac is not None:
memory_limit = int(10000 * mem_frac)
config = [
tf.config.experimental.VirtualDeviceConfiguration(
memory_limit=memory_limit)
]
tf.config.experimental.set_virtual_device_configuration(
gpu, config)
else:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as error:
print(error)
print(gpus)
tf.config.experimental.set_visible_devices(gpus, 'GPU')
gpus = tf.config.experimental.list_logical_devices('GPU')
print(gpus)
## a dictionary for a list of plural/singular verbs for the purpose of having
# an aggregated group of verbs as qoi, result not included in paper.
verb_dict = {
'sing': [
2573, 3216, 3268, 3504, 3658, 4212, 4832, 5176, 5829, 5975, 6526, 7365,
7502, 7719, 8289, 8451, 8480, 9631, 10217, 10229, 10854, 11652, 11680,
12237, 12671, 14523, 14747, 15701, 16412, 17144, 19906, 21145, 22114,
25126
],
'plur': [
2147, 2298, 2444, 2448, 2693, 2868, 2991, 2994, 3191, 3198, 3233, 3280,
3328, 3637, 4133, 4400, 4553, 4682, 4756, 4839, 4982, 5376, 5437, 5993,
6073, 6682, 6869, 6978, 7180, 7324, 8155, 10436, 11245, 14315
]
}
sentence_types = [
'obj_rel_across_anim', 'subj_rel', 'sent_comp', 'prep_anim',
'reflexives_across', 'refl_across_gender'
]
num_words = [11, 11, 10, 11, 11, 13]
mask_indices = [7, 7, 6, 7, 8, 10]
example_templates = [{
'ss': 'the (author) that the (guard) (likes) [MASK] (young) .',
'sp': 'the (author) that the (guards) (like) [MASK] (young) .',
'ps': 'the (authors) that the (guard) (likes) [MASK] (young) .',
'pp': 'the (authors) that the (guards) (like) [MASK] (young) .'
}, {
'ss': 'the (author) that (likes) the (guard) [MASK] (young) .',
'sp': 'the (author) that (likes) the (guards) [MASK] (young) .',
'ps': 'the (authors) that (like) the (guard) [MASK] (young) .',
'pp': 'the (authors) that (like) the (guards) [MASK] (young) .'
}, {
'ss': 'the (author) (said) the (guard) [MASK] (young) .',
'sp': 'the (author) (said) the (guards) [MASK] (young) .',
'ps': 'the (authors) (said) the (guard) [MASK] (young) .',
'pp': 'the (authors) (said) the (guards) [MASK] (young) .'
}, {
'ss': 'the (author) (next) (to) the (guard) [MASK] (young) .',
'sp': 'the (author) (next) (to) the (guards) [MASK] (young) .',
'ps': 'the (authors) (next) (to) the (guard) [MASK] (young) .',
'pp': 'the (authors) (next) (to) the (guards) [MASK] (young) .'
}, {
'ss': 'the (author) that the (guard) (likes) (hurt) [MASK] .',
'sp': 'the (author) that the (guards) (like) (hurt) [MASK] .',
'ps': 'the (authors) that the (guard) (likes) (hurt) [MASK] .',
'pp': 'the (authors) that the (guards) (like) (hurt) [MASK] .'
}, {
'female_male':
'(some) (wizard) (who) (can) (dress) (our) (man) (can) (clean) [MASK]',
'male_male':
'(some) (king) (who) (can) (dress) (our) (man) (can) (clean) [MASK]',
'female_female':
'(some) (wizard) (who) (can) (dress) (our) (woman) (can) (clean) [MASK]',
'male_female':
'(some) (king) (who) (can) (dress) (our) (man) (can) (clean) [MASK]'
}]
example_token_templates = [
'the1 SUBJ that the2 ATTRACTOR VERB [MASK] ADJ .',
'the1 SUBJ that VERB the2 ATTRACTOR [MASK] ADJ .',
'the2 ATTRACTOR VERB the1 SUBJ [MASK] ADJ .',
'the1 SUBJ PREP1 PREP2 the2 ATTRACTOR [MASK] ADJ .',
'the1 SUBJ that the2 ATTRACTOR VERB1 VERB2 [MASK] .',
'DEF SUBJ who VERB1 VERB2 DEF ATTRACTOR VERB2 VERB4 [MASK]'
]
def convert_sentence_type(st):
if 'female' in st or 'male' in st:
return st
else:
st = [s[0] for s in st.split('_') if s == 'sing' or s == 'plur']
return ''.join(st)
def load_json(path):
with tf.io.gfile.GFile(path, 'r') as f:
return json.load(f)
def write_json(o, path):
tf.io.gfile.makedirs(path.rsplit('/', 1)[0])
with tf.io.gfile.GFile(path, 'w') as f:
json.dump(o, f)
def load_pickle(path):
with tf.io.gfile.GFile(path, 'rb') as f:
return pickle.load(f)
def write_pickle(o, path):
if '/' in path:
tf.io.gfile.makedirs(path.rsplit('/', 1)[0])
with tf.io.gfile.GFile(path, 'wb') as f:
pickle.dump(o, f, -1)
def logged_loop(iterable, n=None, **kwargs):
if n is None:
n = len(iterable)
ll = LoopLogger(n, **kwargs)
for i, elem in enumerate(iterable):
ll.update(i + 1)
yield elem
def gen_inflect_from_vocab(vocab_file, freq_threshold=1000):
vbp = {}
vbz = {}
nn = {}
nns = {}
from_pos = {'NNS': nns, 'NN': nn, 'VBP': vbp, 'VBZ': vbz}
for line in open(vocab_file):
if line.startswith(' '): # empty string token
continue
word, pos, count = line.strip().split()
count = int(count)
if len(word) > 1 and pos in from_pos and count >= freq_threshold:
from_pos[pos][word] = count
verb_infl = {'VBP': 'VBZ', 'VBZ': 'VBP'}
for word, count in vbz.items():
candidate = infl_eng.plural_verb(word)
if candidate in vbp:
verb_infl[candidate] = word
verb_infl[word] = candidate
noun_infl = {'NN': 'NNS', 'NNS': 'NN'}
for word, count in nn.items():
candidate = infl_eng.plural_noun(word)
if candidate in nns:
noun_infl[candidate] = word
noun_infl[word] = candidate
noun_infl['that'] = 'those'
return verb_infl, noun_infl
vinfl, ninfl = gen_inflect_from_vocab('wiki.vocab')
def get_accuracy(lm_output, nround=3):
return np.round(
((lm_output[:, 0] - lm_output[:, 1]) > 0).sum() / len(lm_output),
nround)
def rmse(x1, x2):
return np.mean(np.sqrt((x1 - x2)**2), -1)
def avg_diff_p(x1, x2):
return np.mean(np.abs(x1 - x2) / np.abs(x2), -1) * 100
def fill_text_blank(tokens, num_word):
return [
str(i) + '/' + e
for i, e in enumerate(tokens + [' '] * (num_word - len(tokens)))
]
class LoopLogger(object):
"""Class for printing out progress/ETA for a loop."""
def __init__(self,
max_value=None,
step_size=1,
n_steps=25,
print_time=True):
self.max_value = max_value
if n_steps is not None:
self.step_size = max(1, max_value // n_steps)
else:
self.step_size = step_size
self.print_time = print_time
self.n = 0
self.start_time = time.time()
def step(self, values=None):
self.update(self.n + 1, values)
def update(self, i, values=None):
self.n = i
if self.n % self.step_size == 0 or self.n == self.max_value:
if self.max_value is None:
msg = 'On item ' + str(self.n)
else:
msg = '{:}/{:} = {:.1f}%'.format(
self.n, self.max_value, 100.0 * self.n / self.max_value)
if self.print_time:
time_elapsed = time.time() - self.start_time
time_per_step = time_elapsed / self.n
msg += ', ELAPSED: {:.1f}s'.format(time_elapsed)
msg += ', ETA: {:.1f}s'.format(
(self.max_value - self.n) * time_per_step)
if values is not None:
for k, v in values:
msg += ' - ' + str(k) + ': ' + (
'{:.4f}'.format(v) if isinstance(v, float) else str(v))
print(msg)
def load_ml_pickle(pkl_file, outfile_path=''):
with open(pkl_file, 'rb') as f:
df = pkl.load(f)
task = pkl_file.split('.')[-2].split('/')[-1]
print('writing to: ', outfile_path + task + '.tsv')
with open(outfile_path + task + '.tsv', 'wt') as out_file:
tsv_writer = csv.writer(out_file, delimiter='\t')
for typ in df:
for d in df[typ]:
tsv_writer.writerow([task, typ, d[0], d[1]])
def load_ml(tsv_file="marvin_linzen_dataset.tsv"):
cc = Counter()
out = []
for line in open(tsv_file):
case = line.strip().split("\t")
# print(case)
cc[case[1]] += 1
g, ug = case[-2], case[-1]
if 'taxi driver' in g or 'admire' in g or 'swim' in g:
continue
g = g.split()
ug = ug.split()
assert (len(g) == len(ug)), (g, ug)
diffs = [i for i, pair in enumerate(zip(g, ug)) if pair[0] != pair[1]]
if (len(diffs) != 1):
#print(diffs)
#print(g,ug)
continue
assert (len(diffs) == 1), diffs
gv = g[diffs[0]] # good
ugv = ug[diffs[0]] # bad
g[diffs[0]] = "***mask***"
g.append(".")
out.append([case[0], case[1], " ".join(g), gv, ugv])
return out
def load_marvin():
cc = Counter()
# note: I edited the LM_Syneval/src/make_templates.py script, and run "python LM_Syneval/src/make_templates.py LM_Syneval/data/templates/ > marvin_linzen_dataset.tsv"
out = load_ml()
s_features = []
file_names = ['refl.3', 'refl.4']
features_who = []
for f in out:
if f[0] in [
'subj_rel', 'obj_rel_across_anim', 'reflexives_across',
'obj_rel_within_anim'
]:
f_who_string = f[2].split()
f_who_string[f_who_string.index('that')] = 'who'
features_who.append(
[f[0] + '_who', f[1], ' '.join(f_who_string), f[3], f[4]])
# print(features_who[0])
for fn in file_names:
with open('../bert-opensesame/pytorch_pretrained_bert/data_refl/' + fn,
'r') as f:
rgs = [s.split('\t')[0] for s in f.readlines()]
for rg in rgs:
if rg.split()[-1] == 'himself':
if '3' in fn:
f1 = 'male_male'
elif '4' in fn:
f1 = 'male_female'
correct = 'himself'
incorrect = 'herself'
else:
if '3' in fn:
f1 = 'female_female'
elif '4' in fn:
f1 = 'female_male'
correct = 'herself'
incorrect = 'himself'
slst = rg.split()
slst[-1] = '***mask***'
slst.append('.')
s_features.append([
'refl_across_gender', f1, ' '.join(slst), correct, incorrect
])
return out + features_who + s_features + [[
'only_mask' + str(i), 'ph_ph', '***mask*** ' * i, 'is', 'are'
] for i in np.arange(7, 13)]
def eval_marvin():
o = load_marvin()
print(len(o), file=sys.stderr)
rc = defaultdict(Counter)
tc = Counter()
start = time.time()
for i, (case, tp, s, g, b) in enumerate(o):
ps = get_probs_for_words(s, g, b)
if ps is None:
ps = [0, 1]
gp = ps[0]
bp = ps[1]
print(gp > bp, case, tp, g, b, s)
if i % 100 == 0:
print(i, time.time() - start, file=sys.stderr)
start = time.time()
sys.stdout.flush()