0

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
3
  • Add a sample case? Commented May 4, 2017 at 15:15
  • Do you have to use numpy? Commented May 4, 2017 at 15:18
  • Sample added, and yes, I have to use numpy. Commented May 4, 2017 at 15:18

1 Answer 1

3

Look at the documentation: https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html

The interface is numpy.random.choice(a, size=None, replace=True, p=None).

a is the number of words you want to pick from, i.e. len(probabilities).

size can stay at the default None as you only want one prediction.

replace should stay at True as you do not want to remove picked words.

And p=probabilities.

So you want to call:

np.random.choice(len(probabilities), p=probabilities)

You will get a number between 0 and num_words-1, which you then need to map accordingly (bijective and matching your ordering of probabilities) to your word ids, and use as an argument for int_to_vocab.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.