解释¶
解释器¶
解释¶
- class CallableInterpretation(interpret)[source]¶
基类:
Interpretation
一个简单的可调用解释。
示例用法
@CallableInterpretation def my_interpretation(cls, *args): return ...
- 参数
interpret (callable) – 实现解释的函数。
- 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。
- normalize = normalize/reflect¶
根据结合律和交换律进行规范化,但不评估任何数值运算。
- sequential = sequential/eager/normalize/reflect¶
急切地执行已知实现的运算;如果不存在已知的向量化实现,则额外地顺序执行向量化运算。
蒙特卡洛¶
预处理¶
- class Precondition(aux_name='aux')[source]¶
-
用于伴随计算的预处理解释。
此解释旨在使用一次,然后按照如下方式调用
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) – 包含白噪声的辅助变量的名称。
近似¶
- 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。
- laplace_approximate = laplace_approximate¶
使用模型的数值和 Hessian 进行高斯近似,在 guide 的峰值处评估。
- mean_approximate = mean_approximate¶
在提供的 guide 的均值处进行点近似。