【发布时间】:2014-04-24 00:19:36
【问题描述】:
当我使用这个随机生成器:numpy.random.multinomial,我不断得到:
ValueError: sum(pvals[:-1]) > 1.0
我总是传递这个 softmax 函数的输出:
def softmax(w, t = 1.0):
e = numpy.exp(numpy.array(w) / t)
dist = e / np.sum(e)
return dist
除了现在我收到此错误外,我还为参数添加了此错误 (pvals):
while numpy.sum(pvals) > 1:
pvals /= (1+1e-5)
但这并没有解决问题。确保避免此错误的正确方法是什么?
编辑:这是包含此代码的函数
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])
【问题讨论】:
-
你检查过所有的值都是有限的吗?
-
当您将
pvals.sum()传递给np.random.multinomial时,它的值是多少? -
有些东西你没有展示出来,比如
A, and the full traceback (including the actual code,sum(pvals[:-1]) > 1.0` is not here)。 -
A 是 numpy.array,我已经替换了它。
-
如果你使用
np.random.choice而不是np.random.multinomial,你不会得到错误并且得到相同的结果。
标签: python numpy random numerical-stability