Apply to speak at TensorFlow World. Deadline April 23rd. Propose talk

tfp.edward2.as_random_variable

tfp.edward2.as_random_variable(
    distribution,
    sample_shape=(),
    value=None
)

Wrap an existing distribution as a traceable random variable.

This enables the use of custom or user-provided distributions in Edward models. Unlike a bare RandomVariable object, this method wraps the constructor so it is included in the Edward trace and its values can be properly intercepted and overridden.

Where possible, you should prefer the built-in constructors (ed.Normal, etc); these simultaneously construct a Distribution and a RandomVariable object so that the distribution parameters themselves may be intercepted and overridden. RVs constructed via as_random_variable() have a fixed distribution and may not support program transformations (e.g, conjugate marginalization) that rely on overriding distribution parameters.

Args:

  • distribution: tfd.Distribution governing the distribution of the random variable, such as sampling and log-probabilities.
  • sample_shape: tf.TensorShape of samples to draw from the random variable. Default is () corresponding to a single sample.
  • value: Fixed tf.Tensor to associate with random variable. Must have shape sample_shape + distribution.batch_shape + distribution.event_shape. Default is to sample from random variable according to sample_shape.

Returns:

  • rv: a RandomVariable wrapping the provided distribution.

Example

from tensorflow_probability import distributions as tfd
from tensorflow_probability import edward2 as ed

def model():
  # equivalent to ed.Normal(0., 1., name='x')
  return ed.as_random_variable(tfd.Normal(0., 1., name='x'))

log_joint = ed.make_log_joint_fn(model)
output = log_joint(x=2.)