forked from Glaciohound/VCML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embedding.py
453 lines (383 loc) · 14.3 KB
/
embedding.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File : embedding.py
# Author : Chi Han, Jiayuan Mao
# Email : haanchi@gmail.com, maojiayuan@gmail.com
# Date : 23.07.2019
# Last Modified Date: 19.11.2019
# Last Modified By : Chi Han
#
# This file is part of the VCML codebase
# Distributed under MIT license
import torch
import torch.nn as nn
import torch.utils.data
import torch.nn.init as init
from utility.common import detach, matrix_dict, to_normalized
from .functional import HalfGaussianConditionalLogit, logit_ln
from ...visualize import visualize_sets_v2 as visualize
class ConceptEmbedding(nn.Module):
def __init__(self, args, tools, device, version):
super().__init__()
self.args = args
self.tools = tools
self.device = device
self.version = version
self.subfn_dict = matrix_dict(
keys_x=(
'v2.0', 'VCML', 'BERTvariant', 'NSCL'
),
keys_y=(
'get_concept_embeddings',
'get_all_concept_embeddings',
'get_metaconcept_net',
'get_logit_fn',
'get_train',
'get_visualize',
'get_penalty',
'get_init',
),
values=[
[self.get_concept_embeddings,
lambda: self.get_all_concept_embeddings,
self.get_metaconcept_net_v0,
self.get_feasible_fn,
self.get_train,
self.get_visualize_v0,
self.get_penalty_metaconcept,
self.get_init,
],
[self.get_concept_embeddings,
lambda: self.get_all_concept_embeddings,
self.get_metaconcept_net_v1,
# self.get_metaconcept_net_v1_lambda,
self.get_feasible_fn,
self.get_train,
self.get_visualize_v1,
self.get_penalty_metaconcept,
self.get_init,
],
[self.get_bert_embeddings,
lambda: self.get_all_bert_embeddings,
self.get_metaconcept_net_nscl,
self.get_cos_fn,
self.get_bert_train,
self.get_visualize_bert,
self.get_null_penalty,
self.get_bert_init,
],
[self.get_concept_embeddings,
lambda: self.get_all_concept_embeddings,
self.get_metaconcept_net_nscl,
self.get_cos_fn,
self.get_train,
self.get_visualize_nscl,
self.get_penalty_metaconcept,
self.get_init,
],
]
)
self.build()
def build(self):
args = self.args
self.concept_embed_dim = args.embed_dim
self.concept_embedding = self.subfn_dict[
self.version, 'get_concept_embeddings'
]()
self.all_concept_embeddings = self.subfn_dict[
self.version, 'get_all_concept_embeddings'
]()
self.metaconcept_net = self.subfn_dict[
self.version, 'get_metaconcept_net'
]()
self.logit_fn = self.subfn_dict[
self.version, 'get_logit_fn'
]()
self.train = self.subfn_dict[
self.version, 'get_train'
]()
self.visualize = self.subfn_dict[
self.version, 'get_visualize'
]()
self.penalty = self.subfn_dict[
self.version, 'get_penalty'
]()
self.init = self.subfn_dict[
self.version, 'get_init'
]()
def sub_net(self, in_dim, hidden_dim, out_dim):
if hidden_dim != 0:
net = nn.Sequential(
nn.Linear(in_dim, hidden_dim),
nn.LeakyReLU(),
nn.Linear(hidden_dim, out_dim),
)
else:
net = nn.Linear(in_dim, out_dim)
return net
def determine_relation(self, concepts1, concepts2, detach=(True, True)):
# when some of the arguments contains only one vector
if concepts1.dim() == 1:
return self.determine_relation(
concepts1[None], concepts2, detach)[0]
elif concepts2.dim() == 1:
return self.determine_relation(
concepts1, concepts2[None], detach)[:, 0]
# detaching input concepts
if detach[0]:
concepts1 = concepts1.detach()
if detach[1]:
concepts2 = concepts2.detach()
output = self.metaconcept_net(concepts1, concepts2)
return output
# align a tensor with indexes
@staticmethod
def align(logits, is_concepts, n):
return [
None if i not in is_concepts
else logits[:, is_concepts.index(i)]
for i in range(n)
]
# main function
def calculate_logits(self, objects, program_indexes):
if objects is None:
return None, None
# filter out concept-type arguments
is_concepts = [
i for i, index in enumerate(program_indexes)
if self.tools.arguments_in_concepts[index] != -1
]
length = program_indexes.shape[0]
concept_program_indexes = \
self.tools.arguments_in_concepts[
detach(program_indexes[is_concepts])
]
concept_program_indexes = torch.LongTensor(concept_program_indexes)\
.to(self.device)
# calculating the raw similarity logits
concepts_used = self.concept_embedding(
concept_program_indexes)
logits = self.logit_fn(objects, concepts_used)
logits = self.align(logits, is_concepts, length)
return logits
def get_concept_embeddings(self):
return nn.Embedding(
self.tools.n_concepts, self.concept_embed_dim
)
def get_bert_embeddings(self):
from transformers import BertTokenizer, BertModel
self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
self.bert = BertModel.from_pretrained('bert-base-uncased')
_, self.bert_dim = \
self.bert.embeddings.position_embeddings.weight.shape
self.bert_mlp = self.sub_net(self.bert_dim, 0, self.args.embed_dim)
return self.bert_embed
def bert_embed(self, concepts_indexes):
shape = concepts_indexes.shape
flattened = concepts_indexes.flatten()
concepts = [self.tools.concepts[int(index)] for index in flattened]
embeddings = [self.bert_embed_one(concept) for concept in concepts]
reshaped = torch.stack(embeddings).reshape(tuple(shape) + (-1,))
return reshaped
def bert_embed_one(self, phrase):
tokens = self.tokenizer.encode(phrase)
tensor = torch.LongTensor(tokens).to(self.device)
bert_attention, last_state = self.bert(tensor[None])
output = self.bert_mlp(last_state)[0]
return output
def get_metaconcept_net_v0(self):
self.metaconcept_subnet = self.sub_net(
4 * self.concept_embed_dim,
self.args.metaconcept_hidden_dim, 5
)
self.transform_matrix = nn.Parameter(
torch.zeros(self.concept_embed_dim, self.concept_embed_dim)
)
return self.relation_net_v0
def get_metaconcept_net_v1(self):
self.metaconcept_subnet = self.sub_net(
3, self.args.metaconcept_hidden_dim, 5
)
return self.relation_net_v1
def get_metaconcept_net_v1_lambda(self):
self.metaconcept_subnet = self.sub_net(
1, self.args.metaconcept_hidden_dim, 5
)
return self.relation_net_v1_lambda
def get_metaconcept_net_nscl(self):
self.metaconcept_subnet = self.sub_net(
4 * self.args.embed_dim,
self.args.metaconcept_hidden_dim, 5
)
return self.relation_net_nscl
def get_feasible_fn(self):
return HalfGaussianConditionalLogit(
self.args.sample_size, 400, self.device, slack=False)
def get_cos_fn(self):
'''
self.offset = nn.Parameter(torch.tensor(0.))
self.tau = nn.Parameter(torch.tensor(1.))
'''
self.offset = 0.15
self.tau = (1 - self.offset) / 4
return self.cos_fn
def cos_fn(self, embeddings1, embeddings2):
cos = torch.matmul(
to_normalized(embeddings1),
to_normalized(embeddings2).t()
)
return (cos - self.offset) / self.tau
def get_all_concept_embeddings(self):
return self.concept_embedding.weight
def get_all_bert_embeddings(self):
all_indexes = torch.LongTensor(
self.tools.concepts.indexes()).to(self.device)
return self.bert_embed(all_indexes)
def get_init(self):
return self.normal_init
def get_bert_init(self):
return self.bert_init
def get_visualize_v0(self):
return self.visualize_v0
def get_visualize_v1(self):
return self.visualize_v1
def get_visualize_bert(self):
return self.visualize_bert
def get_visualize_nscl(self):
return self.visualize_nscl
def get_penalty_metaconcept(self):
return self.penalty_metaconcept
def get_null_penalty(self):
return self.null_penalty
def get_train(self):
return self.normal_train
def get_bert_train(self):
return self.bert_train
def relation_net_v0(self, concepts1, concepts2):
'''
concepts1 = torch.matmul(concepts1, self.transform_matrix)
concepts2 = torch.matmul(concepts2, self.transform_matrix)
batch1 = concepts1.shape[0]
batch2 = concepts2.shape[0]
dim = concepts1.shape[1]
norm1 = concepts1.pow(2).sum(1).sqrt()
norm2 = concepts2.pow(2).sum(1).sqrt()
tensor = torch.stack([
concepts1[:, None, :].repeat(1, batch2, 1),
norm1[:, None, None].repeat(1, batch2, dim),
concepts2[None, :, :].repeat(batch1, 1, 1),
norm2[None, :, None].repeat(batch1, 1, dim),
], dim=3)
output = self.metaconcept_subnet(tensor).max(2)[0]
'''
# Use a NS-CL -like metaconcept net
c1 = concepts1[:, None]
c2 = concepts2[None]
n1 = concepts1.shape[0]
n2 = concepts2.shape[0]
abdp = torch.cat([c1.repeat(1, n2, 1),
c2.repeat(n1, 1, 1),
c1 - c2,
c1 * c2],
dim=-1)
return self.metaconcept_subnet(abdp)
def relation_net_v1(self, concepts1, concepts2):
A_to_B = self.logit_fn(concepts1, concepts2)
B_to_A = self.logit_fn(concepts2, concepts1).t()
logit_lambda = logit_ln(self.logit_fn.ln_lambda(
concepts1, concepts2
))
tensor = torch.stack([A_to_B, B_to_A, logit_lambda], dim=2)
output = self.metaconcept_subnet(tensor)
return output
def relation_net_v1_lambda(self, concepts1, concepts2):
logit_lambda = logit_ln(self.logit_fn.ln_lambda(
concepts1, concepts2
))
tensor = logit_lambda[:, :, None]
output = self.metaconcept_subnet(tensor)
return output
def relation_net_nscl(self, concepts1, concepts2):
c1 = to_normalized(concepts1)[:, None]
c2 = to_normalized(concepts2)[None]
n1 = concepts1.shape[0]
n2 = concepts2.shape[0]
abdp = torch.cat([c1.repeat(1, n2, 1),
c2.repeat(n1, 1, 1),
c1 - c2,
c1 * c2],
dim=-1)
return self.metaconcept_subnet(abdp)
def get_embedding(self, category, name):
if category == 'concept':
index = torch.LongTensor(
[self.tools.concepts[name]]).to(self.device)
return self.concept_embedding(index)[0]
else:
raise Exception(f'no embedding for {category}-{name} can be found')
'''
elif category == 'attribute':
index = torch.LongTensor(
[self.tools.attributes[name]]).to(self.device)
return self.attribute_embedding(index)[0]
'''
def normal_init(self):
for name, param in self.named_parameters():
try:
init.kaiming_normal_(param)
except Exception:
init.normal_(param, 0, self.args.init_variance)
def bert_init(self):
self.bert.eval()
for name, param in self.named_parameters():
if not name.startswith('bert.'):
try:
init.kaiming_normal_(param)
except Exception:
init.normal_(param, 0, self.args.init_variance)
else:
param.requires_grad_(False)
def normal_train(self, mode=True):
super().train(mode=mode)
def bert_train(self, mode=True):
super().train(mode=mode)
self.bert.eval()
def visualize_v0(self, path, plt):
pass
def visualize_v1(self, path, plt):
visualize.concept_length(self, path, plt)
return
visualize.probability_matrix(self, path, plt)
visualize.intercosine_matrix(self, path, plt)
visualize.pca_embeddings(
self, path, plt,
['cube', 'gray', 'yellow', 'red', 'purple'],
with_origin=True
)
visualize.cosmat_samekind(self, path, plt)
visualize.cosmat_synonym(self, path, plt)
def visualize_bert(self, path, plt):
pass
def visualize_nscl(self, path, plt):
return
visualize.intercosine_matrix(self, path, plt)
visualize.pca_embeddings(
self, path, plt,
['cube', 'gray', 'yellow', 'red', 'purple'],
with_origin=True
)
visualize.cosmat_samekind(self, path, plt)
visualize.cosmat_synonym(self, path, plt)
def penalty_metaconcept(self):
net = self.metaconcept_subnet
penalty = 0
for name, param in net.named_parameters():
penalty = penalty + param.pow(2).sum()
output = penalty * self.args.penalty
return output
def penalty_length(self):
length_sum = self.all_concept_embeddings().pow(2).sum()
output = length_sum * self.args.penalty
return output
def null_penalty(self):
return 0