É possível obter amostras da distribuição categórica, dadas as probabilidades de log sem deixar espaço no log usando o truque Gumbel-max . A idéia é que, se você receber probabilidades log anormais , isso pode ser traduzido para probabilidades apropriadas usando a função softmaxα1,…,αk
pi=exp(αi)∑jexp(αj)
então, para amostrar a partir dessa distribuição, você pode usar o fato de que se são amostras independentes colhidas da distribuição Gumbel padrão parametrizada pelo local m ,g1,…,gk∼G(0)m
F(G≤g)=exp(−exp(−g+m))
então pode ser mostrado (veja as referências abaixo) que
argmaxi{gi+αi}maxi{gi+αi}∼exp(αi)∑jexp(αj)∼G(log∑iexp{αi})
e podemos pegar
z=argmaxi{gi+αi}
p1,…,pk probabilities. This approach was described in greater detail in blog entries by Ryan Adams and Laurent Dinh, moreover Chris J. Maddison, Daniel Tarlow and Tom Minka gave a speach (slides) on Neural Information Processing Systems conference (2014) and wrote a paper titled A* Sampling that generalized those ideas (see also Maddison, 2016; Maddison, Mnih and Teh, 2016; Jang and Poole, 2016), who refer to Yellott (1977) mentioning his as the one among those who first described this property.
It is pretty easy to implement it using inverse transform sampling by taking gi=−log(−logui) where ui are draws from uniform distribution on (0,1). It is certainly not the most time-efficient algorithms for sampling from categorical distribution, but it let's you to stay in log-space what may be an advantage in some scenarios.
Maddison, C. J., Tarlow, D., & Minka, T. (2014). A* sampling. [In:] Advances in Neural Information Processing Systems (pp. 3086-3094).
Yellott, J. I. (1977). The relationship between Luce's choice axiom, Thurstone's theory of comparative judgment, and the double exponential distribution. Journal of Mathematical Psychology, 15(2), 109-144.
Maddison, C. J., Mnih, A., & Teh, Y. W. (2016). The Concrete Distribution: A Continuous Relaxation of Discrete Random Variables. arXiv preprint arXiv:1611.00712.
Jang, E., Gu, S., & Poole, B. (2016). Categorical Reparameterization with Gumbel-Softmax. arXiv preprint arXiv:1611.01144.
Maddison, C. J. (2016). A Poisson process model for Monte Carlo. arXiv preprint arXiv:1602.05986.
exp
can lose precision, leading to distributions like [1.0, 3.45e-66, 0.0, 7.54e-121]. I'd like to hold out for some answer that is robust even in that case. But for now I'm upvoting your answer.