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 model(): 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 log_reg(d, N, w_init=(1, 1), x_init=(0, 1)): w = ed.Normal(loc=tf.ones([d], dtype="float32") * w_init[0], scale=1. * w_init[1], name="w") w0 = ed.Normal(loc=1. * w_init[0], scale=1. * w_init[1], name="w0") x = ed.Normal(loc=tf.ones([N, d], dtype="float32") * x_init[0], scale=1. * x_init[1], name="x") y = ed.Bernoulli(logits=tf.tensordot(x, w, axes=[[1], [0]]) + w0, name="y") return x, y, (w, w0)
def election(N, n_state, black, female, state): mua = ed.Normal(loc=0., scale=100., name='mua') log_sigma_a = ed.Normal(loc=0., scale=10., name='log_sigma_a') a = ed.Normal(loc=mua, scale=tf.ones([n_state]) * tf.exp(log_sigma_a), name='a') b1 = ed.Normal(loc=0., scale=100., name='b1') b2 = ed.Normal(loc=0., scale=100., name='b2') C = tf.one_hot(state, n_state) # shape (N, J) y_hat = tf.matmul(C, tf.expand_dims(a, 1)) + tf.expand_dims( female, 1) * b2 + tf.expand_dims(black, 1) * b1 return ed.Bernoulli(logits=y_hat, name='y')
def german_credit_model(): x_numeric = tf.constant(numericals.astype(np.float32)) x_categorical = [tf.one_hot(c, c.max() + 1) for c in categoricals] all_x = tf.concat([x_numeric] + x_categorical, 1) num_features = int(all_x.shape[1]) overall_log_scale = ed.Normal(loc=0., scale=10., name='overall_log_scale') beta_log_scales = ed.Normal(loc=overall_log_scale, scale=tf.ones([num_features]), name='beta_log_scales') beta = ed.Normal(loc=tf.zeros([num_features]), scale=tf.exp(beta_log_scales), name='beta') logits = tf.einsum('nd,md->mn', all_x, beta[tf.newaxis, :]) return ed.Bernoulli(logits=logits, name='y')
def binary_bayesian_network_log_likelihood(x, mu, sigma): w1 = ed.Normal(loc=tf.constant(mu, shape=[2, 16], dtype=tf.float32), scale=tf.constant(sigma, shape=[2, 16], dtype=tf.float32), name="w1") b1 = ed.Normal(loc=tf.constant(mu, shape=[1, 16], dtype=tf.float32), scale=tf.constant(sigma, shape=[1, 16], dtype=tf.float32), name="b1") y1 = tf.math.tanh(tf.add(tf.matmul(x, w1), b1)) w2 = ed.Normal(loc=tf.constant(mu, shape=[16, 1], dtype=tf.float32), scale=tf.constant(sigma, shape=[16, 1], dtype=tf.float32), name="w2") b2 = ed.Normal(loc=tf.constant(mu, shape=[1], dtype=tf.float32), scale=tf.constant(sigma, shape=[1], dtype=tf.float32), name="b2") y2 = tf.math.sigmoid(tf.add(tf.matmul(y1, w2), b2)) y = ed.Bernoulli(logits=y2, name='y') return y
def testBernoulliSample(self, logits, n): rv = ed.Bernoulli(logits) dist = tfd.Bernoulli(logits) self.assertEqual(rv.distribution.sample(n).shape, dist.sample(n).shape)
class GeneratedRandomVariablesTest(parameterized.TestCase, tf.test.TestCase): 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]}, ) 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]}, ) 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]}, ) 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) 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, "kwargs": {"loc": 0.5, "scale": 1.0}}, {"cls": ed.Normal, "value": [2], "kwargs": {"loc": [0.5], "scale": [1.0]}}, {"cls": ed.Poisson, "value": 2, "kwargs": {"rate": 0.5}}, ) def testValueShapeAndDtype(self, cls, value, kwargs): self._testValueShapeAndDtype(cls, value, **kwargs) def testValueMismatchRaises(self): with self.assertRaises(ValueError): self._testValueShapeAndDtype(ed.Normal, 2, loc=[0.5, 0.5], scale=1.0) with self.assertRaises(ValueError): self._testValueShapeAndDtype(ed.Normal, 2, loc=[0.5], scale=[1.0]) with self.assertRaises(ValueError): self._testValueShapeAndDtype( ed.Normal, np.zeros([10, 3]), loc=[0.5, 0.5], scale=[1.0, 1.0]) def testValueUnknownShape(self): if tf.executing_eagerly(): return # should not raise error ed.Bernoulli(probs=0.5, value=tf.compat.v1.placeholder(tf.int32)) def testAsRandomVariable(self): # A wrapped Normal distribution should behave identically to # the builtin Normal RV. def model_builtin(): return ed.Normal(1., 0.1, name="x") def model_wrapped(): return ed.as_random_variable(tfd.Normal(1., 0.1, name="x")) # Check that both models are interceptable and yield # identical log probs. 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.))) # Check that our attempt to back out the variable name from the # Distribution name is robust to name scoping. with tf.compat.v1.name_scope("nested_scope"): dist = tfd.Normal(1., 0.1, name="x") def model_scoped(): return ed.as_random_variable(dist) log_joint_scoped = ed.make_log_joint_fn(model_scoped) self.assertEqual(self.evaluate(log_joint_builtin(x=7.)), self.evaluate(log_joint_scoped(x=7.))) def testAllDistributionsAreRVs(self): for (dist_name, dist_class) in six.iteritems(tfd.__dict__): if inspect.isclass(dist_class) and issubclass( dist_class, tfd.Distribution): self.assertIn(dist_name, ed.__dict__)
def testValueUnknownShape(self): if tf.executing_eagerly(): return # should not raise error ed.Bernoulli(probs=0.5, value=tf.compat.v1.placeholder(tf.int32))
def testValueUnknownShape(self): # should not raise error ed.Bernoulli(probs=0.5, value=tf.placeholder(tf.int32))
import tensorflow as tf from tensorflow_probability import edward2 as ed N = 10000 car_door = ed.Categorical(probs=tf.constant([1. / 3., 1. / 3., 1. / 3.]), sample_shape=N, name='car_door') picked_door = ed.Categorical(probs=tf.constant([1. / 3., 1. / 3., 1. / 3.]), sample_shape=N, name='picked_door') preference = ed.Bernoulli(probs=tf.constant(0.5), sample_shape=N, name='preference') host_choice = tf.where(tf.not_equal(car_door, picked_door), 3 - car_door - picked_door, tf.where( tf.equal(car_door, 2 * tf.ones(N, dtype=tf.int32)), preference, tf.where( tf.equal(car_door, tf.ones(N, dtype=tf.int32)), 2 * preference, 1 + preference)), name='host_choice') #changed_door = 3 - host_choice - picked_door changed_door = tf.subtract(tf.subtract(3, host_choice), picked_door, name='changed_door') writer = tf.summary.FileWriter('./graphs_tfp', tf.get_default_graph())
def testShapeBernoulli(self): self._testShape(ed.Bernoulli(probs=0.5), [], [], []) self._testShape(ed.Bernoulli(tf.zeros([2, 3])), [], [2, 3], []) self._testShape(ed.Bernoulli(probs=0.5, sample_shape=2), [2], [], []) self._testShape(ed.Bernoulli(probs=0.5, sample_shape=[2, 1]), [2, 1], [], [])
def _testBernoulliSample(self, probs, n): rv = ed.Bernoulli(probs) dist = tfd.Bernoulli(probs) self.assertEqual(rv.distribution.sample(n).shape, dist.sample(n).shape)