def logistic_regression(features): """Bayesian logistic regression, which returns labels given features.""" coeffs = ed.MultivariateNormalDiag( loc=tf.zeros(features.shape[1]), name="coeffs") labels = ed.Bernoulli( logits=tf.tensordot(features, coeffs, [[1], [0]]), name="labels") return labels
def testBernoulliLogProb(self, logits, n): rv = ed.Bernoulli(logits) dist = tfd.Bernoulli(logits) x = rv.distribution.sample(n) rv_log_prob, dist_log_prob = self.evaluate( [rv.distribution.log_prob(x), dist.log_prob(x)]) self.assertAllEqual(rv_log_prob, dist_log_prob)
def mixture_of_real_and_int(): loc = ed.Normal(loc=0., scale=1., name="loc") flip = ed.Bernoulli(probs=0.5, name="flip") if tf.equal(flip, 1): x = ed.Normal(loc=loc, scale=0.5, sample_shape=5, name="x") else: x = ed.Poisson(rate=tf.nn.softplus(loc), sample_shape=3, name="x") return x
def model(population_size): """Creates the Variables for this demo.""" pref_dimension = 3 # pylint: disable=g-long-lambda world_state = Variable( name="world state", spec=ValueSpec(three_headed_monkeys=Space( space=spaces.Box(low=np.array([-np.Inf] * pref_dimension), high=np.array([np.Inf] * pref_dimension))))) social_network = Variable( name="social network", spec=ValueSpec(n=Space( space=spaces.Box(low=np.array([0] * population_size), high=np.array([1] * population_size))))) user_state = Variable( name="user state", spec=ValueSpec(preference=Space( space=spaces.Box(low=np.array([-np.Inf] * pref_dimension), high=np.array([np.Inf] * pref_dimension))))) # Static variables. world_state.initial_value = variable.value( lambda: Value(three_headed_monkeys=ed.Normal( loc=[3.14] * pref_dimension, scale=[0.01], sample_shape=(1, )))) social_network.initial_value = variable.value(lambda: Value(n=ed.Bernoulli( probs=0.01 * tf.ones((population_size, population_size)), dtype=tf.float32))) # Dynamic variables user_state.initial_value = variable.value( lambda: Value(preference=ed.Normal(loc=[3.14] * pref_dimension, scale=[4.13], sample_shape=population_size))) user_state.value = variable.value( lambda previous_user_state, social_network: Value(preference=ed.Normal( loc=(0.7 * previous_user_state.get("preference") + 0.3 * tf.matmul( social_network.get("n"), previous_user_state.get("preference")) ), scale=[0.01])), dependencies=[user_state.previous, social_network]) return [user_state, world_state, social_network]
def testBernoulliSample(self, logits, n): rv = ed.Bernoulli(logits) dist = tfp.distributions.Bernoulli(logits) self.assertEqual(rv.distribution.sample(n).shape, dist.sample(n).shape)
def testBernoulliLogProb(self, logits, n): rv = ed.Bernoulli(logits) dist = tfp.distributions.Bernoulli(logits) x = rv.distribution.sample(n) self.assertAllEqual(rv.distribution.log_prob(x), dist.log_prob(x))
class GeneratedRandomVariablesTest(parameterized.TestCase, tf.test.TestCase): @tfe.run_test_in_graph_and_eager_modes def testBernoulliDoc(self): self.assertGreater(len(ed.Bernoulli.__doc__), 0) self.assertIn(inspect.cleandoc(tfd.Bernoulli.__init__.__doc__), ed.Bernoulli.__doc__) self.assertEqual(ed.Bernoulli.__name__, "Bernoulli") @parameterized.named_parameters( { "testcase_name": "1d_rv_1d_event", "logits": np.zeros(1), "n": [1] }, { "testcase_name": "1d_rv_5d_event", "logits": np.zeros(1), "n": [5] }, { "testcase_name": "5d_rv_1d_event", "logits": np.zeros(5), "n": [1] }, { "testcase_name": "5d_rv_5d_event", "logits": np.zeros(5), "n": [5] }, ) @tfe.run_test_in_graph_and_eager_modes def testBernoulliLogProb(self, logits, n): rv = ed.Bernoulli(logits) dist = tfd.Bernoulli(logits) x = rv.distribution.sample(n) rv_log_prob, dist_log_prob = self.evaluate( [rv.distribution.log_prob(x), dist.log_prob(x)]) self.assertAllEqual(rv_log_prob, dist_log_prob) @parameterized.named_parameters( { "testcase_name": "0d_rv_0d_sample", "logits": 0., "n": 1 }, { "testcase_name": "0d_rv_1d_sample", "logits": 0., "n": [1] }, { "testcase_name": "1d_rv_1d_sample", "logits": np.array([0.]), "n": [1] }, { "testcase_name": "1d_rv_5d_sample", "logits": np.array([0.]), "n": [5] }, { "testcase_name": "2d_rv_1d_sample", "logits": np.array([-0.2, 0.8]), "n": [1] }, { "testcase_name": "2d_rv_5d_sample", "logits": np.array([-0.2, 0.8]), "n": [5] }, ) @tfe.run_test_in_graph_and_eager_modes def testBernoulliSample(self, logits, n): rv = ed.Bernoulli(logits) dist = tfd.Bernoulli(logits) self.assertEqual(rv.distribution.sample(n).shape, dist.sample(n).shape) @parameterized.named_parameters( { "testcase_name": "0d_bernoulli", "rv": ed.Bernoulli(probs=0.5), "sample_shape": [], "batch_shape": [], "event_shape": [] }, { "testcase_name": "2d_bernoulli", "rv": ed.Bernoulli(tf.zeros([2, 3])), "sample_shape": [], "batch_shape": [2, 3], "event_shape": [] }, { "testcase_name": "2x0d_bernoulli", "rv": ed.Bernoulli(probs=0.5, sample_shape=2), "sample_shape": [2], "batch_shape": [], "event_shape": [] }, { "testcase_name": "2x1d_bernoulli", "rv": ed.Bernoulli(probs=0.5, sample_shape=[2, 1]), "sample_shape": [2, 1], "batch_shape": [], "event_shape": [] }, { "testcase_name": "3d_dirichlet", "rv": ed.Dirichlet(tf.zeros(3)), "sample_shape": [], "batch_shape": [], "event_shape": [3] }, { "testcase_name": "2x3d_dirichlet", "rv": ed.Dirichlet(tf.zeros([2, 3])), "sample_shape": [], "batch_shape": [2], "event_shape": [3] }, { "testcase_name": "1x3d_dirichlet", "rv": ed.Dirichlet(tf.zeros(3), sample_shape=1), "sample_shape": [1], "batch_shape": [], "event_shape": [3] }, { "testcase_name": "2x1x3d_dirichlet", "rv": ed.Dirichlet(tf.zeros(3), sample_shape=[2, 1]), "sample_shape": [2, 1], "batch_shape": [], "event_shape": [3] }, ) @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.sample_shape, sample_shape) self.assertEqual(rv.distribution.batch_shape, batch_shape) self.assertEqual(rv.distribution.event_shape, event_shape) @parameterized.parameters( { "cls": ed.Normal, "value": 2, "loc": 0.5, "scale": 1.0 }, { "cls": ed.Normal, "value": [2], "loc": [0.5], "scale": [1.0] }, { "cls": ed.Poisson, "value": 2, "rate": 0.5 }, ) @tfe.run_test_in_graph_and_eager_modes def testValueShapeAndDtype(self, cls, value, **kwargs): rv = cls(value=value, **kwargs) value_shape = rv.value.shape expected_shape = rv.sample_shape.concatenate( rv.distribution.batch_shape).concatenate( rv.distribution.event_shape) self.assertEqual(value_shape, expected_shape) self.assertEqual(rv.distribution.dtype, rv.value.dtype) @parameterized.parameters( { "cls": ed.Normal, "value": 2, "loc": [0.5, 0.5], "scale": 1.0 }, { "cls": ed.Normal, "value": 2, "loc": [0.5], "scale": [1.0] }, { "cls": ed.Normal, "value": np.zeros([10, 3]), "loc": [0.5, 0.5], "scale": [1.0, 1.0] }, ) @tfe.run_test_in_graph_and_eager_modes def testValueMismatchRaises(self, cls, value, **kwargs): with self.assertRaises(ValueError): cls(value=value, **kwargs) def testValueUnknownShape(self): # should not raise error ed.Bernoulli(probs=0.5, value=tf1.placeholder(tf.int32)) @tfe.run_test_in_graph_and_eager_modes def testMakeRandomVariable(self): """Tests that manual wrapping is the same as the built-in solution.""" custom_normal = ed.make_random_variable(tfd.Normal) def model_builtin(): return ed.Normal(1., 0.1, name="x") def model_wrapped(): return custom_normal(1., 0.1, name="x") log_joint_builtin = ed.make_log_joint_fn(model_builtin) log_joint_wrapped = ed.make_log_joint_fn(model_wrapped) self.assertEqual(self.evaluate(log_joint_builtin(x=7.)), self.evaluate(log_joint_wrapped(x=7.)))
def testValueUnknownShape(self): # should not raise error ed.Bernoulli(probs=0.5, value=tf1.placeholder(tf.int32))