class_docstr: `RandomVariable` supports random variable semantics for TFP distributions.
  
  The `RandomVariable` class memoizes concretizations of TFP distribution-like
  objects so that random draws can be re-triggered on-demand, i.e., by calling
  `reset`. For more details type `help(tfp.util.DeferredTensor)`.
  
  #### Examples
  
  ```python
  # In this example we see the memoization semantics in action.
  tfd = tfp.distributions
  tfn = tfp.experimental.nn
  x = tfn.util.RandomVariable(tfd.Normal(0, 1))
  x_ = tf.convert_to_tensor(x)
  x _ + 1. == x + 1.
  # ==> True; `x` always has the same value until reset.
  x.reset()
  tf.convert_to_tensor(x) == x_
  # ==> False; `x` was reset which triggers a new sample.
  ```
  
  ```python
  # In this example we see how to concretize with different semantics.
  tfd = tfp.distributions
  tfn = tfp.experimental.nn
  x = tfn.util.RandomVariable(
      tfd.Bernoulli(probs=[[0.25], [0.5]]),
      convert_to_tensor_fn=tfd.Distribution.mean,
      dtype=tf.float32,
      shape=[2, 1],
      name='x')
  tf.convert_to_tensor(x)
  # ==> [[0.25], [0.5]]
  x.shape
  # ==> [2, 1]
  x.dtype
  # ==> tf.float32
  x.name
  # ==> 'x'
  ```
  
  ```python
  # In this example we see a common pitfall: accessing the memoized value from a
  # different graph context.
  tfd = tfp.distributions
  tfn = tfp.experimental.nn
  x = tfn.util.RandomVariable(tfd.Normal(0, 1))
  @tf.function(autograph=False, jit_compile=True)
  def run():
    return tf.convert_to_tensor(x)
  first = run()
  second = tf.convert_to_tensor(x)
  # raises ValueError:
  #   "You are attempting to access a memoized value from a different
  #   graph context. Please call `this.reset()` before accessing a
  #   memoized value from a different graph context."
  x.reset()
  third = tf.convert_to_tensor(x)
  # ==> No exception.
  first == third
  # ==> False
  ```
class_docstr_tok: ["`","RandomVariable","`","supports","random","variable","semantics","for","TFP","distributions",".","The","`","RandomVariable","`","class","memoizes","concretizations","of","TFP","distribution-like","objects","so","that","random","draws","can","be","re-triggered","on-demand",",","i.e.",",","by","calling","`","reset","`",".","For","more","details","type","`","help","(","tfp.util.DeferredTensor",")","`",".","#","#","#","#","Examples","``","`","python","#","In","this","example","we","see","the","memoization","semantics","in","action",".","tfd","=","tfp.distributions","tfn","=","tfp.experimental.nn","x","=","tfn.util.RandomVariable","(","tfd.Normal","(","0",",","1",")",")","x_","=","tf.convert_to_tensor","(","x",")","x","_","+","1",".","==","x","+","1",".","#","==",">","True",";","`","x","`","always","has","the","same","value","until","reset",".","x.reset","(",")","tf.convert_to_tensor","(","x",")","==","x_","#","==",">","False",";","`","x","`","was","reset","which","triggers","a","new","sample",".","``","`","``","`","python","#","In","this","example","we","see","how","to","concretize","with","different","semantics",".","tfd","=","tfp.distributions","tfn","=","tfp.experimental.nn","x","=","tfn.util.RandomVariable","(","tfd.Bernoulli","(","probs=","[","[","0.25","]",",","[","0.5","]","]",")",",","convert_to_tensor_fn=tfd.Distribution.mean",",","dtype=tf.float32",",","shape=","[","2",",","1","]",",","name=","'","x","'",")","tf.convert_to_tensor","(","x",")","#","==",">","[","[","0.25","]",",","[","0.5","]","]","x.shape","#","==",">","[","2",",","1","]","x.dtype","#","==",">","tf.float32","x.name","#","==",">","'","x'","``","`","``","`","python","#","In","this","example","we","see","a","common","pitfall",":","accessing","the","memoized","value","from","a","#","different","graph","context",".","tfd","=","tfp.distributions","tfn","=","tfp.experimental.nn","x","=","tfn.util.RandomVariable","(","tfd.Normal","(","0",",","1",")",")","@","tf.function","(","autograph=False",",","jit_compile=True",")","def","run","(",")",":","return","tf.convert_to_tensor","(","x",")","first","=","run","(",")","second","=","tf.convert_to_tensor","(","x",")","#","raises","ValueError",":","#","``","You","are","attempting","to","access","a","memoized","value","from","a","different","#","graph","context",".","Please","call","`","this.reset","(",")","`","before","accessing","a","#","memoized","value","from","a","different","graph","context",".","''","x.reset","(",")","third","=","tf.convert_to_tensor","(","x",")","#","==",">","No","exception",".","first","==","third","#","==",">","False","``","`"]
docstr: Creates the `RandomVariable` object.
  
  Args:
    distribution: TFP distribution-like object which is passed into the
      `convert_to_tensor_fn` whenever this object is evaluated in
      `Tensor`-like contexts.
    convert_to_tensor_fn: Python `callable` which takes one argument, the
      `distribution` and returns a `Tensor` of type `dtype` and shape `shape`.
      Default value: `tfp.distributions.Distribution.sample`.
    dtype: TF `dtype` equivalent to what would otherwise be
      `convert_to_tensor_fn(distribution).dtype`.
      Default value: `None` (i.e., `distribution.dtype`).
    shape: `tf.TensorShape`-like object compatible with what would otherwise
      be `convert_to_tensor_fn(distribution).shape`.
      Default value: `'None'` (i.e., unspecified static shape).
    name: Python `str` representing this object's `name`; used only in graph
      mode.
      Default value: `None` (i.e., `distribution.name`)
