forked from pemami4911/neural-combinatorial-rl-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_combinatorial_rl.py
543 lines (457 loc) · 19.5 KB
/
neural_combinatorial_rl.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import torch
import torch.nn as nn
import torch.autograd as autograd
from torch.autograd import Variable
import torch.nn.functional as F
import math
import numpy as np
from beam_search import Beam
class Encoder(nn.Module):
"""Maps a graph represented as an input sequence
to a hidden vector"""
def __init__(self, input_dim, hidden_dim, use_cuda):
super(Encoder, self).__init__()
self.hidden_dim = hidden_dim
self.lstm = nn.LSTM(input_dim, hidden_dim)
self.use_cuda = use_cuda
self.enc_init_state = 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"""
enc_init_hx = Variable(torch.zeros(hidden_dim), requires_grad=False)
if self.use_cuda:
enc_init_hx = enc_init_hx.cuda()
#enc_init_hx.data.uniform_(-(1. / math.sqrt(hidden_dim)),
# 1. / math.sqrt(hidden_dim))
enc_init_cx = Variable(torch.zeros(hidden_dim), requires_grad=False)
if self.use_cuda:
enc_init_cx = enc_init_cx.cuda()
#enc_init_cx = nn.Parameter(enc_init_cx)
#enc_init_cx.data.uniform_(-(1. / math.sqrt(hidden_dim)),
# 1. / math.sqrt(hidden_dim))
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, use_cuda=True):
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()
v = torch.FloatTensor(dim)
if use_cuda:
v = v.cuda()
self.v = nn.Parameter(v)
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,
max_length,
tanh_exploration,
terminating_symbol,
use_tanh,
decode_type,
n_glimpses=1,
beam_size=0,
use_cuda=True):
super(Decoder, self).__init__()
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.n_glimpses = n_glimpses
self.max_length = max_length
self.terminating_symbol = terminating_symbol
self.decode_type = decode_type
self.beam_size = beam_size
self.use_cuda = use_cuda
self.input_weights = nn.Linear(embedding_dim, 4 * hidden_dim)
self.hidden_weights = nn.Linear(hidden_dim, 4 * hidden_dim)
self.pointer = Attention(hidden_dim, use_tanh=use_tanh, C=tanh_exploration, use_cuda=self.use_cuda)
self.glimpse = Attention(hidden_dim, use_tanh=False, use_cuda=self.use_cuda)
self.sm = nn.Softmax()
def apply_mask_to_logits(self, step, logits, mask, prev_idxs):
if mask is None:
mask = torch.zeros(logits.size()).byte()
if self.use_cuda:
mask = mask.cuda()
maskk = mask.clone()
# to prevent them from being reselected.
# Or, allow re-selection and penalize in the objective function
if prev_idxs is not None:
# set most recently selected idx values to 1
maskk[[x for x in range(logits.size(0))],
prev_idxs.data] = 1
logits[maskk] = -np.inf
return logits, maskk
def forward(self, decoder_input, embedded_inputs, hidden, context):
"""
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]
"""
def recurrence(x, hidden, logit_mask, prev_idxs, step):
hx, cx = hidden # batch_size x hidden_dim
gates = self.input_weights(x) + self.hidden_weights(hx)
ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)
ingate = F.sigmoid(ingate)
forgetgate = F.sigmoid(forgetgate)
cellgate = F.tanh(cellgate)
outgate = F.sigmoid(outgate)
cy = (forgetgate * cx) + (ingate * cellgate)
hy = outgate * F.tanh(cy) # batch_size x hidden_dim
g_l = hy
for i in range(self.n_glimpses):
ref, logits = self.glimpse(g_l, context)
logits, logit_mask = self.apply_mask_to_logits(step, logits, logit_mask, prev_idxs)
# [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)
logits, logit_mask = self.apply_mask_to_logits(step, logits, logit_mask, prev_idxs)
probs = self.sm(logits)
return hy, cy, probs, logit_mask
batch_size = context.size(1)
outputs = []
selections = []
steps = range(self.max_length) # or until terminating symbol ?
inps = []
idxs = None
mask = None
if self.decode_type == "stochastic":
for i in steps:
hx, cx, probs, mask = recurrence(decoder_input, hidden, mask, idxs, i)
hidden = (hx, cx)
# select the next inputs for the decoder [batch_size x hidden_dim]
decoder_input, idxs = self.decode_stochastic(
probs,
embedded_inputs,
selections)
inps.append(decoder_input)
# use outs to point to next object
outputs.append(probs)
selections.append(idxs)
return (outputs, selections), hidden
elif self.decode_type == "beam_search":
# Expand input tensors for beam search
decoder_input = Variable(decoder_input.data.repeat(self.beam_size, 1))
context = Variable(context.data.repeat(1, self.beam_size, 1))
hidden = (Variable(hidden[0].data.repeat(self.beam_size, 1)),
Variable(hidden[1].data.repeat(self.beam_size, 1)))
beam = [
Beam(self.beam_size, self.max_length, cuda=self.use_cuda)
for k in range(batch_size)
]
for i in steps:
hx, cx, probs, mask = recurrence(decoder_input, hidden, mask, idxs, i)
hidden = (hx, cx)
probs = probs.view(self.beam_size, batch_size, -1
).transpose(0, 1).contiguous()
n_best = 1
# select the next inputs for the decoder [batch_size x hidden_dim]
decoder_input, idxs, active = self.decode_beam(probs,
embedded_inputs, beam, batch_size, n_best, i)
inps.append(decoder_input)
# use probs to point to next object
if self.beam_size > 1:
outputs.append(probs[:, 0,:])
else:
outputs.append(probs.squeeze(0))
# Check for indexing
selections.append(idxs)
# Should be done decoding
if len(active) == 0:
break
decoder_input = Variable(decoder_input.data.repeat(self.beam_size, 1))
return (outputs, selections), hidden
def decode_stochastic(self, probs, embedded_inputs, selections):
"""
Return the next input for the decoder by selecting the
input corresponding to the max output
Args:
probs: [batch_size x sourceL]
embedded_inputs: [sourceL x batch_size x embedding_dim]
selections: list of all of the previously selected indices during decoding
Returns:
Tensor of size [batch_size x sourceL] containing the embeddings
from the inputs corresponding to the [batch_size] indices
selected for this iteration of the decoding, as well as the
corresponding indicies
"""
batch_size = probs.size(0)
# idxs is [batch_size]
idxs = probs.multinomial().squeeze(1)
# due to race conditions, might need to resample here
for old_idxs in selections:
# compare new idxs
# elementwise with the previous idxs. If any matches,
# then need to resample
if old_idxs.eq(idxs).data.any():
print(' [!] resampling due to race condition')
idxs = probs.multinomial().squeeze(1)
break
sels = embedded_inputs[idxs.data, [i for i in range(batch_size)], :]
return sels, idxs
def decode_beam(self, probs, embedded_inputs, beam, batch_size, n_best, step):
active = []
for b in range(batch_size):
if beam[b].done:
continue
if not beam[b].advance(probs.data[b]):
active += [b]
all_hyp, all_scores = [], []
for b in range(batch_size):
scores, ks = beam[b].sort_best()
all_scores += [scores[:n_best]]
hyps = zip(*[beam[b].get_hyp(k) for k in ks[:n_best]])
all_hyp += [hyps]
all_idxs = Variable(torch.LongTensor([[x for x in hyp] for hyp in all_hyp]).squeeze())
if all_idxs.dim() == 2:
if all_idxs.size(1) > n_best:
idxs = all_idxs[:,-1]
else:
idxs = all_idxs
elif all_idxs.dim() == 3:
idxs = all_idxs[:, -1, :]
else:
if all_idxs.size(0) > 1:
idxs = all_idxs[-1]
else:
idxs = all_idxs
if self.use_cuda:
idxs = idxs.cuda()
if idxs.dim() > 1:
x = embedded_inputs[idxs.transpose(0,1).contiguous().data,
[x for x in range(batch_size)], :]
else:
x = embedded_inputs[idxs.data, [x for x in range(batch_size)], :]
return x.view(idxs.size(0) * n_best, embedded_inputs.size(2)), idxs, active
class PointerNetwork(nn.Module):
"""The pointer network, which is the core seq2seq
model"""
def __init__(self,
embedding_dim,
hidden_dim,
max_decoding_len,
terminating_symbol,
n_glimpses,
tanh_exploration,
use_tanh,
beam_size,
use_cuda):
super(PointerNetwork, self).__init__()
self.encoder = Encoder(
embedding_dim,
hidden_dim,
use_cuda)
self.decoder = Decoder(
embedding_dim,
hidden_dim,
max_length=max_decoding_len,
tanh_exploration=tanh_exploration,
use_tanh=use_tanh,
terminating_symbol=terminating_symbol,
decode_type="stochastic",
n_glimpses=n_glimpses,
beam_size=beam_size,
use_cuda=use_cuda)
# Trainable initial hidden states
dec_in_0 = torch.FloatTensor(embedding_dim)
if use_cuda:
dec_in_0 = dec_in_0.cuda()
self.decoder_in_0 = nn.Parameter(dec_in_0)
self.decoder_in_0.data.uniform_(-(1. / math.sqrt(embedding_dim)),
1. / math.sqrt(embedding_dim))
def forward(self, inputs):
""" Propagate inputs through the network
Args:
inputs: [sourceL x batch_size x embedding_dim]
"""
(encoder_hx, encoder_cx) = self.encoder.enc_init_state
encoder_hx = encoder_hx.unsqueeze(0).repeat(inputs.size(1), 1).unsqueeze(0)
encoder_cx = encoder_cx.unsqueeze(0).repeat(inputs.size(1), 1).unsqueeze(0)
# 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)
return pointer_probs, input_idxs
class CriticNetwork(nn.Module):
"""Useful as a baseline in REINFORCE updates"""
def __init__(self,
embedding_dim,
hidden_dim,
n_process_block_iters,
tanh_exploration,
use_tanh,
use_cuda):
super(CriticNetwork, self).__init__()
self.hidden_dim = hidden_dim
self.n_process_block_iters = n_process_block_iters
self.encoder = Encoder(
embedding_dim,
hidden_dim,
use_cuda)
self.process_block = Attention(hidden_dim,
use_tanh=use_tanh, C=tanh_exploration, use_cuda=use_cuda)
self.sm = nn.Softmax()
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
"""
(encoder_hx, encoder_cx) = self.encoder.enc_init_state
encoder_hx = encoder_hx.unsqueeze(0).repeat(inputs.size(1), 1).unsqueeze(0)
encoder_cx = encoder_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 NeuralCombOptRL(nn.Module):
"""
This module contains the PointerNetwork (actor) and
CriticNetwork (critic). It requires
an application-specific reward function
"""
def __init__(self,
input_dim,
embedding_dim,
hidden_dim,
max_decoding_len,
terminating_symbol,
n_glimpses,
n_process_block_iters,
tanh_exploration,
use_tanh,
beam_size,
objective_fn,
is_train,
use_cuda):
super(NeuralCombOptRL, self).__init__()
self.objective_fn = objective_fn
self.input_dim = input_dim
self.is_train = is_train
self.use_cuda = use_cuda
self.actor_net = PointerNetwork(
embedding_dim,
hidden_dim,
max_decoding_len,
terminating_symbol,
n_glimpses,
tanh_exploration,
use_tanh,
beam_size,
use_cuda)
#self.critic_net = CriticNetwork(
# embedding_dim,
# hidden_dim,
# n_process_block_iters,
# tanh_exploration,
# False,
# use_cuda)
embedding_ = torch.FloatTensor(input_dim,
embedding_dim)
if self.use_cuda:
embedding_ = embedding_.cuda()
self.embedding = nn.Parameter(embedding_)
self.embedding.data.uniform_(-(1. / math.sqrt(embedding_dim)),
1. / math.sqrt(embedding_dim))
def forward(self, inputs):
"""
Args:
inputs: [batch_size, input_dim, sourceL]
"""
batch_size = inputs.size(0)
input_dim = inputs.size(1)
sourceL = inputs.size(2)
# repeat embeddings across batch_size
# result is [batch_size x input_dim x embedding_dim]
embedding = self.embedding.repeat(batch_size, 1, 1)
embedded_inputs = []
# result is [batch_size, 1, input_dim, sourceL]
ips = inputs.unsqueeze(1)
for i in range(sourceL):
# [batch_size x 1 x input_dim] * [batch_size x input_dim x embedding_dim]
# result is [batch_size, embedding_dim]
embedded_inputs.append(torch.bmm(
ips[:, :, :, i].float(),
embedding).squeeze(1))
# Result is [sourceL x batch_size x embedding_dim]
embedded_inputs = torch.cat(embedded_inputs).view(
sourceL,
batch_size,
embedding.size(2))
# query the actor net for the input indices
# making up the output, and the pointer attn
probs_, action_idxs = self.actor_net(embedded_inputs)
# Select the actions (inputs pointed to
# by the pointer net) and the corresponding
# logits
# should be size [batch_size x
actions = []
# inputs is [batch_size, input_dim, sourceL]
inputs_ = inputs.transpose(1, 2)
# inputs_ is [batch_size, sourceL, input_dim]
for action_id in action_idxs:
actions.append(inputs_[[x for x in range(batch_size)], action_id.data, :])
if self.is_train:
# probs_ is a list of len sourceL of [batch_size x sourceL]
probs = []
for prob, action_id in zip(probs_, action_idxs):
probs.append(prob[[x for x in range(batch_size)], action_id.data])
else:
# return the list of len sourceL of [batch_size x sourceL]
probs = probs_
# get the critic value fn estimates for the baseline
# [batch_size]
#v = self.critic_net(embedded_inputs)
# [batch_size]
R = self.objective_fn(actions, self.use_cuda)
#return R, v, probs, actions, action_idxs
return R, probs, actions, action_idxs