View source on GitHub

Python context manager for interception.

Upon entry, an interception context manager pushes an interceptor onto a thread-local stack. Upon exiting, it pops the interceptor from the stack.

interceptor Function which takes a callable f and inputs *args, **kwargs.




Interception controls the execution of Edward programs. Below we illustrate how to set the value of a specific random variable within a program.

from tensorflow_probability import edward2 as ed

def model():
  return ed.Poisson(rate=1.5, name="y")

def interceptor(f, *args, **kwargs):
  if kwargs.get("name") == "y":
    kwargs["value"] = 42
  return interceptable(f)(*args, **kwargs)

with ed.interception(interceptor):
  y = model()

with tf.Session() as sess:
  assert == 42

Wrapping f as interceptable allows interceptors down the stack to additionally modify this operation. Since the operation f() is not wrapped by default, we could have called it directly. Refer also to the example in get_next_interceptor() for more details on nested interceptors.