私はPythonの初心者でもNumpyです。私は、次のコードにいくつかのランダム性を追加する必要がnumpy.random.choiceで乱数を追加する
:
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)]
私は、このテストしている:
int_to_vocab[np.random.choice(probabilities)]
をしかし、それは動作しません。
私はインターネットにもいますが、私の問題に関連するものは何も見つかりませんでした.Numpyは私にとって非常に混乱しています。
ここではnp.random.choice
をどうすれば使用できますか?
サンプルケース:
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
サンプルケースを追加しますか? – Divakar
numpyを使用する必要がありますか? – Olian04
サンプルが追加されました。はい、numpyを使用する必要があります。 – VansFannel