Example #1
0
def TransformedRandomVariable(random_variable,  # pylint: disable=invalid-name
                              reversible_layer,
                              name=None,
                              sample_shape=(),
                              value=None):
  """Random variable for f(x), where x ~ p(x) and f is reversible."""
  return ed.RandomVariable(
      distribution=TransformedDistribution(random_variable.distribution,
                                           reversible_layer,
                                           name=name),
      sample_shape=sample_shape,
      value=value)
 def testGradientsSecondOrder(self):
     f = lambda x: 2 * (x**2)
     x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
     y = f(x)
     if tfe.in_eager_mode():
         df = tfe.gradients_function(f)
         d2f = tfe.gradients_function(lambda x: df(x)[0])
         (z, ) = d2f(x)
     else:
         (z, ) = tf.gradients(y, x)
         (z, ) = tf.gradients(z, x)
     self.assertEqual(self.evaluate(z), 4.0)
Example #3
0
 def func(*args, **kwargs):
     # pylint: disable=g-doc-args
     """Create a random variable for ${cls}.
     See ${cls} for more details.
     Returns:
       RandomVariable.
     #### Original Docstring for Distribution
     ${doc}
     """
     # pylint: enable=g-doc-args
     sample_shape = kwargs.pop('sample_shape', ())
     value = kwargs.pop('value', None)
     return ed.RandomVariable(distribution=distribution_obj,
                              sample_shape=sample_shape,
                              value=value)
 def testShapeRandomVariable(self):
     self._testShape(ed.RandomVariable(tfd.Bernoulli(probs=0.5)), [], [],
                     [])
     self._testShape(ed.RandomVariable(tfd.Bernoulli(tf.zeros([2, 3]))), [],
                     [2, 3], [])
     self._testShape(
         ed.RandomVariable(tfd.Bernoulli(probs=0.5), sample_shape=2), [2],
         [], [])
     self._testShape(
         ed.RandomVariable(tfd.Bernoulli(probs=0.5), sample_shape=[2, 1]),
         [2, 1], [], [])
     self._testShape(
         ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                           sample_shape=tf.constant([2])), [2], [], [])
     self._testShape(
         ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                           sample_shape=tf.constant([2, 4])), [2, 4], [],
         [])
 def testConvertToTensor(self):
   x = ed.RandomVariable(tfd.Normal(0.0, 0.1))
   with self.assertRaises(ValueError):
     _ = tf.convert_to_tensor(value=x, dtype=tf.int32)
 def testOperatorsBoolNonzero(self):
   x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
   with self.assertRaises(TypeError):
     _ = not x
 def testOperatorsNe(self):
   x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
   y = 5.0
   self.assertNotEqual(x, y)
 def testOperatorsEq(self):
   x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
   self.assertEqual(x, x)
 def testOperatorsHash(self):
   x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
   y = 5.0
   self.assertNotEqual(hash(x), hash(y))
   self.assertEqual(hash(x), id(x))
 def testOperatorsBoolNonzero(self):
     x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
     with self.assertRaises(TypeError):
         not x  # pylint: disable=pointless-statement
 def testGradientsFirstOrder(self):
   f = lambda x: 2. * x
   x = ed.RandomVariable(tfd.Normal(0., 1.))
   _, dydx = tfp.math.value_and_gradient(f, x)
   self.assertEqual(self.evaluate(dydx), 2.)
 def testOperatorsGetitem(self):
   x = ed.RandomVariable(tfd.Normal(tf.zeros([3, 4]), tf.ones([3, 4])))
   z = x[0:2, 2:3]
   z_value = x.value[0:2, 2:3]
   z_eval, z_value_eval = self.evaluate([z, z_value])
   self.assertAllEqual(z_eval, z_value_eval)
Example #13
0
def Input(name, shape, dtype=None):
    return ed.RandomVariable(InputDistribution(name=name, shape=shape, dtype=dtype))
