如何使用GPflow从多个链中采样?

2024-05-15 11:12:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近开始使用gpflow构建GP模型。我用一个哈密顿蒙特卡罗方法,用一条单链对后面的物体进行采样

我的目标是运行多个链并执行收敛诊断

这是我为一条链设置的:

num_burnin_steps = ci_niter(100)
num_samples = ci_niter(500)


hmc_helper = gpflow.optimizers.SamplingHelper(
    model.log_posterior_density, model.trainable_parameters
)

hmc = tfp.mcmc.HamiltonianMonteCarlo(
    target_log_prob_fn=hmc_helper.target_log_prob_fn, num_leapfrog_steps=10, step_size=0.01
)

adaptive_hmc = tfp.mcmc.SimpleStepSizeAdaptation(
    hmc, num_adaptation_steps=10, target_accept_prob=f64(0.75), adaptation_rate=0.1
)


@tf.function
def run_chain_fn():
    return tfp.mcmc.sample_chain(
        num_results=num_samples,
        num_burnin_steps=num_burnin_steps,
        current_state=hmc_helper.current_state,
        kernel=adaptive_hmc,
        trace_fn=lambda _, pkr: pkr.inner_results.is_accepted,
    )

samples, _ = run_chain_fn()
constrained_samples = hmc_helper.convert_to_constrained_values(samples

我找不到任何关于如何修改多个链的示例Here是我能找到的做同样事情的最接近的例子。初始_状态更改为有10条链。要对我的示例执行同样的操作,我需要更改hmc_helper.current_状态,但无法找到正确的方法

如有任何建议,将不胜感激

我是个新手,所以如果我的问题不清楚,我道歉

谢谢


Tags: helperlogchaintargetcurrentstepsnumfn