forked from shirgur/PointerNet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
PointerNet.py
324 lines (249 loc) · 10.7 KB
/
PointerNet.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
#!/usr/bin/env python3
import torch
import torch.nn as nn
from torch.nn import Parameter
import torch.nn.functional as F
from torch import tanh, sigmoid
class Encoder(nn.Module):
"""
Encoder class for Pointer-Net
"""
def __init__(self, embedding_dim,
hidden_dim,
n_layers,
dropout,
bidir):
"""
Initiate Encoder
:param Tensor embedding_dim: Number of embbeding channels
:param int hidden_dim: Number of hidden units for the LSTM
:param int n_layers: Number of layers for LSTMs
:param float dropout: Float between 0-1
:param bool bidir: Bidirectional
"""
super(Encoder, self).__init__()
self.hidden_dim = hidden_dim//2 if bidir else hidden_dim
self.n_layers = n_layers*2 if bidir else n_layers
self.bidir = bidir
self.lstm = nn.LSTM(embedding_dim,
self.hidden_dim,
n_layers,
dropout=dropout,
bidirectional=bidir)
# Used for propagating .cuda() command
self.h0 = Parameter(torch.zeros(1), requires_grad=False)
self.c0 = Parameter(torch.zeros(1), requires_grad=False)
def forward(self, embedded_inputs,
hidden):
"""
Encoder - Forward-pass
:param Tensor embedded_inputs: Embedded inputs of Pointer-Net
:param Tensor hidden: Initiated hidden units for the LSTMs (h, c)
:return: LSTMs outputs and hidden units (h, c)
"""
embedded_inputs = embedded_inputs.permute(1, 0, 2)
outputs, hidden = self.lstm(embedded_inputs, hidden)
return outputs.permute(1, 0, 2), hidden
def init_hidden(self, embedded_inputs):
"""
Initiate hidden units
:param Tensor embedded_inputs: The embedded input of Pointer-NEt
:return: Initiated hidden units for the LSTMs (h, c)
"""
batch_size = embedded_inputs.size(0)
# Reshaping (Expanding)
h0 = self.h0.unsqueeze(0).unsqueeze(0).repeat(self.n_layers,
batch_size,
self.hidden_dim)
c0 = self.h0.unsqueeze(0).unsqueeze(0).repeat(self.n_layers,
batch_size,
self.hidden_dim)
return h0, c0
class Attention(nn.Module):
"""
Attention model for Pointer-Net
"""
def __init__(self, input_dim,
hidden_dim):
"""
Initiate Attention
:param int input_dim: Input's diamention
:param int hidden_dim: Number of hidden units in the attention
"""
super(Attention, self).__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.input_linear = nn.Linear(input_dim, hidden_dim)
self.context_linear = nn.Conv1d(input_dim, hidden_dim, 1, 1)
self.V = Parameter(torch.FloatTensor(hidden_dim), requires_grad=True)
self._inf = Parameter(torch.FloatTensor([float('-inf')]), requires_grad=False)
self.tanh = tanh
self.softmax = nn.Softmax(dim=1)
# Initialize vector V
nn.init.uniform_(self.V, -1, 1)
def forward(self, input,
context,
mask):
"""
Attention - Forward-pass
:param Tensor input: Hidden state h
:param Tensor context: Attention context
:param ByteTensor mask: Selection mask
:return: tuple of - (Attentioned hidden state, Alphas)
"""
# (batch, hidden_dim, seq_len)
inp = self.input_linear(input).unsqueeze(2).expand(-1, -1, context.size(1))
# (batch, hidden_dim, seq_len)
context = context.permute(0, 2, 1)
ctx = self.context_linear(context)
# (batch, 1, hidden_dim)
V = self.V.unsqueeze(0).expand(context.size(0), -1).unsqueeze(1)
# (batch, seq_len)
att = torch.bmm(V, self.tanh(inp + ctx)).squeeze(1)
if len(att[mask]) > 0:
att[mask] = self.inf[mask]
alpha = self.softmax(att)
hidden_state = torch.bmm(ctx, alpha.unsqueeze(2)).squeeze(2)
return hidden_state, alpha
def init_inf(self, mask_size):
self.inf = self._inf.unsqueeze(1).expand(*mask_size)
class Decoder(nn.Module):
"""
Decoder model for Pointer-Net
"""
def __init__(self, embedding_dim,
hidden_dim):
"""
Initiate Decoder
:param int embedding_dim: Number of embeddings in Pointer-Net
:param int hidden_dim: Number of hidden units for the decoder's RNN
"""
super(Decoder, self).__init__()
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.input_to_hidden = nn.Linear(embedding_dim, 4 * hidden_dim)
self.hidden_to_hidden = nn.Linear(hidden_dim, 4 * hidden_dim)
self.hidden_out = nn.Linear(hidden_dim * 2, hidden_dim)
self.att = Attention(hidden_dim, hidden_dim)
# Used for propagating .cuda() command
self.mask = Parameter(torch.ones(1), requires_grad=False)
self.runner = Parameter(torch.zeros(1), requires_grad=False)
def forward(self, embedded_inputs,
decoder_input,
hidden,
context):
"""
Decoder - Forward-pass
:param Tensor embedded_inputs: Embedded inputs of Pointer-Net
:param Tensor decoder_input: First decoder's input
:param Tensor hidden: First decoder's hidden states
:param Tensor context: Encoder's outputs
:return: (Output probabilities, Pointers indices), last hidden state
"""
batch_size = embedded_inputs.size(0)
input_length = embedded_inputs.size(1)
# (batch, seq_len)
mask = self.mask.repeat(input_length).unsqueeze(0).repeat(batch_size, 1)
self.att.init_inf(mask.size())
# Generating arang(input_length), broadcasted across batch_size
runner = self.runner.repeat(input_length)
for i in range(input_length):
runner.data[i] = i
runner = runner.unsqueeze(0).expand(batch_size, -1).long()
outputs = []
pointers = []
def step(x, hidden):
"""
Recurrence step function
:param Tensor x: Input at time t
:param tuple(Tensor, Tensor) hidden: Hidden states at time t-1
:return: Hidden states at time t (h, c), Attention probabilities (Alpha)
"""
# Regular LSTM
h, c = hidden
gates = self.input_to_hidden(x) + self.hidden_to_hidden(h)
input, forget, cell, out = gates.chunk(4, 1)
input = sigmoid(input)
forget = sigmoid(forget)
cell = tanh(cell)
out = sigmoid(out)
c_t = (forget * c) + (input * cell)
h_t = out * tanh(c_t)
# Attention section
hidden_t, output = self.att(h_t, context, torch.eq(mask, 0))
hidden_t = tanh(self.hidden_out(torch.cat((hidden_t, h_t), 1)))
return hidden_t, c_t, output
# Recurrence loop
for _ in range(input_length):
h_t, c_t, outs = step(decoder_input, hidden)
hidden = (h_t, c_t)
# Masking selected inputs
masked_outs = outs * mask
# Get maximum probabilities and indices
max_probs, indices = masked_outs.max(1)
one_hot_pointers = (runner == indices.unsqueeze(1).expand(-1, outs.size()[1])).float()
# Update mask to ignore seen indices
mask = mask * (1 - one_hot_pointers)
# Get embedded inputs by max indices
embedding_mask = one_hot_pointers.unsqueeze(2).expand(-1, -1, self.embedding_dim).byte()
decoder_input = embedded_inputs[embedding_mask.data].view(batch_size, self.embedding_dim)
outputs.append(outs.unsqueeze(0))
pointers.append(indices.unsqueeze(1))
outputs = torch.cat(outputs).permute(1, 0, 2)
pointers = torch.cat(pointers, 1)
return (outputs, pointers), hidden
class PointerNet(nn.Module):
"""
Pointer-Net
"""
def __init__(self, embedding_dim,
hidden_dim,
lstm_layers,
dropout,
bidir=False):
"""
Initiate Pointer-Net
:param int embedding_dim: Number of embbeding channels
:param int hidden_dim: Encoders hidden units
:param int lstm_layers: Number of layers for LSTMs
:param float dropout: Float between 0-1
:param bool bidir: Bidirectional
"""
super(PointerNet, self).__init__()
self.embedding_dim = embedding_dim
self.bidir = bidir
self.embedding = nn.Linear(2, embedding_dim)
self.encoder = Encoder(embedding_dim,
hidden_dim,
lstm_layers,
dropout,
bidir)
self.decoder = Decoder(embedding_dim, hidden_dim)
self.decoder_input0 = Parameter(torch.FloatTensor(embedding_dim), requires_grad=False)
# Initialize decoder_input0
nn.init.uniform_(self.decoder_input0, -1, 1)
def forward(self, inputs):
"""
PointerNet - Forward-pass
:param Tensor inputs: Input sequence
:return: Pointers probabilities and indices
"""
batch_size = inputs.size(0)
input_length = inputs.size(1)
decoder_input0 = self.decoder_input0.unsqueeze(0).expand(batch_size, -1)
inputs = inputs.view(batch_size * input_length, -1)
embedded_inputs = self.embedding(inputs).view(batch_size, input_length, -1)
encoder_hidden0 = self.encoder.init_hidden(embedded_inputs)
encoder_outputs, encoder_hidden = self.encoder(embedded_inputs,
encoder_hidden0)
if self.bidir:
decoder_hidden0 = (torch.cat([_ for _ in encoder_hidden[0][-2:]], dim=-1),
torch.cat([_ for _ in encoder_hidden[1][-2:]], dim=-1))
else:
decoder_hidden0 = (encoder_hidden[0][-1],
encoder_hidden[1][-1])
(outputs, pointers), decoder_hidden = self.decoder(embedded_inputs,
decoder_input0,
decoder_hidden0,
encoder_outputs)
return outputs, pointers