有没有办法让pymc3在不满足target_accept时引发异常?

时间:2018-05-08 17:25:57

标签: python pymc pymc3

我目前正在循环中运行采样和训练模型。我希望在他们不符合target_except要求时跳过那些。这有可能吗?

Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [defs_star, atts_star, intercept, sd_def_log__, sd_att_log__, home]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 2000/2000 [02:31<00:00, 13.21it/s]
There were 5 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.227249509897, but should be close to 0.8. Try to increase the number of tuni
ng steps.
The gelman-rubin statistic is larger than 1.05 for some parameters. This indicates slight problems during sampling.
The estimated number of effective samples is smaller than 200 for some parameters.
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 3000/3000 [00:06<00:00, 467.94it/s]
[8, [56]]

修改

我不想更改参数或调整当前模型。这将是另一个问题。

1 个答案:

答案 0 :(得分:0)

目前有两种方法可以做到这一点:第一种是祝福&#34;祝福&#34;方式,现在不是很灵活。

with pm.Model():
    ...
    trace = pm.sample()

trace.report.raise_ok()

这会导致ValueError出现分歧或调整错误,但这是相当新的,因此您无法轻易指定分歧类型。

另一种不太稳定的方法是,使用与上面相同的trace对象

from pymc3.backends.report import WarningType


if any(w.kind is WarningType.BAD_ACCEPTANCE for w in trace.report._warnings):
    raise SomeCustomError(w.message)

_warnings属性标记为私有,可能会在将来发生变化,但可以更灵活地访问收敛统计信息。