Created
February 23, 2018 09:44
-
-
Save Tony607/d7d806d406881714e267c386adf95f9a to your computer and use it in GitHub Desktop.
source: How to generate realistic yelp restaurant reviews with Keras | DLology
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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