I've been trying to code a program that uses the softmax activation function in the middle.
Right now, I have a list of probabilities like this:
P[0.10,0.25,0.60,0.05]
The sum of all the variables in P is always 1.
I wanted a way to pick the index of the list given the probability attached to it. Or, in other words, a function that returned
0 - 10% of the time 1 - 25% of the time 2 - 60% of the time 3 - 5% of the time
I've absolutely no idea where to start on this. Any help would be appreciated. :)
Relative weights to choose elements from the list with different probability. First, define the probability for each element. If you specified the probability using the relative weight, the selections are made according to the relative weights. You can set relative weights using the weight parameter.
Use the random. sample() function when you want to choose multiple random items from a list without repetition or duplicates. There is a difference between choice() and choices() . The choices() was added in Python 3.6 to choose n elements from the list randomly, but this function can repeat items.
randrange() Method to Randomly Select Elements From a List. This method is used to generate a random number in a range, for lists, we can specify the range to be 0 to its length, and get the index, and then the corresponding value.
You can easily achieve this with numpy. It has a choice function which accepts the parameter of probabilities.
np.random.choice( ['pooh', 'rabbit', 'piglet', 'Christopher'], 5, p=[0.5, 0.1, 0.1, 0.3] )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With