推理健身房是锻炼推理方法的地方,以帮助它们更快、更精简和更健壮。
项目描述
Inference Gym
概述
推理健身房是练习推理方法的地方,以帮助它们更快、更精简和更健壮。推理健身房的目标是提供一套具有标准化接口的概率推理问题,便于在各种具有挑战性的任务中测试新的推理技术。
目前它提供了一个概率模型存储库,可用于评估(推理算法的)计算和统计性能。概率模型作为Model
类的子类实现,该类至少提供以下功能
- 模型参数的形状和dtypes描述。
- 事件空间双射函数,将无约束的实空间映射到模型关联密度的支撑集。
- 计算在特定参数设置下的对数未归一化密度。
- 模型名称。
- 样本转换,当应用于模型密度的样本时,表示具有有用解释的量。
每个模型还可以提供
- 与每个样本转换关联的地面真相量。这可能包括均值、方差和其他统计数据。如果这些通过蒙特卡洛方法估计,还提供了一个标准误差。这可以用来验证算法的偏差水平。
入门指南
查看教程。
用法
pip install tfp-nightly inference_gym
# Install at least one the folowing
pip install tf-nightly # For the TensorFlow backend.
pip install jax jaxlib # For the JAX backend.
# Install to support external datasets
pip install tfds-nightly
import matplotlib.pyplot as plt
import numpy as np
from inference_gym import using_tensorflow as
inference_gym
model = inference_gym.targets.GermanCreditNumericLogisticRegression()
samples = inference_method(
model.unnormalized_log_prob,
model.default_event_space_bijector,
model.event_shape,
model.dtype)
plt.figure()
plt.suptitle(str(model)) # 'German Credit Numeric Logistic Regression'
for i, (name, sample_transformation) in enumerate(
model.sample_transformations.items()):
transformed_samples = sample_transformation(samples)
bias_sq = tf.square(
tf.reduce_mean(transformed_samples, 0) -
sample_transformation.ground_truth_mean)
ess = compute_ess( # E.g. tfp.mcmc.effective_sample_size if using MCMC.
transformed_samples,
tf.square(sample_transformation.ground_truth_standard_deviation))
plt.subplot(len(model.sample_transformations), 2, 2 * i + 1)
plt.title('{} bias^2'.format(sample_transformation)) # e.g. 'Identity bias^2'
plt.bar(np.arange(bias_sq.shape[-1]), bias_sq)
plt.subplot(len(model.sample_transformations), 2, 2 * i + 2)
plt.title('{} ess'.format(sample_transformation))
plt.bar(np.arange(ess.shape[-1]), ess)
还可以查看VectorModel
,它可以用来简化推理方法的接口要求。
什么是一个好的推理健身房模型?
一个好的模型理想情况下应该做以下之一或更多
- 帮助建立直觉(通常为1D或2D,以便于可视化)
- 表示贝叶斯推理的一般重要应用
- 提出推理挑战,例如
- 高维性
- 条件差或病态条件
- 混合连续和离散潜在变量
- 多模态
- 非识别性
- 昂贵的梯度
当然,一个模型不应该具有所有这些属性,这样用户就可以更容易地进行实验,以揭示哪些复杂因素对推理过程有什么影响。这不是一个详尽的列表。
进行更改
添加新模型
最简单的方法是模仿现有的例子。以下是一个小表,可以帮助您找到示例。如果您的模型不能很好地用这些可能性来描述,请随时寻求帮助。
贝叶斯模型? | 真实数据集? | 解析地面真相? | Stan实现? | 多个随机变量? | 示例模型 |
---|---|---|---|---|---|
是 | 真实 | 否 | 是 | 是 | GermanCreditNumericSparseLogicRegression |
是 | 真实 | 否 | 是 | 否 | GermanCreditLogicRegression |
是 | 合成 | 否 | 是 | 是 | SyntheticItemResponseTheory |
否 | 无 | 是 | 否 | 否 | IllConditionedGaussian |
表中的贝叶斯模型是指其参数密度通过先验和似然函数的乘积(即使用贝叶斯定理)计算出的模型。这些模型应该继承自BayesianModel
类,因为它为这些模型提供了一些实用工具。
目前我们有一些小工具可以帮助使用cmdstanpy
为没有解析地面真相的模型生成(以正确格式)地面真相值。使用此功能需要在inference_gym/tools/stan
目录中添加模型实现。
新(和现有)模型应遵循模型合同。
添加新的真实数据集
我们强烈建议您首先将数据集添加到TensorFlow Datasets。然后,您可以遵循German Credit (numeric)
数据集的示例,该数据集用于GermanCreditLogicRegression
。
添加新的合成数据集
参考 SyntheticItemResponseTheory
模型的例子。
生成真实值文件。
请参阅 inference_gym/tools/get_ground_truth.py
。
引用 Inference Gym
要引用 Inference Gym
@software{inferencegym2020,
author = {Pavel Sountsov and Alexey Radul and contributors},
title = {Inference Gym},
url = {https://pypi.ac.cn/project/inference_gym},
version = {0.0.4},
year = {2020},
}
请确保更新 version
属性以匹配您实际使用的版本。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源代码发行版
构建发行版
inference_gym-0.0.4-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 6e98bf201f0fb8b3c82a84e398179597fe171af13fcb21bc598c4da8d72bbaf1 |
|
MD5 | a64d5253d97f14a2bab94b36d927d016 |
|
BLAKE2b-256 | 4aa69531d7b35e2e4753108498421410a7205dedbd5dd38541e8b43dee992594 |