How to avoid value errors when using numpy.random.multinomial?

When I use this random generator: numpy.random.multinomialI keep getting:

ValueError: sum(pvals[:-1]) > 1.0

I always pass the output of this softmax function:

def softmax(w, t = 1.0):
    e = numpy.exp(numpy.array(w) / t)
    dist = e / np.sum(e)
    return dist

in addition to getting this error, I also added this for the ( pvals) parameter :

while numpy.sum(pvals) > 1:
    pvals /= (1+1e-5)

but it didn’t decide. What is the correct way to avoid this error?

EDIT: here is the function that includes this 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])
+5
source share
4 answers

I also encountered this problem during my work on language modeling.

numpy : sorfmax() float32, numpy.random.multinomial() pval float64 IMPLICITLY. , pval.sum() 1.0, - .

+3

-, : softmax , logsumexp :

from scipy.misc import logsumexp

def log_softmax(vec):
    return vec - logsumexp(vec)

def softmax(vec):
    return np.exp(log_softmax(vec))

:

print(softmax(np.array([1.0, 0.0, -1.0, 1.1])))

, ?

+2

softmax, , , . 1 (, 1.0000024...).

while. NaN, , .

, numpy.random.multinomial , NaN.

, :

def softmax(vec):
    vec -= min(A(vec))
    if max(vec) > 700:
        a = np.argsort(vec)
        aa = np.argsort(a)
        vec = vec[a]
        i = 0
        while max(vec) > 700:
            i += 1
            vec -= vec[i]
        vec = vec[aa]
    e = np.exp(vec)
    return e/np.sum(e)

def sample_multinomial(w):
    """
       Sample multinomial distribution with parameters given by softmax of w
       Returns an int    
    """
    p = softmax(w)
    x = np.random.uniform(0,1)
    for i,v in enumerate(np.cumsum(p)):
        if x < v: return i
    return len(p)-1 # shouldn't happen...
+1

, , , , . , :

a = np.asarray(a).astype('float64')
a = a / np.sum(a)
b = np.random.multinomial(1, a, 1)

. , , , . float64, .

0

Source: https://habr.com/ru/post/1537801/


All Articles