python - How can I avoid value errors when using numpy.random.multinomial? -
when use random generator: numpy.random.multinomial
, keep getting:
valueerror: sum(pvals[:-1]) > 1.0
i passing output of softmax function:
def softmax(w, t = 1.0): e = numpy.exp(numpy.array(w) / t) dist = e / np.sum(e) return dist
except getting error, added parameter (pvals
):
while numpy.sum(pvals) > 1: pvals /= (1+1e-5)
but didn't solve it. right way make sure avoid error?
edit: here function includes code
def get_mdn_prediction(vec): coeffs = vec[::3] means = vec[1::3] stds = np.log(1+np.exp(vec[2::3])) stds = np.maximum(stds, min_std) coe = softmax(coeffs) while np.sum(coe) > 1-1e-9: coe /= (1+1e-5) coeff = unhot(np.random.multinomial(1, coe)) return np.random.normal(means[coeff], stds[coeff])
something few people noticed: robust version of softmax can obtained removing logsumexp values:
from scipy.misc import logsumexp def log_softmax(vec): return vec - logsumexp(vec) def softmax(vec): return np.exp(log_softmax(vec))
just check it:
print(softmax(np.array([1.0, 0.0, -1.0, 1.1])))
simple, isn't it?
Comments
Post a Comment