解释

解释器

exception PatternMissingError[source]

基类: NotImplementedError

interpretation(new)[source]
pop_interpretation()[source]
push_interpretation(new)[source]
reinterpret(x)[source]

对延迟表达式进行重载解释。

这处理有限类别的表达式,并在未处理的情况下引发 ValueError

参数

x (一个 funsor包含 funsor 的数据结构。) – 输入,通常涉及延迟的 Funsor 对象。

返回值

输入的重解释版本。

引发异常

ValueError

解释

class CallableInterpretation(interpret)[source]

基类: Interpretation

一个简单的可调用解释。

示例用法

@CallableInterpretation
def my_interpretation(cls, *args):
    return ...
参数

interpret (callable) – 实现解释的函数。

set_callable(interpret)[source]

重置可调用属性 .interpret

class DispatchedInterpretation(name='dispatched')[source]

基类: Interpretation

基于模式匹配的解释。

示例用法

my_interpretation = DispatchedInterpretation("my_interpretation")

# Register a funsor pattern and rule.
@my_interpretation.register(...)
def my_impl(cls, *args):
    ...

# Use the new interpretation.
with my_interpretation:
    ...
class Interpretation(name)[source]

基类: ContextDecorator, ABC

Funsor 解释的抽象基类。

实例可用作上下文管理器或装饰器。

参数

name (str) – 用于打印和调试的名称(必需)。

class Memoize(base_interpretation, cache=None)[source]

基类: Interpretation

利用 cons-hashing 进行隐式的公共子表达式消除。

参数
  • base_interpretation (Interpretation) – 需要进行备忘录化的解释。

  • cache (dict) – 一个可选的临时缓存,结果将被存储在此。

class StatefulInterpretation(name='stateful')[source]

基类: Interpretation

具有实例依赖状态或参数的解释的基类。

示例用法

class MyInterpretation(StatefulInterpretation):

    def __init__(self, my_param):
        self.my_param = my_param

@MyInterpretation.register(...)
def my_impl(interpretation_state, cls, *args):
    my_param = interpretation_state.my_param
    ...

with MyInterpretation(my_param=0.1):
    ...
eager = eager/normalize/reflect

在可能的情况下,进行急切的、精确的、朴素的解释。

lazy = lazy/reflect

急切地执行替换,但为其他所有内容构建惰性 funsor。

memoize(cache=None)[source]

包装 Memoize 并产生 cache 字典的上下文管理器。

moment_matching = moment_matching/eager/normalize/reflect

Reduce 表达式的矩匹配解释。在其他情况下,这会回退到 eager 解释。

normalize = normalize/reflect

根据结合律和交换律进行规范化,但不评估任何数值运算。

sequential = sequential/eager/normalize/reflect

急切地执行已知实现的运算;如果不存在已知的向量化实现,则额外地顺序执行向量化运算。

蒙特卡洛

class MonteCarlo(*, rng_key=None, **sample_inputs)[source]

基类: StatefulInterpretation

Integrate 表达式的蒙特卡洛解释。在其他情况下,这会回退到之前的解释器。

参数

rng_key

预处理

class Precondition(aux_name='aux')[source]

基类: StatefulInterpretation

用于伴随计算的预处理解释。

此解释旨在使用一次,然后按照如下方式调用 combine_subs()

# Lazily build a factor graph.
with reflect:
    log_joint = Gaussian(...) + ... + Gaussian(...)
    log_Z = log_joint.reduce(ops.logaddexp)

# Run a backward sampling under the precondition interpretation.
with Precondition() as p:
    marginals = adjoint(
        ops.logaddexp, ops.add, log_Z, batch_vars=p.sample_vars
    )
combine_subs = p.combine_subs()

# Extract samples from Delta distributions.
samples = {
    k: v(**combine_subs)
    for name, delta in marginals.items()
    for k, v in funsor.montecarlo.extract_samples(delta).items()
}

有关完整用法,请参阅 forward_filter_backward_precondition()

参数

aux_name (str) – 包含白噪声的辅助变量的名称。

combine_subs()[source]

在预处理完成后创建合并替换的方法。返回的替换将每个因子辅助变量替换为对单个合并辅助变量的切片。

返回值

将每个因子辅助变量索引到单个全局辅助变量中的替换。

返回类型

dict

近似

argmax_approximate = argmax_approximate

在提供的 guide 的 argmax 处进行点近似。

compute_argmax(model, approx_vars)[source]
compute_argmax(model: Tensor, approx_vars)
compute_argmax(model: Gaussian, approx_vars)
compute_argmax(model: Contraction[Union[LogaddexpOp, NullOp], AddOp, frozenset, Tuple[Union[Tensor, Number], Gaussian]], approx_vars)
compute_argmax(model: Contraction[NullOp, AddOp, frozenset, tuple], approx_vars)

计算 funsor 的 argmax。

参数
  • model (Funsor) – 被近似变量的函数。

  • approx_vars (frozenset) – 需要最大化的 Variable 对象的 frozenset。

返回值

一个字典,映射名称 (str) 到点估计 (Funsor),对应于 approx_vars 中的每个变量名。

返回类型

str

laplace_approximate = laplace_approximate

使用模型的数值和 Hessian 进行高斯近似,在 guide 的峰值处评估。

mean_approximate = mean_approximate

在提供的 guide 的均值处进行点近似。

证据下界

class Elbo(guide, approx_vars)[source]

基类: StatefulInterpretation

给定一个近似 guide funsor,近似计算

model.reduce(ops.logaddexp, approx_vars)

使用下界

Integrate(guide, model - guide, approx_vars)
参数
  • guide (Funsor) – 一个 guide 或 proposal funsor。

  • approx_vars (frozenset) – 被积分的变量。