def testSessionRun(self): with self.cached_session() as sess: x = ed.RandomVariable(tfp.distributions.Normal(0.0, 0.1)) x_ph = tf1.placeholder(tf.float32, []) y = ed.RandomVariable(tfp.distributions.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)
def testConstructor(self): x = ed.RandomVariable(tfp.distributions.Poisson(rate=np.ones([2, 5])), value=np.ones([2, 5])) self.assertAllEqual(tf.convert_to_tensor(x), x.value) with self.assertRaises(ValueError): _ = ed.RandomVariable(tfp.distributions.Bernoulli(probs=0.5), value=tf.zeros([2, 5], dtype=tf.int32)) x = ed.RandomVariable(FakeDistribution()) with self.assertRaises(NotImplementedError): _ = ed.RandomVariable(FakeDistributionNoSample())
def testOperatorsFloordiv(self): x = ed.RandomVariable(tfp.distributions.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(tfp.distributions.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 make_index_rv(*args, **kwargs): del args sample_shape = kwargs.pop('sample_shape', ()) rv_value = kwargs.pop('value', None) return ed.RandomVariable(distribution=index_dist, sample_shape=sample_shape, value=rv_value)
def testArrayPriority(self): x = ed.RandomVariable(tfp.distributions.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 testNumpy(self): x = ed.RandomVariable(tfp.distributions.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 testOperatorsGetitem(self): x = ed.RandomVariable(tfp.distributions.Normal(tf.random.normal([3, 4]), 1.)) z = x[0:2, 2:3] z_value = x.value[0:2, 2:3] self.assertIsInstance(z, ed.RandomVariable) self.assertAllEqual(z.distribution.mean(), x.distribution.mean()[0:2, 2:3]) self.assertAllEqual(tf.convert_to_tensor(z), z_value)
def testShape(self, probs, sample_shape, batch_shape, event_shape): rv = ed.RandomVariable(tfp.distributions.Bernoulli(probs=probs), sample_shape=sample_shape) 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 testStr(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0), value=1.234) if tf.executing_eagerly(): pattern = "RandomVariable(\"1.234\", shape=(), dtype=float32" else: pattern = "RandomVariable(\"{name}\", shape=(), dtype=float32".format( name=x.distribution.name) regexp = re.escape(pattern) self.assertRegexpMatches(str(x), regexp)
def testGradientsFirstOrder(self): x = ed.RandomVariable(tfp.distributions.Normal(0., 1.)) def f(x): return 2. * x with tf.GradientTape() as tape: tape.watch(x.value) y = f(x) z = tape.gradient(y, [x.value])[0] self.assertEqual(z, 2.)
def testRepr(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0), value=1.234) if tf.executing_eagerly(): string = ("<ed.RandomVariable '{name}' shape=() " "dtype=float32 numpy=1.234>".format(name=x.distribution.name)) else: string = "<ed.RandomVariable '{name}' shape=() dtype=float32>".format( name=x.distribution.name) self.assertEqual(repr(x), string)
def testGradientsFirstOrder(self): f = lambda x: 2. * x x = ed.RandomVariable(tfp.distributions.Normal(0., 1.)) y = f(x) if tf.executing_eagerly(): df = tfe.gradients_function(f) (z, ) = df(x) else: (z, ) = tf.gradients(y, x) self.assertEqual(self.evaluate(z), 2.)
def testOperatorsGetitem(self): x = ed.RandomVariable(tfp.distributions.Normal(tf.random.normal([3, 4]), 1.)) z = x[0:2, 2:3] self.assertIsInstance(z, ed.RandomVariable) z_mean_eval, x_mean_eval = self.evaluate([z.distribution.mean(), x.distribution.mean()[0:2, 2:3]]) self.assertAllEqual(z_mean_eval, x_mean_eval) 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 testGradientsSecondOrder(self): f = lambda x: 2 * (x**2) x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) y = f(x) if tf.executing_eagerly(): 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)
def testGradientsSecondOrder(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) def f(x): return 2 * (x ** 2) with tf.GradientTape() as tape2: tape2.watch(x.value) with tf.GradientTape() as tape: tape.watch(x.value) y = f(x) z = tape.gradient(y, [x.value])[0] z = tape2.gradient(z, [x.value])[0] self.assertEqual(z, 4.0)
def testGradientsFirstOrder(self): x = ed.RandomVariable(tfp.distributions.Normal(0., 1.)) def f(x): return 2. * x if tf.executing_eagerly(): with tf.GradientTape() as tape: tape.watch(x.value) y = f(x) z = tape.gradient(y, [x.value])[0] else: y = f(x) (z,) = tf.gradients(y, x) self.assertEqual(self.evaluate(z), 2.)
def testGradientsSecondOrder(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) def f(x): return 2 * (x ** 2) if tf.executing_eagerly(): with tf.GradientTape() as tape2: tape2.watch(x.value) with tf.GradientTape() as tape: tape.watch(x.value) y = f(x) z = tape.gradient(y, [x.value])[0] z = tape2.gradient(z, [x.value])[0] else: y = f(x) (z,) = tf.gradients(y, x) (z,) = tf.gradients(z, x) self.assertEqual(self.evaluate(z), 4.0)
def testOperatorsHash(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) y = 5.0 self.assertNotEqual(hash(x), hash(y)) self.assertEqual(hash(x), id(x))
def testRandomTensorSample(self): num_samples = tf.cast( tfp.distributions.Poisson(rate=5.).sample(), tf.int32) _ = ed.RandomVariable(tfp.distributions.Normal(loc=0.0, scale=1.0), sample_shape=num_samples)
class RandomVariableTest(parameterized.TestCase, tf.test.TestCase): @tfe.run_test_in_graph_and_eager_modes def testConstructor(self): x = ed.RandomVariable(tfp.distributions.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(tfp.distributions.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(tfp.distributions.Normal(0., 1.)) y = f(x) if tf.executing_eagerly(): 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(tfp.distributions.Normal(0.0, 1.0)) y = f(x) if tf.executing_eagerly(): 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(tfp.distributions.Normal(0.0, 1.0), value=1.234) if tf.executing_eagerly(): pattern = "RandomVariable(\"1.234\", shape=(), dtype=float32" else: pattern = "RandomVariable(\"{name}\", shape=(), dtype=float32".format( name=x.distribution.name) regexp = re.escape(pattern) self.assertRegexpMatches(str(x), regexp) @tfe.run_test_in_graph_and_eager_modes def testRepr(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0), value=1.234) if tf.executing_eagerly(): string = ("<ed.RandomVariable '{name}' shape=() " "dtype=float32 numpy=1.234>".format( name=x.distribution.name)) else: string = "<ed.RandomVariable '{name}' shape=() dtype=float32>".format( name=x.distribution.name) self.assertEqual(repr(x), string) @tfe.run_test_in_graph_and_eager_modes def testNumpy(self): x = ed.RandomVariable(tfp.distributions.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() @tfe.run_test_in_graph_and_eager_modes def testOperatorsAdd(self): x = ed.RandomVariable(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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( tfp.distributions.Normal(tf.random.normal([3, 4]), 1.)) z = x[0:2, 2:3] self.assertIsInstance(z, ed.RandomVariable) z_mean_eval, x_mean_eval = self.evaluate( [z.distribution.mean(), x.distribution.mean()[0:2, 2:3]]) self.assertAllEqual(z_mean_eval, x_mean_eval) 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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.Normal(0.0, 1.0)) y = 5.0 self.assertNotEqual(hash(x), hash(y)) self.assertEqual(hash(x), id(x)) # TODO(trandustin): Re-enable test. # @tfe.run_test_in_graph_and_eager_modes # def testOperatorsEq(self): # x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) # self.assertEqual(x, x) @tfe.run_test_in_graph_and_eager_modes def testOperatorsNe(self): x = ed.RandomVariable(tfp.distributions.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(tfp.distributions.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(tfp.distributions.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(tfp.distributions.Normal(0.0, 0.1)) with self.assertRaises(ValueError): _ = tf.convert_to_tensor(x, dtype=tf.int32) def testSessionEval(self): with self.cached_session() as sess: x = ed.RandomVariable(tfp.distributions.Normal(0.0, 0.1)) x_ph = tf1.placeholder(tf.float32, []) y = ed.RandomVariable(tfp.distributions.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.cached_session() as sess: x = ed.RandomVariable(tfp.distributions.Normal(0.0, 0.1)) x_ph = tf1.placeholder(tf.float32, []) y = ed.RandomVariable(tfp.distributions.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(tfp.distributions.Bernoulli(probs=0.5)), "sample_shape": [], "batch_shape": [], "event_shape": [] }, { "rv": ed.RandomVariable( tfp.distributions.Bernoulli(tf.zeros([2, 3]))), "sample_shape": [], "batch_shape": [2, 3], "event_shape": [] }, { "rv": ed.RandomVariable(tfp.distributions.Bernoulli(probs=0.5), sample_shape=2), "sample_shape": [2], "batch_shape": [], "event_shape": [] }, { "rv": ed.RandomVariable(tfp.distributions.Bernoulli(probs=0.5), sample_shape=[2, 1]), "sample_shape": [2, 1], "batch_shape": [], "event_shape": [] }, { "rv": ed.RandomVariable(tfp.distributions.Bernoulli(probs=0.5), sample_shape=tf.constant([2])), "sample_shape": [2], "batch_shape": [], "event_shape": [] }, { "rv": ed.RandomVariable(tfp.distributions.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.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( tfp.distributions.Poisson(rate=5.).sample(), tf.int32) _ = ed.RandomVariable(tfp.distributions.Normal(loc=0.0, scale=1.0), sample_shape=num_samples)
def testConvertToTensor(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 0.1)) with self.assertRaises(ValueError): _ = tf.convert_to_tensor(x, dtype=tf.int32)
def testOperatorsBoolNonzero(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) with self.assertRaises(TypeError): _ = not x
def testOperatorsRpow(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) y = 5.0 z = y ** x z_value = y ** x.value self.assertAllEqual(z, z_value)
def testOperatorsAbs(self): x = ed.RandomVariable(tfp.distributions.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 testNumpy(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0), value=1.23) self.assertEqual(x, tf.constant(1.23))
def testOperatorsPow(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) y = 5.0 z = x ** y z_value = x.value ** y self.assertAllEqual(z, z_value)
def testStr(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0), value=1.234) pattern = "RandomVariable(\"1.234\", shape=(), dtype=float32" regexp = re.escape(pattern) self.assertRegexpMatches(str(x), regexp)
def testOperatorsNe(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) y = 5.0 self.assertNotEqual(x, y)
def testOperatorsNeg(self): x = ed.RandomVariable(tfp.distributions.Normal(0.0, 1.0)) z = -x z_value = -x.value self.assertAllEqual(z, z_value)