forked from wouterkool/attention-learn-to-route
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer_network.py
353 lines (283 loc) · 13.2 KB
/
pointer_network.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
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import math
import numpy as np
class Encoder(nn.Module):
"""Maps a graph represented as an input sequence
to a hidden vector"""
def __init__(self, input_dim, hidden_dim):
super(Encoder, self).__init__()
self.hidden_dim = hidden_dim
self.lstm = nn.LSTM(input_dim, hidden_dim)
self.init_hx, self.init_cx = self.init_hidden(hidden_dim)
def forward(self, x, hidden):
output, hidden = self.lstm(x, hidden)
return output, hidden
def init_hidden(self, hidden_dim):
"""Trainable initial hidden state"""
std = 1. / math.sqrt(hidden_dim)
enc_init_hx = nn.Parameter(torch.FloatTensor(hidden_dim))
enc_init_hx.data.uniform_(-std, std)
enc_init_cx = nn.Parameter(torch.FloatTensor(hidden_dim))
enc_init_cx.data.uniform_(-std, std)
return enc_init_hx, enc_init_cx
class Attention(nn.Module):
"""A generic attention module for a decoder in seq2seq"""
def __init__(self, dim, use_tanh=False, C=10):
super(Attention, self).__init__()
self.use_tanh = use_tanh
self.project_query = nn.Linear(dim, dim)
self.project_ref = nn.Conv1d(dim, dim, 1, 1)
self.C = C # tanh exploration
self.tanh = nn.Tanh()
self.v = nn.Parameter(torch.FloatTensor(dim))
self.v.data.uniform_(-(1. / math.sqrt(dim)), 1. / math.sqrt(dim))
def forward(self, query, ref):
"""
Args:
query: is the hidden state of the decoder at the current
time step. batch x dim
ref: the set of hidden states from the encoder.
sourceL x batch x hidden_dim
"""
# ref is now [batch_size x hidden_dim x sourceL]
ref = ref.permute(1, 2, 0)
q = self.project_query(query).unsqueeze(2) # batch x dim x 1
e = self.project_ref(ref) # batch_size x hidden_dim x sourceL
# expand the query by sourceL
# batch x dim x sourceL
expanded_q = q.repeat(1, 1, e.size(2))
# batch x 1 x hidden_dim
v_view = self.v.unsqueeze(0).expand(
expanded_q.size(0), len(self.v)).unsqueeze(1)
# [batch_size x 1 x hidden_dim] * [batch_size x hidden_dim x sourceL]
u = torch.bmm(v_view, self.tanh(expanded_q + e)).squeeze(1)
if self.use_tanh:
logits = self.C * self.tanh(u)
else:
logits = u
return e, logits
class Decoder(nn.Module):
def __init__(self,
embedding_dim,
hidden_dim,
tanh_exploration,
use_tanh,
n_glimpses=1,
mask_glimpses=True,
mask_logits=True):
super(Decoder, self).__init__()
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.n_glimpses = n_glimpses
self.mask_glimpses = mask_glimpses
self.mask_logits = mask_logits
self.use_tanh = use_tanh
self.tanh_exploration = tanh_exploration
self.decode_type = None # Needs to be set explicitly before use
self.lstm = nn.LSTMCell(embedding_dim, hidden_dim)
self.pointer = Attention(hidden_dim, use_tanh=use_tanh, C=tanh_exploration)
self.glimpse = Attention(hidden_dim, use_tanh=False)
self.sm = nn.Softmax(dim=1)
def update_mask(self, mask, selected):
return mask.clone().scatter_(1, selected.unsqueeze(-1), True)
def recurrence(self, x, h_in, prev_mask, prev_idxs, step, context):
logit_mask = self.update_mask(prev_mask, prev_idxs) if prev_idxs is not None else prev_mask
logits, h_out = self.calc_logits(x, h_in, logit_mask, context, self.mask_glimpses, self.mask_logits)
# Calculate log_softmax for better numerical stability
log_p = F.log_softmax(logits, dim=1)
probs = log_p.exp()
if not self.mask_logits:
# If self.mask_logits, this would be redundant, otherwise we must mask to make sure we don't resample
# Note that as a result the vector of probs may not sum to one (this is OK for .multinomial sampling)
# But practically by not masking the logits, a model is learned over all sequences (also infeasible)
# while only during sampling feasibility is enforced (a.k.a. by setting to 0. here)
probs[logit_mask] = 0.
# For consistency we should also mask out in log_p, but the values set to 0 will not be sampled and
# Therefore not be used by the reinforce estimator
return h_out, log_p, probs, logit_mask
def calc_logits(self, x, h_in, logit_mask, context, mask_glimpses=None, mask_logits=None):
if mask_glimpses is None:
mask_glimpses = self.mask_glimpses
if mask_logits is None:
mask_logits = self.mask_logits
hy, cy = self.lstm(x, h_in)
g_l, h_out = hy, (hy, cy)
for i in range(self.n_glimpses):
ref, logits = self.glimpse(g_l, context)
# For the glimpses, only mask before softmax so we have always an L1 norm 1 readout vector
if mask_glimpses:
logits[logit_mask] = -np.inf
# [batch_size x h_dim x sourceL] * [batch_size x sourceL x 1] =
# [batch_size x h_dim x 1]
g_l = torch.bmm(ref, self.sm(logits).unsqueeze(2)).squeeze(2)
_, logits = self.pointer(g_l, context)
# Masking before softmax makes probs sum to one
if mask_logits:
logits[logit_mask] = -np.inf
return logits, h_out
def forward(self, decoder_input, embedded_inputs, hidden, context, eval_tours=None):
"""
Args:
decoder_input: The initial input to the decoder
size is [batch_size x embedding_dim]. Trainable parameter.
embedded_inputs: [sourceL x batch_size x embedding_dim]
hidden: the prev hidden state, size is [batch_size x hidden_dim].
Initially this is set to (enc_h[-1], enc_c[-1])
context: encoder outputs, [sourceL x batch_size x hidden_dim]
"""
batch_size = context.size(1)
outputs = []
selections = []
steps = range(embedded_inputs.size(0))
idxs = None
mask = Variable(
embedded_inputs.data.new().byte().new(embedded_inputs.size(1), embedded_inputs.size(0)).zero_(),
requires_grad=False
)
for i in steps:
hidden, log_p, probs, mask = self.recurrence(decoder_input, hidden, mask, idxs, i, context)
# select the next inputs for the decoder [batch_size x hidden_dim]
idxs = self.decode(
probs,
mask
) if eval_tours is None else eval_tours[:, i]
idxs = idxs.detach() # Otherwise pytorch complains it want's a reward, todo implement this more properly?
# Gather input embedding of selected
decoder_input = torch.gather(
embedded_inputs,
0,
idxs.contiguous().view(1, batch_size, 1).expand(1, batch_size, *embedded_inputs.size()[2:])
).squeeze(0)
# use outs to point to next object
outputs.append(log_p)
selections.append(idxs)
return (torch.stack(outputs, 1), torch.stack(selections, 1)), hidden
def decode(self, probs, mask):
if self.decode_type == "greedy":
_, idxs = probs.max(1)
assert not mask.gather(1, idxs.unsqueeze(-1)).data.any(), \
"Decode greedy: infeasible action has maximum probability"
elif self.decode_type == "sampling":
idxs = probs.multinomial().squeeze(1)
# Check if sampling went OK, can go wrong due to bug on GPU
while mask.gather(1, idxs.unsqueeze(-1)).data.any():
print(' [!] resampling due to race condition')
idxs = probs.multinomial().squeeze(1)
else:
assert False, "Unknown decode type"
return idxs
class CriticNetworkLSTM(nn.Module):
"""Useful as a baseline in REINFORCE updates"""
def __init__(self,
embedding_dim,
hidden_dim,
n_process_block_iters,
tanh_exploration,
use_tanh):
super(CriticNetworkLSTM, self).__init__()
self.hidden_dim = hidden_dim
self.n_process_block_iters = n_process_block_iters
self.encoder = Encoder(embedding_dim, hidden_dim)
self.process_block = Attention(hidden_dim, use_tanh=use_tanh, C=tanh_exploration)
self.sm = nn.Softmax(dim=1)
self.decoder = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, 1)
)
def forward(self, inputs):
"""
Args:
inputs: [embedding_dim x batch_size x sourceL] of embedded inputs
"""
inputs = inputs.transpose(0, 1).contiguous()
encoder_hx = self.encoder.init_hx.unsqueeze(0).repeat(inputs.size(1), 1).unsqueeze(0)
encoder_cx = self.encoder.init_cx.unsqueeze(0).repeat(inputs.size(1), 1).unsqueeze(0)
# encoder forward pass
enc_outputs, (enc_h_t, enc_c_t) = self.encoder(inputs, (encoder_hx, encoder_cx))
# grab the hidden state and process it via the process block
process_block_state = enc_h_t[-1]
for i in range(self.n_process_block_iters):
ref, logits = self.process_block(process_block_state, enc_outputs)
process_block_state = torch.bmm(ref, self.sm(logits).unsqueeze(2)).squeeze(2)
# produce the final scalar output
out = self.decoder(process_block_state)
return out
class PointerNetwork(nn.Module):
def __init__(self,
embedding_dim,
hidden_dim,
problem,
n_encode_layers=None,
tanh_clipping=10.,
mask_inner=True,
mask_logits=True,
normalization=None):
super(PointerNetwork, self).__init__()
self.problem = problem
assert problem.NAME == "tsp", "Pointer Network only supported for TSP"
self.input_dim = 2
self.encoder = Encoder(
embedding_dim,
hidden_dim)
self.decoder = Decoder(
embedding_dim,
hidden_dim,
tanh_exploration=tanh_clipping,
use_tanh=tanh_clipping > 0,
n_glimpses=1,
mask_glimpses=mask_inner,
mask_logits=mask_logits
)
# Trainable initial hidden states
std = 1. / math.sqrt(embedding_dim)
self.decoder_in_0 = nn.Parameter(torch.FloatTensor(embedding_dim))
self.decoder_in_0.data.uniform_(-std, std)
self.embedding = nn.Parameter(torch.FloatTensor(self.input_dim, embedding_dim))
self.embedding.data.uniform_(-std, std)
def set_decode_type(self, decode_type):
self.decoder.decode_type = decode_type
def forward(self, inputs, eval_tours=None, return_pi=False):
batch_size, graph_size, input_dim = inputs.size()
embedded_inputs = torch.mm(
inputs.transpose(0, 1).contiguous().view(-1, input_dim),
self.embedding
).view(graph_size, batch_size, -1)
# query the actor net for the input indices
# making up the output, and the pointer attn
_log_p, pi = self._inner(embedded_inputs, eval_tours)
cost, mask = self.problem.get_costs(inputs, pi)
# Log likelyhood is calculated within the model since returning it per action does not work well with
# DataParallel since sequences can be of different lengths
ll = self._calc_log_likelihood(_log_p, pi, mask)
if return_pi:
return cost, ll, pi
return cost, ll
def _calc_log_likelihood(self, _log_p, a, mask):
# Get log_p corresponding to selected actions
log_p = _log_p.gather(2, a.unsqueeze(-1)).squeeze(-1)
# Optional: mask out actions irrelevant to objective so they do not get reinforced
if mask is not None:
log_p[mask] = 0
assert (log_p > -1000).data.all(), "Logprobs should not be -inf, check sampling procedure!"
# Calculate log_likelihood
return log_p.sum(1)
def _inner(self, inputs, eval_tours=None):
encoder_hx = encoder_cx = Variable(
torch.zeros(1, inputs.size(1), self.encoder.hidden_dim, out=inputs.data.new()),
requires_grad=False
)
# encoder forward pass
enc_h, (enc_h_t, enc_c_t) = self.encoder(inputs, (encoder_hx, encoder_cx))
dec_init_state = (enc_h_t[-1], enc_c_t[-1])
# repeat decoder_in_0 across batch
decoder_input = self.decoder_in_0.unsqueeze(0).repeat(inputs.size(1), 1)
(pointer_probs, input_idxs), dec_hidden_t = self.decoder(decoder_input,
inputs,
dec_init_state,
enc_h,
eval_tours)
return pointer_probs, input_idxs