I'm newbie in python and also in Numpy.
I have to add some randomness to the following code:
def pick_word(probabilities, int_to_vocab):
"""
Pick the next word in the generated text
:param probabilities: Probabilites of the next word
:param int_to_vocab: Dictionary of word ids as the keys and words as the values
:return: String of the predicted word
"""
return int_to_vocab[np.argmax(probabilities)]
I have tested this:
int_to_vocab[np.random.choice(probabilities)]
But it doesn't work.
I have also on Internet and I haven't found anything related to my problem and the Numpy is very confusing for me.
How can I use np.random.choice here?
Sample case:
284 test_int_to_vocab = {word_i: word for word_i, word in enumerate(['this', 'is', 'a', 'test'])}
285
--> 286 pred_word = pick_word(test_probabilities, test_int_to_vocab)
287
288 # Check type
<ipython-input-6-2aff0e70ab48> in pick_word(probabilities, int_to_vocab)
6 :return: String of the predicted word
7 """
----> 8 return int_to_vocab[np.random.choice(probabilities)]
9
10
KeyError: 0.050000000000000003