Interpretations¶
Interpreter¶
-
exception
PatternMissingError[source]¶ Bases:
NotImplementedError
-
reinterpret(x)[source]¶ Overloaded reinterpretation of a deferred expression.
This handles a limited class of expressions, raising
ValueErrorin unhandled cases.Parameters: x (A funsor or data structure holding funsors.) – An input, typically involving deferred Funsors.Returns: A reinterpreted version of the input. Raises: ValueError
Interpretations¶
-
class
Interpretation(name)[source]¶ Bases:
contextlib.ContextDecorator,abc.ABCAbstract base class for Funsor interpretations.
Instances may be used as context managers or decorators.
Parameters: name (str) – A name used for printing and debugging (required).
-
class
CallableInterpretation(interpret)[source]¶ Bases:
funsor.interpretations.InterpretationA simple callable interpretation.
Example usage:
@CallableInterpretation def my_interpretation(cls, *args): return ...
Parameters: interpret (callable) – A function implementing interpretation.
-
class
DispatchedInterpretation(name='dispatched')[source]¶ Bases:
funsor.interpretations.InterpretationAn interpretation based on pattern matching.
Example usage:
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
StatefulInterpretation(name='stateful')[source]¶ Bases:
funsor.interpretations.InterpretationBase class for interpretations with instance-dependent state or parameters.
Example usage:
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): ...
-
class
Memoize(base_interpretation, cache=None)[source]¶ Bases:
funsor.interpretations.InterpretationExploits cons-hashing to do implicit common subexpression elimination.
Parameters: - base_interpretation (Interpretation) – The interpretation to memoize.
- cache (dict) – An optional temporary cache where results will be memoized.
-
normalize= normalize/reflect¶ Normalize modulo associativity and commutativity, but do not evaluate any numerical operations.
-
lazy= lazy/reflect¶ Performs substitutions eagerly, but construct lazy funsors for everything else.
-
eager= eager/normalize/reflect¶ Eager exact naive interpretation wherever possible.
-
sequential= sequential/eager/normalize/reflect¶ Eagerly execute ops with known implementations; additonally execute vectorized ops sequentially if no known vectorized implementation exists.
Monte Carlo¶
-
class
MonteCarlo(*, rng_key=None, **sample_inputs)[source]¶ Bases:
funsor.interpretations.StatefulInterpretationA Monte Carlo interpretation of
Integrateexpressions. This falls back to the previous interpreter in other cases.Parameters: rng_key –
Preconditioning¶
-
class
Precondition(aux_name='aux')[source]¶ Bases:
funsor.interpretations.StatefulInterpretationPreconditioning interpretation for adjoint computations.
This interpretation is intended to be used once, followed by a call to
combine_subs()as follows:# 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() }
See
forward_filter_backward_precondition()for complete usage.Parameters: aux_name (str) – Name of the auxiliary variable containing white noise. -
combine_subs()[source]¶ Method to create a combining substitution after preconditioning is complete. The returned substitution replaces per-factor auxiliary variables with slices into a single combined auxiliary variable.
Returns: A substitution indexing each factor-wise auxiliary variable into a single global auxiliary variable. Return type: dict
-
Approximations¶
-
argmax_approximate= argmax_approximate¶ Point-approximate at the argmax of the provided guide.
-
mean_approximate= mean_approximate¶ Point-approximate at the mean of the provided guide.
-
laplace_approximate= laplace_approximate¶ Gaussian approximate using the value and Hessian of the model, evaluated at the mode of the guide.
Evidence lower bound¶
-
class
Elbo(guide, approx_vars)[source]¶ Bases:
funsor.interpretations.StatefulInterpretationGiven an approximating
guidefunsor, approximates:model.reduce(ops.logaddexp, approx_vars)
by the lower bound:
Integrate(guide, model - guide, approx_vars)
Parameters: