[英]Passing input of a function returning NaN in Python
我正在使用EMCEE 。 我在下面写了一个从那里引起问题的部分。 我遇到此错误ValueError: lnprob returned NaN.
此错误中断了计算,我不知道该如何克服。 因此,我应该做一些事情来传递这个错误,以便继续进行其余的计算。
我唯一想到的就是在lnprob
函数中添加一行,例如:
if not np.isfinite(lp):
return -np.inf
if np.isnan(lp):
return -np.inf
但这是不正确的
代码是:
def log_prior(H0, od0, c, b, Orc, M):
if 0.4 < od0 < 0.9 and 50 < H0 < 90 and 0 < c < 3 and 0 < b < 1 and -0.3 < M < 0.2 and 0 < Orc < 0.1:
return 0.0
return -np.inf
def lnlike(H0, od0, c, b, Orc, M):
lg = -chi2(H0, od0, c, b, Orc, M)/2.
return lg
def lnprob(H0, od0, c, b, Orc, M):
lp = log_prior(H0, od0, c, b, Orc, M)
if not np.isfinite(lp):
return -np.inf
return lp + lnlike(H0, od0, c, b, Orc, M)
def func(theta):
H0, od0, c, b, Orc, M = theta
return -2. * lnprob(H0, od0, c, b, Orc, M)
我感谢您的帮助。
我只想说python如果lnprob是NaN,没关系,继续其他值
如果仍然适合您,请执行以下操作:
def func(theta):
H0, od0, c, b, Orc, M = theta
try:
lnprobval = -2. * lnprob(H0, od0, c, b, Orc, M)
except ValueError: # NaN value case
lnprobval = -np.inf # just set to negative infinity
return lnprobval
该代码应替换def func(theta)
的return
语句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.