forked from hunkim/word-rnn-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeam.py
51 lines (40 loc) · 2.18 KB
/
beam.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
import tensorflow as tf
import numpy as np
class BeamSearch():
def __init__(self, probs):
self.probs = probs
def beamsearch(self, oov, empty, eos, k=1, maxsample=4000, use_unk=False):
"""return k samples (beams) and their NLL scores, each sample is a sequence of labels,
all samples starts with an `empty` label and end with `eos` or truncated to length of `maxsample`.
You need to supply `predict` which returns the label probability of each sample.
`use_unk` allow usage of `oov` (out-of-vocabulary) label in samples
"""
dead_k = 0 # samples that reached eos
dead_samples = []
dead_scores = []
live_k = 1 # samples that did not yet reached eos
live_samples = [[empty]]
live_scores = [0]
while live_k and dead_k < k:
# total score for every sample is sum of -log of word prb
cand_scores = np.array(live_scores)[:, None] - np.log(self.probs)
if not use_unk and oov is not None:
cand_scores[:, oov] = 1e20
cand_flat = cand_scores.flatten()
# find the best (lowest) scores we have from all possible samples and new words
ranks_flat = cand_flat.argsort()[:(k - dead_k)]
live_scores = cand_flat[ranks_flat]
# append the new words to their appropriate live sample
voc_size = self.probs.shape[1]
live_samples = [live_samples[r // voc_size] + [r % voc_size] for r in ranks_flat]
# live samples that should be dead are...
zombie = [s[-1] == eos or len(s) >= maxsample for s in live_samples]
# add zombies to the dead
dead_samples += [s for s, z in zip(live_samples, zombie) if z] # remove first label == empty
dead_scores += [s for s, z in zip(live_scores, zombie) if z]
dead_k = len(dead_samples)
# remove zombies from the living
live_samples = [s for s, z in zip(live_samples, zombie) if not z]
live_scores = [s for s, z in zip(live_scores, zombie) if not z]
live_k = len(live_samples)
return dead_samples + live_samples, dead_scores + live_scores