docstr_tok: ["Creates","the","`","RandomVariable","`","object",".","Args",":","distribution",":","TFP","distribution-like","object","which","is","passed","into","the","`","convert_to_tensor_fn","`","whenever","this","object","is","evaluated","in","`","Tensor","`","-like","contexts",".","convert_to_tensor_fn",":","Python","`","callable","`","which","takes","one","argument",",","the","`","distribution","`","and","returns","a","`","Tensor","`","of","type","`","dtype","`","and","shape","`","shape","`",".","Default","value",":","`","tfp.distributions.Distribution.sample","`",".","dtype",":","TF","`","dtype","`","equivalent","to","what","would","otherwise","be","`","convert_to_tensor_fn","(","distribution",")",".dtype","`",".","Default","value",":","`","None","`","(","i.e.",",","`","distribution.dtype","`",")",".","shape",":","`","tf.TensorShape","`","-like","object","compatible","with","what","would","otherwise","be","`","convert_to_tensor_fn","(","distribution",")",".shape","`",".","Default","value",":","`","'None","'","`","(","i.e.",",","unspecified","static","shape",")",".","name",":","Python","`","str","`","representing","this","object","'s","`","name","`",";","used","only","in","graph","mode",".","Default","value",":","`","None","`","(","i.e.",",","`","distribution.name","`",")"]
code: def __init__(
      self,
      distribution,
      convert_to_tensor_fn=tfd.Distribution.sample,
      dtype=None,
      shape=None,
      name=None,
  ):
      """Creates the `RandomVariable` object.
  
      Args:
        distribution: TFP distribution-like object which is passed into the
          `convert_to_tensor_fn` whenever this object is evaluated in
          `Tensor`-like contexts.
        convert_to_tensor_fn: Python `callable` which takes one argument, the
          `distribution` and returns a `Tensor` of type `dtype` and shape `shape`.
          Default value: `tfp.distributions.Distribution.sample`.
        dtype: TF `dtype` equivalent to what would otherwise be
          `convert_to_tensor_fn(distribution).dtype`.
          Default value: `None` (i.e., `distribution.dtype`).
        shape: `tf.TensorShape`-like object compatible with what would otherwise
          be `convert_to_tensor_fn(distribution).shape`.
          Default value: `'None'` (i.e., unspecified static shape).
        name: Python `str` representing this object's `name`; used only in graph
          mode.
          Default value: `None` (i.e., `distribution.name`)
      """
  
      self._distribution = distribution
      self._convert_to_tensor_fn = convert_to_tensor_fn
      super(RandomVariable, self).__init__(
          tf.constant([], tf.bool),  # Dummy.
          CallOnce(lambda _: convert_to_tensor_fn(distribution)),
          shape=shape,
          dtype=dtype or distribution.dtype,
          name=name or distribution.name,
      )
  
imports: import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import distribution
  from tensorflow_probability.python.util.deferred_tensor import DeferredTensor
label_desc: Use image node_id 1 to create a new RandomVariable object from inherited base classes: DeferredTensor with example: obj = RandomVariable(distribution, convert_to_tensor_fn, dtype, shape, name)