class RandomVariableTest(parameterized.TestCase, tf.test.TestCase):
    @tfe.run_test_in_graph_and_eager_modes
    def testConstructor(self):
        x = ed.RandomVariable(tfd.Poisson(rate=tf.ones([2, 5])),
                              value=tf.ones([2, 5]))
        x_sample, x_value = self.evaluate([tf.convert_to_tensor(x), x.value])
        self.assertAllEqual(x_sample, x_value)
        with self.assertRaises(ValueError):
            _ = ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                                  value=tf.zeros([2, 5], dtype=tf.int32))
        x = ed.RandomVariable(FakeDistribution())
        with self.assertRaises(NotImplementedError):
            _ = x.value

    @tfe.run_test_in_graph_and_eager_modes
    def testGradientsFirstOrder(self):
        f = lambda x: 2. * x
        x = ed.RandomVariable(tfd.Normal(0., 1.))
        y = f(x)
        if tfe.in_eager_mode():
            df = tfe.gradients_function(f)
            (z, ) = df(x)
        else:
            (z, ) = tf.gradients(y, x)
        self.assertEqual(self.evaluate(z), 2.)

    @tfe.run_test_in_graph_and_eager_modes
    def testGradientsSecondOrder(self):
        f = lambda x: 2 * (x**2)
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = f(x)
        if tfe.in_eager_mode():
            df = tfe.gradients_function(f)
            d2f = tfe.gradients_function(lambda x: df(x)[0])
            (z, ) = d2f(x)
        else:
            (z, ) = tf.gradients(y, x)
            (z, ) = tf.gradients(z, x)
        self.assertEqual(self.evaluate(z), 4.0)

    @tfe.run_test_in_graph_and_eager_modes
    def testStr(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0), value=1.234)
        if tfe.in_eager_mode():
            pattern = "RandomVariable(\"1.234\", shape=(), dtype=float32"
        else:
            pattern = "RandomVariable(\"Normal/\", shape=(), dtype=float32"
        regexp = re.escape(pattern)
        self.assertRegexpMatches(str(x), regexp)

    @tfe.run_test_in_graph_and_eager_modes
    def testRepr(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0), value=1.234)
        if tfe.in_eager_mode():
            string = ("<ed.RandomVariable 'Normal/' shape=() "
                      "dtype=float32 numpy=1.234>")
        else:
            string = "<ed.RandomVariable 'Normal/' shape=() dtype=float32>"
        self.assertEqual(repr(x), string)

    @tfe.run_test_in_graph_and_eager_modes
    def testNumpy(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0), value=1.23)
        if tfe.in_eager_mode():
            self.assertEqual(x.numpy(), tf.constant(1.23).numpy())
        else:
            with self.assertRaises(NotImplementedError):
                _ = x.numpy()

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsAdd(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x + y
        z_value = x.value + y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsRadd(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = y + x
        z_value = y + x.value
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsSub(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x - y
        z_value = x.value - y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsRsub(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = y - x
        z_value = y - x.value
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsMul(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x * y
        z_value = x.value * y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsRmul(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = y * x
        z_value = y * x.value
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsDiv(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x / y
        z_value = x.value / y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsRdiv(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = y / x
        z_value = y / x.value
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsFloordiv(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x // y
        z_value = x.value // y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsRfloordiv(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = y // x
        z_value = y // x.value
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsMod(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x % y
        z_value = x.value % y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsRmod(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = y % x
        z_value = y % x.value
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsLt(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x < y
        z_value = x.value < y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsLe(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x <= y
        z_value = x.value <= y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsGt(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x > y
        z_value = x.value > y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsGe(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x >= y
        z_value = x.value >= y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsGetitem(self):
        x = ed.RandomVariable(tfd.Normal(tf.zeros([3, 4]), tf.ones([3, 4])))
        z = x[0:2, 2:3]
        z_value = x.value[0:2, 2:3]
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsPow(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = x**y
        z_value = x.value**y
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsRpow(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        z = y**x
        z_value = y**x.value
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsNeg(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        z = -x
        z_value = -x.value
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsAbs(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        z = abs(x)
        z_value = abs(x.value)
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsHash(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        self.assertNotEqual(hash(x), hash(y))
        self.assertEqual(hash(x), id(x))

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsEq(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        self.assertEqual(x, x)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsNe(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = 5.0
        self.assertNotEqual(x, y)

    @tfe.run_test_in_graph_and_eager_modes
    def testOperatorsBoolNonzero(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        with self.assertRaises(TypeError):
            _ = not x

    @tfe.run_test_in_graph_and_eager_modes
    def testArrayPriority(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
        y = np.array(5.0, dtype=np.float32)
        z = y / x
        z_value = y / x.value
        z_eval, z_value_eval = self.evaluate([z, z_value])
        self.assertAllEqual(z_eval, z_value_eval)

    @tfe.run_test_in_graph_and_eager_modes
    def testConvertToTensor(self):
        x = ed.RandomVariable(tfd.Normal(0.0, 0.1))
        with self.assertRaises(ValueError):
            _ = tf.convert_to_tensor(x, dtype=tf.int32)

    def testSessionEval(self):
        with self.test_session() as sess:
            x = ed.RandomVariable(tfd.Normal(0.0, 0.1))
            x_ph = tf.placeholder(tf.float32, [])
            y = ed.RandomVariable(tfd.Normal(x_ph, 0.1))
            self.assertLess(x.eval(), 5.0)
            self.assertLess(x.eval(sess), 5.0)
            self.assertLess(x.eval(feed_dict={x_ph: 100.0}), 5.0)
            self.assertGreater(y.eval(feed_dict={x_ph: 100.0}), 5.0)
            self.assertGreater(y.eval(sess, feed_dict={x_ph: 100.0}), 5.0)
            self.assertRaises(tf.errors.InvalidArgumentError, y.eval)
            self.assertRaises(tf.errors.InvalidArgumentError, y.eval, sess)

    def testSessionRun(self):
        with self.test_session() as sess:
            x = ed.RandomVariable(tfd.Normal(0.0, 0.1))
            x_ph = tf.placeholder(tf.float32, [])
            y = ed.RandomVariable(tfd.Normal(x_ph, 0.1))
            self.assertLess(sess.run(x), 5.0)
            self.assertLess(sess.run(x, feed_dict={x_ph: 100.0}), 5.0)
            self.assertGreater(sess.run(y, feed_dict={x_ph: 100.0}), 5.0)
            self.assertRaises(tf.errors.InvalidArgumentError, sess.run, y)

    @parameterized.parameters(
        {
            "rv": ed.RandomVariable(tfd.Bernoulli(probs=0.5)),
            "sample_shape": [],
            "batch_shape": [],
            "event_shape": []
        },
        {
            "rv": ed.RandomVariable(tfd.Bernoulli(tf.zeros([2, 3]))),
            "sample_shape": [],
            "batch_shape": [2, 3],
            "event_shape": []
        },
        {
            "rv": ed.RandomVariable(tfd.Bernoulli(probs=0.5), sample_shape=2),
            "sample_shape": [2],
            "batch_shape": [],
            "event_shape": []
        },
        {
            "rv": ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                                    sample_shape=[2, 1]),
            "sample_shape": [2, 1],
            "batch_shape": [],
            "event_shape": []
        },
        {
            "rv":
            ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                              sample_shape=tf.constant([2])),
            "sample_shape": [2],
            "batch_shape": [],
            "event_shape": []
        },
        {
            "rv":
            ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                              sample_shape=tf.constant([2, 4])),
            "sample_shape": [2, 4],
            "batch_shape": [],
            "event_shape": []
        },
    )
    @tfe.run_test_in_graph_and_eager_modes
    def testShape(self, rv, sample_shape, batch_shape, event_shape):
        self.assertEqual(rv.shape, sample_shape + batch_shape + event_shape)
        self.assertEqual(rv.shape, rv.get_shape())
        self.assertEqual(rv.sample_shape, sample_shape)
        self.assertEqual(rv.distribution.batch_shape, batch_shape)
        self.assertEqual(rv.distribution.event_shape, event_shape)

    @tfe.run_test_in_graph_and_eager_modes
    def testRandomTensorSample(self):
        num_samples = tf.cast(tfd.Poisson(rate=5.).sample(), tf.int32)
        _ = ed.RandomVariable(tfd.Normal(loc=0.0, scale=1.0),
                              sample_shape=num_samples)
 def testConvertToTensor(self):
     x = ed.RandomVariable(tfd.Normal(0.0, 0.1))
     with self.assertRaises(ValueError):
         tf.convert_to_tensor(x, dtype=tf.int32)  # pylint: disable=pointless-statement
 def testRandomTensorSample(self):
   num_samples = tf.cast(tfd.Poisson(rate=5.).sample(), tf.int32)
   _ = ed.RandomVariable(tfd.Normal(loc=0.0, scale=1.0),
                         sample_shape=num_samples)
class RandomVariableTest(parameterized.TestCase, tf.test.TestCase):

  def testConstructor(self):
    x = ed.RandomVariable(tfd.Poisson(rate=tf.ones([2, 5])),
                          value=tf.ones([2, 5]))
    x_sample, x_value = self.evaluate([tf.convert_to_tensor(value=x), x.value])
    self.assertAllEqual(x_sample, x_value)
    with self.assertRaises(ValueError):
      _ = ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                            value=tf.zeros([2, 5], dtype=tf.int32))
    x = ed.RandomVariable(FakeDistribution())
    with self.assertRaises(NotImplementedError):
      _ = x.value

  def testGradientsFirstOrder(self):
    f = lambda x: 2. * x
    x = ed.RandomVariable(tfd.Normal(0., 1.))
    _, dydx = tfp.math.value_and_gradient(f, x)
    self.assertEqual(self.evaluate(dydx), 2.)

  def testGradientsSecondOrder(self):
    f = lambda x: 2. * x**2.
    df = lambda x: tfp.math.value_and_gradient(f, x)[1]
    x = ed.RandomVariable(tfd.Normal(0., 1.))
    _, d2ydx2 = tfp.math.value_and_gradient(df, x)
    self.assertEqual(self.evaluate(d2ydx2), 4.)

  def testStr(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0), value=1.234)
    if tf.executing_eagerly():
      pattern = "RandomVariable(\"1.234\", shape=(), dtype=float32"
    else:
      pattern = "RandomVariable(\"Normal/\", shape=(), dtype=float32"
    regexp = re.escape(pattern)
    self.assertRegexpMatches(str(x), regexp)

  def testRepr(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0), value=1.234)
    if tf.executing_eagerly():
      string = ("<ed.RandomVariable 'Normal/' shape=() "
                "dtype=float32 numpy=1.234>")
    else:
      string = "<ed.RandomVariable 'Normal/' shape=() dtype=float32>"
    self.assertEqual(repr(x), string)

  def testNumpy(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0), value=1.23)
    if tf.executing_eagerly():
      self.assertEqual(x.numpy(), tf.constant(1.23).numpy())
    else:
      with self.assertRaises(NotImplementedError):
        _ = x.numpy()

  def testOperatorsAdd(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x + y
    z_value = x.value + y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsRadd(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = y + x
    z_value = y + x.value
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsSub(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x - y
    z_value = x.value - y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsRsub(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = y - x
    z_value = y - x.value
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsMul(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x * y
    z_value = x.value * y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsRmul(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = y * x
    z_value = y * x.value
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsDiv(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x / y
    z_value = x.value / y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsRdiv(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = y / x
    z_value = y / x.value
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsFloordiv(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x // y
    z_value = x.value // y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsRfloordiv(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = y // x
    z_value = y // x.value
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsMod(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x % y
    z_value = x.value % y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsRmod(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = y % x
    z_value = y % x.value
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsLt(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x < y
    z_value = x.value < y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsLe(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x <= y
    z_value = x.value <= y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsGt(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x > y
    z_value = x.value > y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsGe(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x >= y
    z_value = x.value >= y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsGetitem(self):
    x = ed.RandomVariable(tfd.Normal(tf.zeros([3, 4]), tf.ones([3, 4])))
    z = x[0:2, 2:3]
    z_value = x.value[0:2, 2:3]
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsPow(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = x ** y
    z_value = x.value ** y
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsRpow(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    z = y ** x
    z_value = y ** x.value
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsNeg(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    z = -x
    z_value = -x.value
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsAbs(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    z = abs(x)
    z_value = abs(x.value)
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testOperatorsHash(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    self.assertNotEqual(hash(x), hash(y))
    self.assertEqual(hash(x), id(x))

  def testOperatorsEq(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    self.assertEqual(x, x)

  def testOperatorsNe(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = 5.0
    self.assertNotEqual(x, y)

  def testOperatorsBoolNonzero(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    with self.assertRaises(TypeError):
      _ = not x

  def testArrayPriority(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
    y = np.array(5.0, dtype=np.float32)
    z = y / x
    z_value = y / x.value
    z_eval, z_value_eval = self.evaluate([z, z_value])
    self.assertAllEqual(z_eval, z_value_eval)

  def testConvertToTensor(self):
    x = ed.RandomVariable(tfd.Normal(0.0, 0.1))
    with self.assertRaises(ValueError):
      _ = tf.convert_to_tensor(value=x, dtype=tf.int32)

  def testSessionEval(self):
    if tf.executing_eagerly(): return
    with self.cached_session() as sess:
      x = ed.RandomVariable(tfd.Normal(0.0, 0.1))
      x_ph = tf.compat.v1.placeholder(tf.float32, [])
      y = ed.RandomVariable(tfd.Normal(x_ph, 0.1))
      self.assertLess(x.eval(), 5.0)
      self.assertLess(x.eval(sess), 5.0)
      self.assertLess(x.eval(feed_dict={x_ph: 100.0}), 5.0)
      self.assertGreater(y.eval(feed_dict={x_ph: 100.0}), 5.0)
      self.assertGreater(y.eval(sess, feed_dict={x_ph: 100.0}), 5.0)
      self.assertRaises(tf.errors.InvalidArgumentError, y.eval)
      self.assertRaises(tf.errors.InvalidArgumentError, y.eval, sess)

  def testSessionRun(self):
    if tf.executing_eagerly(): return
    with self.cached_session() as sess:
      x = ed.RandomVariable(tfd.Normal(0.0, 0.1))
      x_ph = tf.compat.v1.placeholder(tf.float32, [])
      y = ed.RandomVariable(tfd.Normal(x_ph, 0.1))
      self.assertLess(sess.run(x), 5.0)
      self.assertLess(sess.run(x, feed_dict={x_ph: 100.0}), 5.0)
      self.assertGreater(sess.run(y, feed_dict={x_ph: 100.0}), 5.0)
      self.assertRaises(tf.errors.InvalidArgumentError, sess.run, y)

  # Note: we must defer creation of any tensors until after tf.test.main().
  # pylint: disable=g-long-lambda
  @parameterized.parameters(
      {"rv": lambda: ed.RandomVariable(tfd.Bernoulli(probs=0.5)),
       "sample_shape": [],
       "batch_shape": [],
       "event_shape": []},
      {"rv": lambda: ed.RandomVariable(tfd.Bernoulli(tf.zeros([2, 3]))),
       "sample_shape": [],
       "batch_shape": [2, 3],
       "event_shape": []},
      {"rv": lambda: ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                                       sample_shape=2),
       "sample_shape": [2],
       "batch_shape": [],
       "event_shape": []},
      {"rv": lambda: ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                                       sample_shape=[2, 1]),
       "sample_shape": [2, 1],
       "batch_shape": [],
       "event_shape": []},
      {"rv": lambda: ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                                       sample_shape=tf.constant([2])),
       "sample_shape": [2],
       "batch_shape": [],
       "event_shape": []},
      {"rv": lambda: ed.RandomVariable(tfd.Bernoulli(probs=0.5),
                                       sample_shape=tf.constant([2, 4])),
       "sample_shape": [2, 4],
       "batch_shape": [],
       "event_shape": []},
  )
  # pylint: enable=g-long-lambda
  def testShape(self, rv, sample_shape, batch_shape, event_shape):
    rv = rv()
    self.assertEqual(rv.shape, sample_shape + batch_shape + event_shape)
    self.assertEqual(rv.shape, rv.shape)
    self.assertEqual(rv.sample_shape, sample_shape)
    self.assertEqual(rv.distribution.batch_shape, batch_shape)
    self.assertEqual(rv.distribution.event_shape, event_shape)

  def testRandomTensorSample(self):
    num_samples = tf.cast(tfd.Poisson(rate=5.).sample(), tf.int32)
    _ = ed.RandomVariable(tfd.Normal(loc=0.0, scale=1.0),
                          sample_shape=num_samples)
 def testOperatorsNeg(self):
   x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
   z = -x
   z_value = -x.value
   z_eval, z_value_eval = self.evaluate([z, z_value])
   self.assertAllEqual(z_eval, z_value_eval)
 def testGradientsSecondOrder(self):
   f = lambda x: 2. * x**2.
   df = lambda x: tfp.math.value_and_gradient(f, x)[1]
   x = ed.RandomVariable(tfd.Normal(0., 1.))
   _, d2ydx2 = tfp.math.value_and_gradient(df, x)
   self.assertEqual(self.evaluate(d2ydx2), 4.)
 def testOperatorsAbs(self):
   x = ed.RandomVariable(tfd.Normal(0.0, 1.0))
   z = abs(x)
   z_value = abs(x.value)
   z_eval, z_value_eval = self.evaluate([z, z_value])
   self.assertAllEqual(z_eval, z_value_eval)