Skip to content

Commit

Permalink
fix to beam search stopping criteria (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Matthews authored and neubig committed May 23, 2019
1 parent a87e7b9 commit ca4eac4
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion xnmt/search_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,18 @@ def generate_output(self,
completed_hyp = []
for length in range(self.max_len):
if len(completed_hyp) >= self.beam_size:
break
completed_hyp = sorted(completed_hyp, key=lambda hyp: hyp.score, reverse=True)
completed_hyp = completed_hyp[:self.beam_size]
worst_complete_hyp_score = completed_hyp[-1].score
active_hyp = [hyp for hyp in active_hyp if hyp.score >= worst_complete_hyp_score]
# Assumption: each additional word will always *decrease* the total score.
if len(active_hyp) == 0:
break

# Expand hyp
new_set = []
for hyp in active_hyp:
# Note: prev_word has *not* yet been added to prev_state
if length > 0:
prev_word = hyp.word
prev_state = hyp.output.state
Expand Down

0 comments on commit ca4eac4

Please sign in to comment.