-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.py
435 lines (335 loc) · 14.8 KB
/
model.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
# original author : Debanjali Biswas
# changes by: Theresa Schmidt
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Alignment Model
"""
import torch
import sys
import torch.nn as nn
seed=0
torch.manual_seed(seed)
# Encoder to generate context vector using BERT/ELMO embeddings
class Encoder(nn.Module):
def __init__(self, embedding_dim, device, feature_dim, with_feature):
"""
Constructor
Parameters
----------
embedding_dim : int
Embedding dimension.
device : object
torch device where model tensors are saved.
feature_dim : int
Number of features. For base model, 1 and for extended model, 3.
with_feature : boolean
Check whether to add features or not.
"""
super().__init__()
self.device = device
self.feature_dim = feature_dim
self.none_action_vector = nn.parameter.Parameter(torch.randn(1, embedding_dim))
self.with_feature = with_feature
self.embedding_dim = embedding_dim # Embedding dim
self.seqlstm = nn.LSTM(input_size=embedding_dim, hidden_size=embedding_dim)
self.parentlstm = nn.LSTM(input_size=embedding_dim, hidden_size=embedding_dim)
self.childlstm = nn.LSTM(input_size=embedding_dim, hidden_size=embedding_dim)
# self.encodingmlp = nn.Sequential(nn.Linear(self.embedding_dim * self.feature_dim, self.embedding_dim * self.feature_dim,), # Linear layer1
# nn.Sigmoid())
def sequence_embedding(self, node, embedding_vectors, vector_lookup_list):
"""
Sequence Embedding Function
Parameters
----------
node : Tensor node_sequence_length
Node.
embedding_vectors : Dict
Embedding dictionary for a particular Recipe;
where keys are vector_lookup_list token_ids and values are their corresponding word embeddings (BERT/ELMO).
vector_lookup_list : Dict
Look up dictionary for a particular Recipe embeddings;
where key is the Conllu file token 'id' and values are list of token_ids generated using BERT/ELMO.
Returns
-------
embedding: Tensor 1 X embedding_dim
Embedding vector of the node. Returns none_action_vector for null action.
"""
if len(node):
# token_list = torch.zeros(1, self.embedding_dim).to(self.device) # embedding vector for the node sequence
emb_id_list = []
for token_id in node:
emb_id_list.extend(vector_lookup_list[token_id.item()])
input = [embedding_vectors[emb_id] for emb_id in emb_id_list]
embedding = torch.squeeze(
self.seqlstm(torch.cat(input).view(len(input), 1, -1))[1][1], dim=0
)
return embedding
# return embedding # .view(1, 1, 1, self.embedding_dim)
"""for emb_id in emb_id_list :
emb = torch.unsqueeze(embedding_vectors[emb_id], dim = 0) # 1 X embedding_dim
token_list = token_list.add(emb) # 1 X embedding_dim
embedding = token_list / len(emb_id_list) # 1 X embedding_dim"""
else:
return self.none_action_vector
# return embedding # 1 X embedding_dim
def forward(
self, action, parent_list, child_list, embedding_vectors, vector_lookup_list
):
"""
Encoder Model
Parameters
----------
action : Tensor action_sequence_length
Action Node.
parent_list : List of Tensors
List of Parent Nodes for Action node.
child_list : List of Tensors
List of Child Nodes for Action node.
embedding_vectors : Dict
Embedding dictionary for a particular Recipe;
where keys are vector_lookup_list token_ids and values are their corresponding word embeddings (BERT/ELMO).
vector_lookup_list : Dict
Look up dictionary for a particular Recipe embeddings;
where key is the Conllu file token 'id' and values are list of token_ids generated using BERT/ELMO.
Returns
-------
encoding : Tensor feature_dim X embedding_dim
Encoding vector for input.
For extended model, (action, parent_list, child_list) and for base model, only (action).
"""
# generating embedding vectors for action node sequence
action_context = self.sequence_embedding(
action, embedding_vectors, vector_lookup_list
) # 1 X embedding_dim
if self.with_feature:
parent_emb_list = child_emb_list = []
# parent_list_context = torch.zeros(1, self.embedding_dim).to(self.device) # embedding vector for all the parent node sequence
# child_list_context = torch.zeros(1, self.embedding_dim).to(self.device) # embedding vector for all the child node sequence
if len(parent_list):
# generating embedding vectors for each parent node sequence and concatinating them up to one vector
for parent in parent_list:
parent_context = self.sequence_embedding(
parent, embedding_vectors, vector_lookup_list
) # 1 X embedding_dim
parent_emb_list.append(parent_context) # 1 X embedding_dim
parent_list_context = torch.squeeze(
self.parentlstm(
torch.cat(parent_emb_list).view(len(parent_emb_list), 1, -1)
)[1][1],
dim=0,
)
else:
parent_list_context = torch.zeros(1, self.embedding_dim).to(self.device)
if len(child_list):
# generating embedding vectors for each child node sequence and concatinating them up to one vector
for child in child_list:
child_context = self.sequence_embedding(
child, embedding_vectors, vector_lookup_list
) # 1 X embedding_dim
child_emb_list.append(child_context) # 1 X embedding_dim
child_list_context = torch.squeeze(
self.childlstm(
torch.cat(child_emb_list).view(len(child_emb_list), 1, -1)
)[1][1],
dim=0,
)
else:
child_list_context = torch.zeros(1, self.embedding_dim).to(self.device)
context = torch.cat(
[action_context, parent_list_context, child_list_context], dim=0
) # 3 X embedding_dim
# return context # 3 X embedding_dim
else:
context = action_context
encoding = torch.unsqueeze(torch.flatten(context), dim=0)
# encoding = self.encodingmlp(encoding)
return encoding
# Linear Classifier to generate probability of alignment between an action from Recipe1 and an action from Recipe2
class Scorer(nn.Module):
def __init__(
self, feature_dim, embedding_dim, hidden_dim1, hidden_dim2, output_dim, dropout0, dropout1, dropout2, device
):
"""
Contructor
Parameters
----------
feature_dim : int
Number of features. For base model, 1 and for extended model, 3.
embedding_dim : int
Embedding dimension.
hidden_dim1 : int
Hidden dimension for MLP layer 1 in scorer.
hidden_dim2 : int
Hidden dimension for MLP layer 2 in scorer.
output_dim : int
Output dimension of MLP in Scorer (always 1).
dropout0 : float
Dropout-rate between encoding and hidden layer 1.
dropout1 : float
Dropout-rate between hidden layer 1 and hidden layer 2.
dropout2 : float
Dropout-rate after hidden layer 2.
device : object
torch device where model tensors are saved.
with_feature : boolean
Check whether to add features or not.
"""
super().__init__()
self.device = device
self.feature_dim = feature_dim
self.embedding_dim = embedding_dim
# self.weights = nn.parameter.Parameter(torch.randn(self.embedding_dim, self.embedding_dim))
self.linear_classifier = nn.Sequential(
nn.Linear(self.embedding_dim * feature_dim, hidden_dim1), # Linear layer1
nn.Sigmoid(),
nn.Dropout(dropout0)
nn.Linear(hidden_dim1, hidden_dim2), # Linear layer1
nn.Sigmoid(),
nn.Dropout(dropout1)
nn.Linear(hidden_dim2, output_dim), # Linear layer1
nn.Sigmoid(),
nn.Dropout(dropout2)
).to(
self.device
) # MLP structure
# self.dropout = nn.Dropout(dropout) # Dropout
def forward(self, encoding1, encoding2):
"""
Scorer model using an element-wise multiplication and MLP
Parameters
----------
encoding1 : Tensor feature_dim X embedding_dim
Encoding vector for action node from Recipe 1.
encoding2 : Tensor feature_dim X embedding_dim
Encoding vector for action node from Recipe 2.
Returns
-------
pred : Tensor of size 1
Classifier probability prediction.
"""
# encoding1 = torch.unsqueeze(torch.flatten(context1), dim = 0) # 1 X feature_dim * embedding_dim
# encoding2 = torch.unsqueeze(torch.flatten(context2), dim = 0)# 1 X feature_dim * embedding_dim
encoding = torch.mul(encoding1, encoding2) # 1 X feature_dim * embedding_dim
pred = self.linear_classifier(encoding) # 1 X output_dim
# pred = self.dropout(pred) # 1 X output_dim
pred = torch.squeeze(pred, 1) # 1
return pred # 1
# Alignment model to predict which action from Recipe2 aligns (including none) with a particular action (Action1) from Recipe1
class AlignmentModel(nn.Module):
def __init__(
self, embedding_dim, hidden_dim1, hidden_dim2, output_dim, dropout0, dropout1, dropout2, device, with_feature=True
):
"""
Alignment Model
Parameters
----------
embedding_dim : int
Embedding Dimension
hidden_dim1 : int
Hidden dimension for MLP layer 1 in scorer.
hidden_dim2 : int
Hidden dimension for MLP layer 2 in scorer.
output_dim : int
Output dimension of MLP in Scorer (always 1).
dropout0 : float
Dropout-rate between encoding and hidden layer 1.
dropout1 : float
Dropout-rate between hidden layer 1 and hidden layer 2.
dropout2 : float
Dropout-rate after hidden layer 2.
device : object
torch device where model tensors are saved.
with_feature : boolean; Optional
Check whether to add features or not. Default value True.
"""
super().__init__()
self.embedding_dim = embedding_dim
if with_feature:
self.feature_dim = 3
else:
self.feature_dim = 1
self.encoder = Encoder(
self.embedding_dim, device, self.feature_dim, with_feature
) # Encoder class object
self.scorer = Scorer(
self.feature_dim,
self.embedding_dim,
hidden_dim1,
hidden_dim2,
output_dim,
dropout0,
dropout1,
dropout2,
device,
) # Classifier class object
def forward(
self,
action1,
parent_list1,
child_list1,
embedding_vectors1,
vector_lookup_list1,
recipe2_actions,
embedding_vectors2,
vector_lookup_list2,
):
"""
Alignment Model
Parameters
----------
action1 : Tensor action_sequence_length
Action Node from Recipe1.
parent_list1 : List of Tensors
List of Parent Nodes for Action node from Recipe1.
child_list1 : List of Tensors
List of Child Nodes for Action node from Recipe1.
embedding_vectors1 : Dict
Embedding dictionary for Recipe 1;
where keys are vector_lookup_list token_ids and values are their corresponding word embeddings (BERT/ELMO).
vector_lookup_list1 : Dict
Look up dictionary for Recipe 1 embeddings;
where key is the Conllu file token 'id' and values are list of token_ids generated using BERT/ELMO.
recipe2_actions : List of dict
List of all action dictionaries from Recipe2
(action dictionaries contain action node and their corresponding lists of parent nodes and child nodes).
embedding_vectors2 : Dict
Embedding dictionary for Recipe 2;
where keys are vector_lookup_list token_ids and values are their corresponding word embeddings (BERT/ELMO).
vector_lookup_list2 : Dict
Look up dictionary for Recipe 2 embeddings;
where key is the Conllu file token 'id' and values are list of token_ids generated using BERT/ELMO.
Returns
-------
prediction_list : Tensor # 1 X length of recipe2_actions
Probabilities of all class alignment, where class is an action in Recipe2 (including none of these).
"""
prediction_list = (
[]
) # List of probability predictions between an action in Recipe1 and all actions in Recipe2 (including none of these)
encoding1 = self.encoder(
action1, parent_list1, child_list1, embedding_vectors1, vector_lookup_list1
) # embedding_dim X 3
for node in recipe2_actions:
encoding2 = self.encoder(
node["Action"],
node["Parent_List"],
node["Child_List"],
embedding_vectors2,
vector_lookup_list2,
) # embedding_dim X 3
pred = self.scorer(encoding1, encoding2) # Probability prediction
prediction_list.append(pred)
prediction_list = torch.stack(
prediction_list, dim=1
) # 1 X length of recipe2_actions
return prediction_list # 1 X length of recipe2_actions