我有这个功能:
import numpy as np
def unhot(vec):
""" takes a one-hot vector and returns the corresponding integer """
assert np.sum(vec) == 1 # this assertion shouldn't fail, but it did...
return list(vec).index(1)
我在以下调用的输出中调用:
numpy.random.multinomial(1, coe)
当我运行它时,我在某个时候出现了断言错误。这怎么可能?是否不保证numpy.random.multinomial的输出是单热向量?
然后,我删除了断言错误,现在有了:
ValueError: 1 is not in list
我缺少一些精美的打印纸,还是刚刚坏了?
最佳答案
嗯,这就是问题,我应该意识到,因为我之前遇到过:
np.random.multinomial(1,A([ 0., 0., np.nan, 0.]))
退货
array([0, 0, -9223372036854775807,0])
我正在使用不稳定的softmax实现,该实现提供了Nans。
现在,我试图确保通过多项式传递的参数的总和
coe = softmax(coeffs)
while np.sum(coe) > 1-1e-9:
coe /= (1+1e-5)
我认为,有了NaN,while语句将永远不会被触发。
关于python - numpy.random.multinomial错误输出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23257855/