def sample_top(a=[], top_k=10):
 idx = np.argpartition(a, (- top_k))[(- top_k):]
 probs = a[idx]
 probs = (probs / np.sum(probs))
 choice = np.random.choice(idx, p=probs)
 return choice

def sample_top ( a = [ ] , top_k = 10 ) : idx = np . argpartition ( a , ( - top_k ) ) [ ( - top_k ) : ] probs = a [ idx ] probs = ( probs / np . sum ( probs ) ) choice = np . random . choice ( idx , p = probs ) return choice
sample from top_k probabilities .