Choose list variable given probability of each variable

后端 未结 5 1433
野趣味
野趣味 2020-12-08 00:34

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         


        
5条回答
  •  伪装坚强ぢ
    2020-12-08 01:12

    This problem is equivalent to sampling from a categorical distribution. This distribution is commonly conflated with the multinomial distribution which models the result of multiple samples from a categorical distribution.

    In numpy, it is easy to sample from the multinomial distribution using numpy.random.multinomial, but a specific categorical version of this does not exist. However, it can be accomplished by sampling from the multinomial distribution with a single trial and then returning the non-zero element in the output.

    import numpy as np
    pvals = [0.10,0.25,0.60,0.05]
    ind = np.where(np.random.multinomial(1,pvals))[0][0]
    

提交回复
热议问题