-
Notifications
You must be signed in to change notification settings - Fork 12
/
tuple_embedding_models.py
328 lines (246 loc) · 14.6 KB
/
tuple_embedding_models.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
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
#GiG
from collections import Counter
import random
import numpy as np
from sklearn.decomposition import TruncatedSVD
import torch
import fasttext
from torchtext.data import get_tokenizer
import dl_models
from configurations import *
#This is the Abstract Base Class for all Tuple Embedding models
class ABCTupleEmbedding:
def __init__(self):
pass
#This function is used as a preprocessing step
# this could be used to compute frequencies, train a DL model etc
def preprocess(self, list_of_tuples):
pass
#This function computes the tuple embedding.
# Given a list of strings, it returns a list of tuple embeddings
# each tuple embedding is 1D numpy ndarray
def get_tuple_embedding(self, list_of_tuples):
pass
#This function sends a list of words and outputs a list of word embeddings
def get_word_embedding(self, list_of_words):
pass
#This is a simple method for aggregation
# This computes the embedding of a tuple as the average of the constituent word embeddings.
#By default, it uses fastText model
class AverageEmbedding(ABCTupleEmbedding):
def __init__(self):
super().__init__()
print("Loading FastText model")
self.word_embedding_model = fasttext.load_model(FASTTEXT_EMBEDDIG_PATH)
self.dimension_size = EMB_DIMENSION_SIZE
self.tokenizer = get_tokenizer("basic_english")
#There is no pre processing needed for Average Embedding
def preprocess(self, list_of_tuples):
pass
#list_of_strings is an Iterable of tuples as strings
def get_tuple_embedding(self, list_of_tuples):
#This is an one liner for efficiency
# returns a list of word embeddings for each token in a tuple
# self.word_embedding_model.get_word_vector(token) for token in self.tokenizer(tuple)
# next we convert the list of word embeddings to a numpy array using np.array(list)
# next we compute the element wise mean via np.mean(np.array([embeddings]), axis=0)
# we repeat this process for all tuples in list_of_tuples
# for tuple in list_of_tuples
# then convert everything to a numpy array at the end through np.array([ ])
# So if you send N tuples, then this will return a numpy matrix of size N x D where D is embedding dimension
average_embeddings = np.array([np.mean(np.array([self.word_embedding_model.get_word_vector(token) for token in self.tokenizer(_tuple)]), axis=0) for _tuple in list_of_tuples])
return average_embeddings
#Return word embeddings for a list of words
def get_word_embedding(self, list_of_words):
return [self.word_embedding_model.get_word_vector(word) for word in list_of_words]
class SIFEmbedding(ABCTupleEmbedding):
#sif_weighting_param is a parameter in the SIF weighting scheme, usually in the range [3e-5, 3e-3]
# the SIF paper set the default value to 1e-3
# remove_pc is a Boolean parameter that controls whether to remove the first principal component or not
# min_freq: if a word is too infrequent (ie frequency < min_freq), set a SIF weight of 1.0 else apply the formula
# The SIF paper applies this formula if the word is not the top-N most frequent
def __init__(self, sif_weighting_param=1e-3, remove_pc=True, min_freq=0):
super().__init__()
print("Loading FastText model")
self.word_embedding_model = fasttext.load_model(FASTTEXT_EMBEDDIG_PATH)
self.dimension_size = EMB_DIMENSION_SIZE
self.tokenizer = get_tokenizer("basic_english")
#Word to frequency counter
self.word_to_frequencies = Counter()
#Total number of distinct tokens
self.total_tokens = 0
self.sif_weighting_param = sif_weighting_param
self.remove_pc = remove_pc
self.min_freq = min_freq
self.token_weight_dict = {}
#There is no pre processing needed for Average Embedding
def preprocess(self, list_of_tuples):
for tuple_as_str in list_of_tuples:
self.word_to_frequencies.update(self.tokenizer(tuple_as_str))
#Count all the tokens in each tuples
self.total_tokens = sum(self.word_to_frequencies.values())
#Compute the weight for each token using the SIF scheme
a = self.sif_weighting_param
for word, frequency in self.word_to_frequencies.items():
if frequency >= self.min_freq:
self.token_weight_dict[word] = a / (a + frequency / self.total_tokens)
else:
self.token_weight_dict[word] = 1.0
#list_of_strings is an Iterable of tuples as strings
# See the comments of AverageEmbedding's get_tuple_embedding for details about how this works
def get_tuple_embedding(self, list_of_tuples):
num_tuples = len(list_of_tuples)
tuple_embeddings = np.zeros((num_tuples, self.dimension_size))
for index, _tuple in enumerate(list_of_tuples):
#Compute a weighted average using token_weight_dict
tuple_embeddings[index, :] = np.mean(np.array([self.word_embedding_model.get_word_vector(token) * self.token_weight_dict[token] for token in self.tokenizer(_tuple)]), axis=0)
#From the code of the SIF paper at
# https://github.com/PrincetonML/SIF/blob/master/src/SIF_embedding.py
if self.remove_pc:
svd = TruncatedSVD(n_components=1, n_iter=7, random_state=0)
svd.fit(tuple_embeddings)
pc = svd.components_
sif_embeddings = tuple_embeddings - tuple_embeddings.dot(pc.transpose()) * pc
else:
sif_embeddings = tuple_embeddings
return sif_embeddings
def get_word_embedding(self, list_of_words):
return [self.word_embedding_model.get_word_vector(word) for word in list_of_words]
class AutoEncoderTupleEmbedding(ABCTupleEmbedding):
def __init__(self, hidden_dimensions=(2*AE_EMB_DIMENSION_SIZE, AE_EMB_DIMENSION_SIZE)):
super().__init__()
self.input_dimension = EMB_DIMENSION_SIZE
self.hidden_dimensions = hidden_dimensions
self.sif_embedding_model = SIFEmbedding()
#This function is used as a preprocessing step
# this could be used to compute frequencies, train a DL model etc
def preprocess(self, list_of_tuples):
print("Training AutoEncoder model")
self.sif_embedding_model.preprocess(list_of_tuples)
embedding_matrix = self.sif_embedding_model.get_tuple_embedding(list_of_tuples)
trainer = dl_models.AutoEncoderTrainer (self.input_dimension, self.hidden_dimensions)
self.autoencoder_model = trainer.train(embedding_matrix, num_epochs=NUM_EPOCHS, batch_size=BATCH_SIZE)
#This function computes the tuple embedding.
# Given a list of strings, it returns a list of tuple embeddings
# each tuple embedding is 1D numpy ndarray
def get_tuple_embedding(self, list_of_tuples):
embedding_matrix = torch.tensor(self.sif_embedding_model.get_tuple_embedding(list_of_tuples)).float()
return self.autoencoder_model.get_tuple_embedding(embedding_matrix)
#This function sends a list of words and outputs a list of word embeddings
def get_word_embedding(self, list_of_words):
embedding_matrix = torch.tensor(self.sif_embedding_model.get_tuple_embedding(list_of_words)).float()
return self.autoencoder_model.get_tuple_embedding(embedding_matrix)
#This function is used by both CTT and Hybrid - so it is put outside of any class
#It takes a list of tuple strings and outputs three lists (T, T', L)
# t_i \in T and t'_i \in T' are (potentially perturbed) tuples
# and l_i is a label denoting whether they are duplicates or not
# for each tuple t in list_of_tuples,
# we generate synth_tuples_per_tuple positive tuple pairs
# and synth_tuples_per_tuple * pos_to_neg_ratio negative tuple pairs
def generate_synthetic_training_data(list_of_tuples, synth_tuples_per_tuple=5,
pos_to_neg_ratio=1, max_perturbation=0.4):
num_positives_per_tuple = synth_tuples_per_tuple
num_negatives_per_tuple = synth_tuples_per_tuple * pos_to_neg_ratio
num_tuples = len(list_of_tuples)
total_number_of_elems = num_tuples * (num_positives_per_tuple + num_negatives_per_tuple)
#We create three lists containing T, T' and L respectively
#We use the following format: first num_tuples * num_positives_per_tuple correspond to T
# and the remaining correspond to T'
left_tuple_list = [None for _ in range(total_number_of_elems)]
right_tuple_list = [None for _ in range(total_number_of_elems)]
label_list = [0 for _ in range(total_number_of_elems) ]
random.seed(RANDOM_SEED)
tokenizer = get_tokenizer("basic_english")
for index in range(len(list_of_tuples)):
tokenized_tuple = tokenizer(list_of_tuples[index])
max_tokens_to_remove = int(len(tokenized_tuple) * max_perturbation)
training_data_index = index * (num_positives_per_tuple + num_negatives_per_tuple)
#Create num_positives_per_tuple tuple pairs with positive label
for temp_index in range(num_positives_per_tuple):
tokenized_tuple_copy = tokenized_tuple[:]
#If the tuple has 10 words and max_tokens_to_remove is 0.5, then we can remove at most 5 words
# we choose a random number between 0 and 5.
# suppose it is 3. Then we randomly remove 3 words
num_tokens_to_remove = random.randint(0, max_tokens_to_remove)
for _ in range(num_tokens_to_remove):
#randint is inclusive. so randint(0, 5) can return 5 also
tokenized_tuple_copy.pop( random.randint(0, len(tokenized_tuple_copy) - 1) )
left_tuple_list[training_data_index] = list_of_tuples[index]
right_tuple_list[training_data_index] = ' '.join(tokenized_tuple_copy)
label_list[training_data_index] = 1
training_data_index += 1
for temp_index in range(num_negatives_per_tuple):
left_tuple_list[training_data_index] = list_of_tuples[index]
right_tuple_list[training_data_index] = random.choice(list_of_tuples)
label_list[training_data_index] = 0
training_data_index += 1
return left_tuple_list, right_tuple_list, label_list
class CTTTupleEmbedding(ABCTupleEmbedding):
def __init__(self, hidden_dimensions=(2*AE_EMB_DIMENSION_SIZE, AE_EMB_DIMENSION_SIZE),
synth_tuples_per_tuple=5, pos_to_neg_ratio=1, max_perturbation=0.4):
super().__init__()
self.input_dimension = EMB_DIMENSION_SIZE
self.hidden_dimensions = hidden_dimensions
self.synth_tuples_per_tuple = synth_tuples_per_tuple
self.pos_to_neg_ratio = pos_to_neg_ratio
self.max_perturbation = max_perturbation
#By default, CTT uses SIF as the aggregator
self.sif_embedding_model = SIFEmbedding()
#This function is used as a preprocessing step
# this could be used to compute frequencies, train a DL model etc
def preprocess(self, list_of_tuples):
print("Training CTT model")
self.sif_embedding_model.preprocess(list_of_tuples)
left_tuple_list, right_tuple_list, label_list = generate_synthetic_training_data(list_of_tuples,
self.synth_tuples_per_tuple, self.pos_to_neg_ratio, self.max_perturbation)
self.left_embedding_matrix = self.sif_embedding_model.get_tuple_embedding(left_tuple_list)
self.right_embedding_matrix = self.sif_embedding_model.get_tuple_embedding(right_tuple_list)
self.label_list = label_list
trainer = dl_models.CTTModelTrainer (self.input_dimension, self.hidden_dimensions)
self.ctt_model = trainer.train(self.left_embedding_matrix, self.right_embedding_matrix, self.label_list,
num_epochs=NUM_EPOCHS, batch_size=BATCH_SIZE)
#This function computes the tuple embedding.
# Given a list of strings, it returns a list of tuple embeddings
# each tuple embedding is 1D numpy ndarray
def get_tuple_embedding(self, list_of_tuples):
embedding_matrix = torch.tensor(self.sif_embedding_model.get_tuple_embedding(list_of_tuples)).float()
return embedding_matrix
#This function sends a list of words and outputs a list of word embeddings
def get_word_embedding(self, list_of_words):
embedding_matrix = torch.tensor(self.sif_embedding_model.get_tuple_embedding(list_of_words)).float()
return embedding_matrix
#Hybrid is same as CTT except using Autoencoder for aggregator
class HybridTupleEmbedding(ABCTupleEmbedding):
def __init__(self, hidden_dimensions=(2*AE_EMB_DIMENSION_SIZE, AE_EMB_DIMENSION_SIZE),
synth_tuples_per_tuple=5, pos_to_neg_ratio=1, max_perturbation=0.4):
super().__init__()
self.input_dimension = EMB_DIMENSION_SIZE
self.hidden_dimensions = hidden_dimensions
self.synth_tuples_per_tuple = synth_tuples_per_tuple
self.pos_to_neg_ratio = pos_to_neg_ratio
self.max_perturbation = max_perturbation
#Hybrid uses autoencoder instead of SIF aggregator
self.autoencoder_embedding_model = AutoEncoderTupleEmbedding()
#This function is used as a preprocessing step
# this could be used to compute frequencies, train a DL model etc
def preprocess(self, list_of_tuples):
print("Training CTT model")
self.autoencoder_embedding_model.preprocess(list_of_tuples)
left_tuple_list, right_tuple_list, label_list = generate_synthetic_training_data(list_of_tuples,
self.synth_tuples_per_tuple, self.pos_to_neg_ratio, self.max_perturbation)
self.left_embedding_matrix = self.autoencoder_embedding_model.get_tuple_embedding(left_tuple_list)
self.right_embedding_matrix = self.autoencoder_embedding_model.get_tuple_embedding(right_tuple_list)
self.label_list = label_list
trainer = dl_models.CTTModelTrainer (self.input_dimension, self.hidden_dimensions)
self.ctt_model = trainer.train(self.left_embedding_matrix, self.right_embedding_matrix, self.label_list,
num_epochs=NUM_EPOCHS, batch_size=BATCH_SIZE)
#This function computes the tuple embedding.
# Given a list of strings, it returns a list of tuple embeddings
# each tuple embedding is 1D numpy ndarray
def get_tuple_embedding(self, list_of_tuples):
embedding_matrix = torch.tensor(self.autoencoder_embedding_model.get_tuple_embedding(list_of_tuples)).float()
return embedding_matrix
#This function sends a list of words and outputs a list of word embeddings
def get_word_embedding(self, list_of_words):
embedding_matrix = torch.tensor(self.autoencoder_embedding_model.get_tuple_embedding(list_of_words)).float()
return embedding_matrix