I think random.choice() based on a probability distribution works quite well. Otherwise, it will be like what you say…
We also don’t want to use the index with maximum probability distribution as it will make our result repetitive.
Let’s look at the result of our unit test for sample(), and see how actual index was selected from a probability distribution given by Softmax. The output from sample() was;
Sampling:
list of sampled indices:
[23, 16, 26, 26, 24, 3, 21, 1, 7, 24, 15, 3, 25, 20, 6, 13, 10, 8, 20, 12, 2, 0]
Then, I add a few lines to log probability distributions, and show how those indices were selected from given probability distributions.
Start from the top-left, and go to the right. Then, one row down, and start from the left.
See 3rd, 5th, 10th,… You can see np.random.choice() selected an index which is not the maximum value. It’s working well.
If we select the maximum value, the result of Exercise 4 will be;
j = 0 idx = 0
single_example = turiasaurus
single_example_chars ['t', 'u', 'r', 'i', 'a', 's', 'a', 'u', 'r', 'u', 's']
single_example_ix [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
X = [None, 20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
Y = [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19, 0]
Iteration: 0, Loss: 23.087336
Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
j = 1535 idx = 1535
j = 1536 idx = 0
Iteration: 2000, Loss: 27.884160
Aurus
Aurus
Aurus
Aurus
Aurus
Aurus
Aurus
Iteration: 4000, Loss: 25.901815
Angosaurus
Angosaurus
Angosaurus
Angosaurus
Angosaurus
Angosaurus
Angosaurus
This is not an interesting result.
Hope this clarifies.