Skip to content

Commit ea513ed

Browse files
ZxYuanfrankchn
authored andcommitted
Update word2vec_basic.py (tensorflow#13531)
Use random.sample to simplify random selection of context words
1 parent 251a1e7 commit ea513ed

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

tensorflow/examples/tutorials/word2vec/word2vec_basic.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,9 @@ def generate_batch(batch_size, num_skips, skip_window):
115115
data_index += span
116116
for i in range(batch_size // num_skips):
117117
context_words = [w for w in range(span) if w != skip_window]
118-
random.shuffle(context_words)
119-
words_to_use = collections.deque(context_words)
120-
for j in range(num_skips):
118+
words_to_use = random.sample(context_words, num_skips)
119+
for j, context_word in enumerate(words_to_use):
121120
batch[i * num_skips + j] = buffer[skip_window]
122-
context_word = words_to_use.pop()
123121
labels[i * num_skips + j, 0] = buffer[context_word]
124122
if data_index == len(data):
125123
buffer[:] = data[:span]

0 commit comments

Comments
 (0)