Skip to content

Commit

Permalink
simplifies to_word
Browse files Browse the repository at this point in the history
simplifies the definition of `to_word` with `np.random.choice`
  • Loading branch information
Freakwill committed Jun 4, 2018
1 parent fc36b63 commit e24303d
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions compose_poem.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@


def to_word(predict, vocabs):
t = np.cumsum(predict)
s = np.sum(predict)
sample = int(np.searchsorted(t, np.random.rand(1) * s))
predict = predict[0]
predict /= np.sum(predict)
sample = np.random.choice(np.arange(len(predict)), p=predict)
if sample > len(vocabs):
sample = len(vocabs) - 1
return vocabs[sample]
return vocabs[-1]
else:
return vocabs[sample]


def gen_poem(begin_word):
Expand Down

0 comments on commit e24303d

Please sign in to comment.