问题描述
当我使用此随机生成器时: numpy.random.multinomial
,我不断得到:
When I use this random generator: numpy.random.multinomial
, I keep getting:
ValueError: sum(pvals[:-1]) > 1.0
我一直在传递此softmax函数的输出:
I am always passing 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
除了我现在遇到此错误外,我还为参数(pvals
)添加了它:
except now that I am getting this error, I also added this for the parameter (pvals
):
while numpy.sum(pvals) > 1:
pvals /= (1+1e-5)
但是并不能解决问题.确保避免此错误的正确方法是什么?
but that didn't solve it. What is the right way to make sure I avoid this error?
这是包含此代码的函数
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])
推荐答案
在我的语言建模工作中,我也遇到了这个问题.
I also encountered this problem during my language modelling work.
此问题的根源来自numpy的隐式数据转换:我的sorfmax()的输出为float32
类型,但是,numpy.random.multinomial()
会将pval
强制转换为float64
类型.有时由于数值舍入,这种数据类型转换会导致pval.sum()
超过1.0.
The root of this problem rises from numpy's implicit data casting: the output of my sorfmax() is in float32
type, however, numpy.random.multinomial()
will cast the pval
into float64
type IMPLICITLY. This data type casting would cause pval.sum()
exceed 1.0 sometimes due to numerical rounding.
此问题已得到认可并发布在此处
This issue is recognized and posted here
这篇关于使用numpy.random.multinomial时如何避免值错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!