def sample_next(self, prev, incl_eos = True):
"""Samples a single word from context.
Can be useful to debug the model, for example if you have a bigram model,
and know the probability of X-Y should be really high, you can run
sample_next([Y]) to see how often X get generated.
incl_eos determines whether the space of words should include EOS or not.
"""
wps = []
tot = -np.inf # this is the log (total mass)
for w in self.lm.vocab():
if not incl_eos and w == "END_OF_SENTENCE":
continue
lp = self.lm.cond_logprob(w, prev)
wps.append([w, lp/self.temp])
tot = np.logaddexp2(lp/self.temp, tot)
p = self.rnd.random()
word = self.rnd.choice(wps)[0]
s = -np.inf # running mass
for w,lp in wps:
s = np.logaddexp2(s, lp)
if p < pow(2, s-tot):
word = w
break
return word
评论列表
文章目录