基于模拟的推理基准
项目描述
基于模拟的推理基准
此存储库包含一个基于模拟的推理基准框架sbibm
,我们在相关论文“基准测试基于模拟的推理”中进行了描述。论文的简短摘要和交互式结果可以在项目网站上找到:https://sbi-benchmark.github.io
基准框架包括任务、参考后验、度量、绘图以及与SBI工具箱的集成。该框架设计为高度可扩展,易于在以下新研究项目中使用。
为了强调sbibm
可以独立于任何特定的分析流程使用,我们将论文实验的复现代码分割到github.com/sbi-benchmark/results/上的独立存储库中。除了复现论文实验的流程外,该存储库还包括完整的结果,包括用于快速比较的数据框。
如果您有任何问题或评论,请随时联系我们或打开问题。我们邀请贡献,例如,新的任务、新颖的度量或其他SBI工具箱的包装器。
安装
假设您有一个工作的Python环境,只需通过pip
安装sbibm
$ pip install sbibm
基于常微分方程(ODE)的模型(目前包括SIR和Lotka-Volterra模型)通过Julia语言和diffeqtorch
库进行实现。如果您打算使用这些任务,请额外遵循diffeqtorch
的安装说明。如果您目前不打算模拟这些任务,可以跳过这一步。
快速入门
以下是sbibm
的快速演示,更详细的解释请见下文
import sbibm
task = sbibm.get_task("two_moons") # See sbibm.get_available_tasks() for all tasks
prior = task.get_prior()
simulator = task.get_simulator()
observation = task.get_observation(num_observation=1) # 10 per task
# These objects can then be used for custom inference algorithms, e.g.
# we might want to generate simulations by sampling from prior:
thetas = prior(num_samples=10_000)
xs = simulator(thetas)
# Alternatively, we can import existing algorithms, e.g:
from sbibm.algorithms import rej_abc # See help(rej_abc) for keywords
posterior_samples, _, _ = rej_abc(task=task, num_samples=10_000, num_observation=1, num_simulations=100_000)
# Once we got samples from an approximate posterior, compare them to the reference:
from sbibm.metrics import c2st
reference_samples = task.get_reference_posterior_samples(num_observation=1)
c2st_accuracy = c2st(reference_samples, posterior_samples)
# Visualise both posteriors:
from sbibm.visualisation import fig_posterior
fig = fig_posterior(task_name="two_moons", observation=1, samples=[posterior_samples])
# Note: Use fig.show() or fig.save() to show or save the figure
# Get results from other algorithms for comparison:
from sbibm.visualisation import fig_metric
results_df = sbibm.get_results(dataset="main_paper.csv")
fig = fig_metric(results_df.query("task == 'two_moons'"), metric="C2ST")
任务
您可以通过调用sbibm.get_available_tasks()
来查看可用的任务列表。如果我们想使用,例如,two_moons
任务,我们可以使用sbibm.get_task
来加载它,如下所示
import sbibm
task = sbibm.get_task("slcp")
接下来,我们可能想要获取prior
和simulator
prior = task.get_prior()
simulator = task.get_simulator()
如果我们调用prior()
,我们将从先验分布中获得一个单次抽取。可以提供num_samples
作为可选参数。以下将生成100个模拟器样本
thetas = prior(num_samples=100)
xs = simulator(thetas)
xs
是一个形状为(100, 8)
的torch.Tensor
,因为对于SLCP,数据是八维的。请注意,如果需要,转换为和从torch.Tensor
的转换非常简单:使用.numpy()
转换为numpy数组,例如xs.numpy()
。对于反向转换,在numpy数组上使用torch.from_numpy()
。
某些算法可能需要评估先验分布的pdf,这可以通过使用task.get_prior_dist()
获得的torch.Distribution
实例来获得,该实例公开了log_prob
和sample
方法。先验的参数可以作为字典参数使用task.get_prior_params()
。
对于每个任务,基准测试包含10个观察结果和相应的参考后验样本。要获取第一个观察结果和相应的参考后验样本
observation = task.get_observation(num_observation=1)
reference_samples = task.get_reference_posterior_samples(num_observation=1)
每个任务都有一些有用的属性,包括
task.dim_data # dimensionality data, here: 8
task.dim_parameters # dimensionality parameters, here: 5
task.num_observations # number of different observations x_o available, here: 10
task.name # name: slcp
task.name_display # name_display: SLCP
最后,如果您想查看任务的源代码,请查看sbibm/tasks/slcp/task.py
。如果您想实现一个新任务,我们建议模仿现有任务的模式。您会发现每个任务都有一个私有的_setup
方法,该方法用于生成参考后验样本。
算法
如简介中所述,sbibm
包装了多个第三方包以运行各种算法。我们发现给每个算法提供相同的接口是最简单的:通常,每个算法指定一个run
函数,该函数以task
和超参数作为参数,并最终返回所需的num_posterior_samples
。这样,一个人可以简单地导入算法的运行函数,在给定的任务上调整它,并返回样本的指标。外部工具箱实现算法的包装器位于子文件夹sbibm/algorithms
中。目前提供了与sbi
、pyabc
、pyabcranger
以及与elfi
的实验性集成。
指标
为了在基准测试中比较算法,可以计算许多不同的指标。每个任务都为每个观察结果提供了参考样本。根据基准测试,这些样本要么是通过使用后验的解析解获得,要么是通过定制基于似然的逼近方法获得。
可以通过比较算法样本与参考样本来计算许多指标。为了做到这一点,可以计算许多不同的双样本测试(请参阅sbibm/metrics
)。这些测试遵循一个简单的接口,只需要传递来自参考和算法的样本。
例如,为了计算C2ST
import torch
from sbibm.metrics.c2st import c2st
from sbibm.algorithms import rej_abc
reference_samples = task.get_reference_posterior_samples(num_observation=1)
algorithm_samples, _, _ = rej_abc(task=task, num_samples=10_000, num_simulations=100_000, num_observation=1)
c2st_accuracy = c2st(reference_samples, algorithm_samples)
更多信息,请参阅help(c2st)
。
图形
sbibm
包括用于绘图的结果代码,例如,用于绘制特定任务的指标
from sbibm.visualisation import fig_metric
results_df = sbibm.get_results(dataset="main_paper.csv")
results_subset = results_df.query("task == 'two_moons'")
fig = fig_metric(results_subset, metric="C2ST") # Use fig.show() or fig.save() to show or save the figure
它也可以用来绘制后验概率图,例如,将推理算法的结果与参考样本进行比较。
from sbibm.visualisation import fig_posterior
fig = fig_posterior(task_name="two_moons", observation=1, samples=[algorithm_samples])
结果与实验
我们将在github.com/sbi-benchmark/results/tree/main/benchmarking_sbi的单独存储库中托管结果和重现手稿实验的代码:这包括重现手稿实验的流程以及用于新比较的数据框。
引用
手稿可通过PMLR获取http://proceedings.mlr.press/v130/lueckmann21a.html
@InProceedings{lueckmann2021benchmarking,
title = {Benchmarking Simulation-Based Inference},
author = {Lueckmann, Jan-Matthis and Boelts, Jan and Greenberg, David and Goncalves, Pedro and Macke, Jakob},
booktitle = {Proceedings of The 24th International Conference on Artificial Intelligence and Statistics},
pages = {343--351},
year = {2021},
editor = {Banerjee, Arindam and Fukumizu, Kenji},
volume = {130},
series = {Proceedings of Machine Learning Research},
month = {13--15 Apr},
publisher = {PMLR}
}
支持
本研究得到了德国研究基金会(DFG;SFB 1233 PN 276693517,SFB 1089,SPP 2041,德国卓越战略 – EXC编号2064/1 PN 390727645)和德国联邦教育和研究部(BMBF;项目“ADIMEM”,FKZ 01IS18052 A-D)”的支持。
许可证
MIT
项目详情
下载文件
下载适合您平台文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分发
构建分发
sbibm-1.1.0.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 93417444b4abc773723f2a513ef00012f52efe5d003c78cce27fd82842c4ff4e |
|
MD5 | aab85835736571a591e7cec58321228c |
|
BLAKE2b-256 | 4bc2a23798fa032aacd7b2e4ad0427d392c3de2cb913f1f9072d904f893490b0 |
sbibm-1.1.0-py2.py3-none-any.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 12ecc6b4d327b92f9225f08faa6405e4274fb8e75943d86a53157d1b1bcfaf74 |
|
MD5 | 651d55c2db638f4ad0fe9a61e1a2598b |
|
BLAKE2b-256 | 6a3dca6a49f7d8ad77f274087b58584eb61e7d13d2c52181386a4fb29caa897f |