forked from wolferobert3/vast_aaai_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.py
179 lines (134 loc) · 5.73 KB
/
helper_functions.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
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
import numpy as np
from os import path
from sklearn.decomposition import PCA
import pickle
import random
import torch
from matplotlib import pyplot as plt
#Helper Functions
def load_term_object(target, directory):
with open(path.join(directory, target + '-object.pkl'), 'rb') as object_reader:
return pickle.load(object_reader)
#Math Functions
def cosine_similarity(a, b):
return ((np.dot(a, b)) / (np.sqrt(np.dot(a, a)) * np.sqrt(np.dot(b, b))))
def std_deviation(J):
mean_J = np.mean(J)
var_J = sum([(j - mean_J)**2 for j in J])
return (np.sqrt(var_J / (len(J)-1)))
def create_permutation(a, b):
permutation = random.sample(a+b, len(a+b))
return permutation[:int(len(permutation)*.5)], permutation[int(len(permutation)*.5):]
#Returns a list of vectors from layers of a language model
def get_embeddings(term, context, model, tokenizer, layers = tuple(range(13)), tensor_type = 'tf', prev_token = False):
context += ' '
encoding = tokenizer.encode(term, add_special_tokens = False, add_prefix_space=True)
encoded_context = tokenizer.encode(context, add_special_tokens=True)
if prev_token:
positions = [encoded_context.index(encoding[0])]
if positions[0] == 0:
context = ' ' + context
else:
positions[0] -= 1
else:
positions = []
if len(encoding) == 1:
positions = [encoded_context.index(encoding[0])]
else:
for i in range(len(encoded_context)):
if encoding[0] == encoded_context[i] and encoding[1:] == encoded_context[i+1:i+len(encoding)]:
positions = [j for j in range(i, i + len(encoding))]
if not positions:
context = context.replace(term, ' ' + term)
encoded_context = tokenizer.encode(context, add_special_tokens = True)
if len(encoding) == 1:
positions = [encoded_context.index(encoding[0])]
else:
for i in range(len(encoded_context)):
if encoding[0] == encoded_context[i] and encoding[1:] == encoded_context[i+1:i+len(encoding)]:
positions = [j for j in range(i, i + len(encoding))]
inputs = tokenizer(context, return_tensors = tensor_type)
if tensor_type == 'tf':
output_ = model(inputs)
if tensor_type == 'pt':
with torch.no_grad():
output_ = model(**inputs)
np.squeeze(output_)
embeddings = []
for layer in layers:
target_embedding = []
for position in positions:
sub_embedding = np.array(output_[-1][layer][0][position])
target_embedding.append(sub_embedding)
embeddings.append(target_embedding)
return embeddings
def get_last_token_embeddings(context, model, tokenizer, layers = tuple(range(13)), tensor_type = 'tf'):
inputs = tokenizer(context, return_tensors = tensor_type)
if tensor_type == 'tf':
output_ = model(inputs)
if tensor_type == 'pt':
with torch.no_grad():
output_ = model(**inputs)
np.squeeze(output_)
embeddings = []
for layer in layers:
embeddings.append(np.array(output_[-1][layer][0][-1]))
return embeddings
def form_representations(cwe_list, rep_type = 'Last'):
representations = []
if rep_type == 'Last':
for vector in cwe_list:
representations.append(vector[-1])
return representations
if rep_type == 'First':
for vector in cwe_list:
representations.append(vector[0])
return representations
if rep_type == 'Mean':
for vector in cwe_list:
cwe_arr = np.array([i for i in vector])
representations.append(np.mean(cwe_arr, axis = 0))
return representations
if rep_type == 'Max':
for vector in cwe_list:
cwe_arr = np.array([i for i in vector])
representations.append(np.nanmax(cwe_arr, axis = 0))
return representations
if rep_type == 'Min':
for vector in cwe_list:
cwe_arr = np.array([i for i in vector])
representations.append(np.nanmin(cwe_arr, axis = 0))
return representations
if rep_type == 'Abs_Max':
for vector in cwe_list:
cwe_arr = np.array([i for i in vector])
max_arr = np.nanmax(cwe_arr, axis = 0)
min_arr = np.nanmin(cwe_arr, axis = 0)
max_ind = abs(max_arr) >= abs(min_arr)
min_ind = abs(min_arr) < abs(max_arr)
abs_max = np.zeros(max_arr.shape)
abs_max[max_ind] = max_arr[max_ind]
abs_max[min_ind] = min_arr[min_ind]
representations.append(abs_max)
return representations
if rep_type == 'Concat':
for vector in cwe_list:
concat_cwe = np.array(vector[0])
if len(vector) > 1:
for subword in vector[1:]:
concat_cwe = np.append(concat_cwe, subword)
representations.append(concat_cwe)
return representations
return representations
def pca_transform(embedding_array, pcs, subtract_mean = True):
pca = PCA(n_components = pcs)
if subtract_mean:
common_mean = np.mean(embedding_array, axis=0)
transformed_array = embedding_array - common_mean
else:
transformed_array = np.array(embedding_array, copy = True)
pcs = pca.fit_transform(transformed_array)
pcas = pca.components_
pc_remove = np.matmul(np.matmul(transformed_array, pcas.T), pcas)
transformed_embeddings = transformed_array - pc_remove
return transformed_embeddings, pcs