Skip to content

Instantly share code, notes, and snippets.

@Tony607
Created February 23, 2018 09:44
Show Gist options
  • Save Tony607/d7d806d406881714e267c386adf95f9a to your computer and use it in GitHub Desktop.
Save Tony607/d7d806d406881714e267c386adf95f9a to your computer and use it in GitHub Desktop.
source: How to generate realistic yelp restaurant reviews with Keras | DLology
def sample(preds, temperature=1.0):
'''
Generate some randomness with the given preds
which is a list of numbers, if the temperature
is very small, it will always pick the index
with highest pred value
'''
preds = np.asarray(preds).astype('float64')
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1, preds, 1)
return np.argmax(probas)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment