-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmodels.py
More file actions
326 lines (255 loc) · 13.9 KB
/
Copy pathmodels.py
File metadata and controls
326 lines (255 loc) · 13.9 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
# coding: utf-8
import torch
import torch.nn as nn
class EncoderRNN(nn.Module):
def __init__(self, input_size, embedding_size, hidden_size, n_layers=2, dropout=0.5):
super(EncoderRNN, self).__init__()
self.input_size = input_size
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.n_layers = n_layers
self.dropout = dropout
self.embedding = nn.Embedding(input_size, embedding_size, padding_idx=0)
self.em_dropout = nn.Dropout(dropout)
self.gru = nn.GRU(embedding_size, hidden_size, n_layers, dropout=dropout, bidirectional=True)
def forward(self, input_seqs, input_lengths, hidden=None):
# Note: we run this all at once (over multiple batches of multiple sequences)
embedded = self.embedding(input_seqs) # S x B x E
embedded = self.em_dropout(embedded)
packed = torch.nn.utils.rnn.pack_padded_sequence(embedded, input_lengths)
outputs, hidden = self.gru(packed, hidden)
outputs, output_lengths = torch.nn.utils.rnn.pad_packed_sequence(outputs) # unpack (back to padded)
outputs = outputs[:, :, :self.hidden_size] + outputs[:, :, self.hidden_size:] # Sum bidirectional outputs
# S x B x H
return outputs, hidden
class Attn(nn.Module):
def __init__(self, hidden_size):
super(Attn, self).__init__()
self.hidden_size = hidden_size
self.attn = nn.Linear(hidden_size * 2, hidden_size)
self.score = nn.Linear(hidden_size, 1, bias=False)
self.softmax = nn.Softmax(dim=1)
def forward(self, hidden, encoder_outputs, seq_mask=None):
max_len = encoder_outputs.size(0)
repeat_dims = [1] * hidden.dim()
repeat_dims[0] = max_len
hidden = hidden.repeat(*repeat_dims) # S x B x H
# For each position of encoder outputs
this_batch_size = encoder_outputs.size(1)
energy_in = torch.cat((hidden, encoder_outputs), 2).view(-1, 2 * self.hidden_size)
attn_energies = self.score(torch.tanh(self.attn(energy_in))) # (S x B) x 1
attn_energies = attn_energies.squeeze(1)
attn_energies = attn_energies.view(max_len, this_batch_size).transpose(0, 1) # B x S
if seq_mask is not None:
attn_energies = attn_energies.masked_fill_(seq_mask, -1e12)
attn_energies = self.softmax(attn_energies)
# Normalize energies to weights in range 0 to 1, resize to B x 1 x S
return attn_energies.unsqueeze(1)
class AttnDecoderRNN(nn.Module):
def __init__(
self, hidden_size, embedding_size, input_size, output_size, n_layers=2, dropout=0.5):
super(AttnDecoderRNN, self).__init__()
# Keep for reference
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.input_size = input_size
self.output_size = output_size
self.n_layers = n_layers
self.dropout = dropout
# Define layers
self.em_dropout = nn.Dropout(dropout)
self.embedding = nn.Embedding(input_size, embedding_size, padding_idx=0)
self.gru = nn.GRU(hidden_size + embedding_size, hidden_size, n_layers, dropout=dropout)
self.concat = nn.Linear(hidden_size * 2, hidden_size)
self.out = nn.Linear(hidden_size, output_size)
# Choose attention model
self.attn = Attn(hidden_size)
def forward(self, input_seq, last_hidden, encoder_outputs, seq_mask):
# Get the embedding of the current input word (last output word)
batch_size = input_seq.size(0)
embedded = self.embedding(input_seq)
embedded = self.em_dropout(embedded)
embedded = embedded.view(1, batch_size, self.embedding_size) # S=1 x B x N
# Calculate attention from current RNN state and all encoder outputs;
# apply to encoder outputs to get weighted average
attn_weights = self.attn(last_hidden[-1].unsqueeze(0), encoder_outputs, seq_mask)
context = attn_weights.bmm(encoder_outputs.transpose(0, 1)) # B x S=1 x N
# Get current hidden state from input word and last hidden state
rnn_output, hidden = self.gru(torch.cat((embedded, context.transpose(0, 1)), 2), last_hidden)
# Attentional vector using the RNN hidden state and context vector
# concatenated together (Luong eq. 5)
output = self.out(torch.tanh(self.concat(torch.cat((rnn_output.squeeze(0), context.squeeze(1)), 1))))
# Return final output, hidden state
return output, hidden
class TreeNode: # the class save the tree node
def __init__(self, embedding, left_flag=False):
self.embedding = embedding
self.left_flag = left_flag
class Score(nn.Module):
def __init__(self, input_size, hidden_size):
super(Score, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.attn = nn.Linear(hidden_size + input_size, hidden_size)
self.score = nn.Linear(hidden_size, 1, bias=False)
def forward(self, hidden, num_embeddings, num_mask=None):
max_len = num_embeddings.size(1)
repeat_dims = [1] * hidden.dim()
repeat_dims[1] = max_len
hidden = hidden.repeat(*repeat_dims) # B x O x H
# For each position of encoder outputs
this_batch_size = num_embeddings.size(0)
energy_in = torch.cat((hidden, num_embeddings), 2).view(-1, self.input_size + self.hidden_size)
score = self.score(torch.tanh(self.attn(energy_in))) # (B x O) x 1
score = score.squeeze(1)
score = score.view(this_batch_size, -1) # B x O
if num_mask is not None:
score = score.masked_fill_(num_mask, -1e12)
return score
class TreeAttn(nn.Module):
def __init__(self, input_size, hidden_size):
super(TreeAttn, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.attn = nn.Linear(hidden_size + input_size, hidden_size)
self.score = nn.Linear(hidden_size, 1)
def forward(self, hidden, encoder_outputs, seq_mask=None):
max_len = encoder_outputs.size(0)
repeat_dims = [1] * hidden.dim()
repeat_dims[0] = max_len
hidden = hidden.repeat(*repeat_dims) # S x B x H
this_batch_size = encoder_outputs.size(1)
energy_in = torch.cat((hidden, encoder_outputs), 2).view(-1, self.input_size + self.hidden_size)
score_feature = torch.tanh(self.attn(energy_in))
attn_energies = self.score(score_feature) # (S x B) x 1
attn_energies = attn_energies.squeeze(1)
attn_energies = attn_energies.view(max_len, this_batch_size).transpose(0, 1) # B x S
if seq_mask is not None:
attn_energies = attn_energies.masked_fill_(seq_mask, -1e12)
attn_energies = nn.functional.softmax(attn_energies, dim=1) # B x S
return attn_energies.unsqueeze(1)
class EncoderSeq(nn.Module):
def __init__(self, input_size, embedding_size, hidden_size, n_layers=2, dropout=0.5):
super(EncoderSeq, self).__init__()
self.input_size = input_size
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.n_layers = n_layers
self.dropout = dropout
self.embedding = nn.Embedding(input_size, embedding_size, padding_idx=0)
self.em_dropout = nn.Dropout(dropout)
self.gru_pade = nn.GRU(embedding_size, hidden_size, n_layers, dropout=dropout, bidirectional=True)
def forward(self, input_seqs, input_lengths, hidden=None):
# Note: we run this all at once (over multiple batches of multiple sequences)
embedded = self.embedding(input_seqs) # S x B x E
embedded = self.em_dropout(embedded)
packed = torch.nn.utils.rnn.pack_padded_sequence(embedded, input_lengths)
pade_hidden = hidden
pade_outputs, pade_hidden = self.gru_pade(packed, pade_hidden)
pade_outputs, _ = torch.nn.utils.rnn.pad_packed_sequence(pade_outputs)
problem_output = pade_outputs[-1, :, :self.hidden_size] + pade_outputs[0, :, self.hidden_size:]
pade_outputs = pade_outputs[:, :, :self.hidden_size] + pade_outputs[:, :, self.hidden_size:] # S x B x H
return pade_outputs, problem_output
class Prediction(nn.Module):
# a seq2tree decoder with Problem aware dynamic encoding
def __init__(self, hidden_size, op_nums, input_size, dropout=0.5):
super(Prediction, self).__init__()
# Keep for reference
self.hidden_size = hidden_size
self.input_size = input_size
self.op_nums = op_nums
# Define layers
self.dropout = nn.Dropout(dropout)
self.embedding_weight = nn.Parameter(torch.randn(1, input_size, hidden_size))
# for Computational symbols and Generated numbers
self.concat_l = nn.Linear(hidden_size, hidden_size)
self.concat_r = nn.Linear(hidden_size * 2, hidden_size)
self.concat_lg = nn.Linear(hidden_size, hidden_size)
self.concat_rg = nn.Linear(hidden_size * 2, hidden_size)
self.ops = nn.Linear(hidden_size * 2, op_nums)
self.attn = TreeAttn(hidden_size, hidden_size)
self.score = Score(hidden_size * 2, hidden_size)
def forward(self, node_stacks, left_childs, encoder_outputs, num_pades, padding_hidden, seq_mask, mask_nums):
current_embeddings = []
for st in node_stacks:
if len(st) == 0:
current_embeddings.append(padding_hidden)
else:
current_node = st[-1]
current_embeddings.append(current_node.embedding)
current_node_temp = []
for l, c in zip(left_childs, current_embeddings):
if l is None:
c = self.dropout(c)
g = torch.tanh(self.concat_l(c))
t = torch.sigmoid(self.concat_lg(c))
current_node_temp.append(g * t)
else:
ld = self.dropout(l)
c = self.dropout(c)
g = torch.tanh(self.concat_r(torch.cat((ld, c), 1)))
t = torch.sigmoid(self.concat_rg(torch.cat((ld, c), 1)))
current_node_temp.append(g * t)
current_node = torch.stack(current_node_temp)
current_embeddings = self.dropout(current_node)
current_attn = self.attn(current_embeddings.transpose(0, 1), encoder_outputs, seq_mask)
current_context = current_attn.bmm(encoder_outputs.transpose(0, 1)) # B x 1 x N
# the information to get the current quantity
batch_size = current_embeddings.size(0)
# predict the output (this node corresponding to output(number or operator)) with PADE
repeat_dims = [1] * self.embedding_weight.dim()
repeat_dims[0] = batch_size
embedding_weight = self.embedding_weight.repeat(*repeat_dims) # B x input_size x N
embedding_weight = torch.cat((embedding_weight, num_pades), dim=1) # B x O x N
leaf_input = torch.cat((current_node, current_context), 2)
leaf_input = leaf_input.squeeze(1)
leaf_input = self.dropout(leaf_input)
# p_leaf = nn.functional.softmax(self.is_leaf(leaf_input), 1)
# max pooling the embedding_weight
embedding_weight_ = self.dropout(embedding_weight)
num_score = self.score(leaf_input.unsqueeze(1), embedding_weight_, mask_nums)
# num_score = nn.functional.softmax(num_score, 1)
op = self.ops(leaf_input)
# return p_leaf, num_score, op, current_embeddings, current_attn
return num_score, op, current_node, current_context, embedding_weight
class GenerateNode(nn.Module):
def __init__(self, hidden_size, op_nums, embedding_size, dropout=0.5):
super(GenerateNode, self).__init__()
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.embeddings = nn.Embedding(op_nums, embedding_size)
self.em_dropout = nn.Dropout(dropout)
self.generate_l = nn.Linear(hidden_size * 2 + embedding_size, hidden_size)
self.generate_r = nn.Linear(hidden_size * 2 + embedding_size, hidden_size)
self.generate_lg = nn.Linear(hidden_size * 2 + embedding_size, hidden_size)
self.generate_rg = nn.Linear(hidden_size * 2 + embedding_size, hidden_size)
def forward(self, node_embedding, node_label, current_context):
node_label_ = self.embeddings(node_label)
node_label = self.em_dropout(node_label_)
node_embedding = node_embedding.squeeze(1)
current_context = current_context.squeeze(1)
node_embedding = self.em_dropout(node_embedding)
current_context = self.em_dropout(current_context)
l_child = torch.tanh(self.generate_l(torch.cat((node_embedding, current_context, node_label), 1)))
l_child_g = torch.sigmoid(self.generate_lg(torch.cat((node_embedding, current_context, node_label), 1)))
r_child = torch.tanh(self.generate_r(torch.cat((node_embedding, current_context, node_label), 1)))
r_child_g = torch.sigmoid(self.generate_rg(torch.cat((node_embedding, current_context, node_label), 1)))
l_child = l_child * l_child_g
r_child = r_child * r_child_g
return l_child, r_child, node_label_
class Merge(nn.Module):
def __init__(self, hidden_size, embedding_size, dropout=0.5):
super(Merge, self).__init__()
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.em_dropout = nn.Dropout(dropout)
self.merge = nn.Linear(hidden_size * 2 + embedding_size, hidden_size)
self.merge_g = nn.Linear(hidden_size * 2 + embedding_size, hidden_size)
def forward(self, node_embedding, sub_tree_1, sub_tree_2):
sub_tree_1 = self.em_dropout(sub_tree_1)
sub_tree_2 = self.em_dropout(sub_tree_2)
node_embedding = self.em_dropout(node_embedding)
sub_tree = torch.tanh(self.merge(torch.cat((node_embedding, sub_tree_1, sub_tree_2), 1)))
sub_tree_g = torch.sigmoid(self.merge_g(torch.cat((node_embedding, sub_tree_1, sub_tree_2), 1)))
sub_tree = sub_tree * sub_tree_g
return sub_tree