"vscode:/vscode.git/clone" did not exist on "0f23e6d3d49daee3ad8491d56aef4c90c8a89848"
Commit cce6c09b authored by Rodrigo Laguna's avatar Rodrigo Laguna Committed by Chris Shallue
Browse files

speedup caption generator on im2txt (#6255)

* replace the usage of python's built in sort with numpy's argsort, which is faster. Also use the given axes to get the most_likely_words instead of calling enumerate and list (it's slower and more memory conssuming)

* correct indentation and redaction on comment for the changes purposes according to cshallue's comment
parent e4a046e7
...@@ -176,11 +176,13 @@ class CaptionGenerator(object): ...@@ -176,11 +176,13 @@ class CaptionGenerator(object):
word_probabilities = softmax[i] word_probabilities = softmax[i]
state = new_states[i] state = new_states[i]
# For this partial caption, get the beam_size most probable next words. # For this partial caption, get the beam_size most probable next words.
words_and_probs = list(enumerate(word_probabilities)) # Sort the indexes with numpy, select the last self.beam_size
words_and_probs.sort(key=lambda x: -x[1]) # (3 by default) (ie, the most likely) and then reverse the sorted
words_and_probs = words_and_probs[0:self.beam_size] # indexes with [::-1] to sort them from higher to lower.
# Each next word gives a new partial caption. most_likely_words = np.argsort(word_probabilities)[:-self.beam_size][::-1]
for w, p in words_and_probs:
for w in most_likely_words:
p = word_probabilities[w]
if p < 1e-12: if p < 1e-12:
continue # Avoid log(0). continue # Avoid log(0).
sentence = partial_caption.sentence + [w] sentence = partial_caption.sentence + [w]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment