def sample_discrete_from_log(p_log,return_lognorms=False,axis=<NUM_LIT:0>,dtype=np.int32):
lognorms = logsumexp(p_log,axis=axis)<EOL>cumvals = np.exp(p_log - np.expand_dims(lognorms,axis)).cumsum(axis)<EOL>thesize = np.array(p_log.shape)<EOL>thesize[axis] = <NUM_LIT:1><EOL>randvals = random(size=thesize) *np.reshape(cumvals[[slice(None) if i is not axis else -<NUM_LIT:1><EOL>for i in range(p_log.ndim)]],thesize)<EOL>samples = np.sum(randvals > cumvals,axis=axis,dtype=dtype)<EOL>if return_lognorms:<EOL><INDENT>return samples, lognorms<EOL><DEDENT>else:<EOL><INDENT>return samples<EOL><DEDENT>
samples log probability array along specified axis