def testShapeDirichlet(self): self._testShape(ed.Dirichlet(tf.zeros(3)), [], [], [3]) self._testShape(ed.Dirichlet(tf.zeros([2, 3])), [], [2], [3]) self._testShape(ed.Dirichlet(tf.zeros(3), sample_shape=1), [1], [], [3]) self._testShape(ed.Dirichlet(tf.zeros(3), sample_shape=[2, 1]), [2, 1], [], [3])
def variational_model(): qpi = ed.Dirichlet(concentration=qpi_alpha, name='qpi') # This model works well qmu = ed.MultivariateNormalDiag( loc=qmu_loc, scale_diag=qmu_scale, name='qmu') # qmu = ed.Normal(loc=qmu_loc, scale=qmu_scale, name='qmu') qsigma = ed.InverseGamma(concentration=qsigma_alpha, rate=qsigma_beta, name='qsigma') qz = ed.MultivariateNormalDiag(loc=qz_loc, scale_diag=qz_scale, name='qz') qw = ed.MultivariateNormalDiag(loc=qw_loc, scale_diag=qw_scale, name='qw') return qpi, qmu, qsigma, qz, qw
def model_mixture(X, ls=1., n_mix=2, ridge_factor=1e-3): """Defines the Gaussian Process Model. Args: X: (np.ndarray of float32) input training features. with dimension (N, D). ls: (float32) length scale parameter. n_mix: (int8) Number of mixture components. ridge_factor: (float32) ridge factor to stabilize Cholesky decomposition. Returns: (tf.Tensors of float32) model parameters. """ N = X.shape[0] K_mat = rbf(X, ls=ls, ridge_factor=ridge_factor) mix_prob = ed.Dirichlet(concentration=tf.ones(n_mix, dtype=tf.float32) / n_mix, name='mix_prob') gp_f = ed.Independent(distribution=tfd.MultivariateNormalTriL( loc=tf.zeros(shape=[n_mix, N]), scale_tril=replicate_along_zero_axis(tf.cholesky(K_mat), n_mix), ), reinterpreted_batch_ndims=1, name="gp_f") sigma = ed.Normal(loc=tf.ones(n_mix) * -5., scale=tf.ones(n_mix) * 1., name='sigma') y = ed.MixtureSameFamily( components_distribution=tfd.MultivariateNormalDiag( loc=gp_f, scale_identity_multiplier=tf.exp(sigma)), mixture_distribution=tfd.Categorical(probs=mix_prob), name="y") return mix_prob, gp_f, sigma, y
def latent_dirichlet_allocation(concentration, topics_words): """Latent Dirichlet Allocation in terms of its generative process. The model posits a distribution over bags of words and is parameterized by a concentration and the topic-word probabilities. It collapses per-word topic assignments. Args: concentration: A Tensor of shape [1, num_topics], which parameterizes the Dirichlet prior over topics. topics_words: A Tensor of shape [num_topics, num_words], where each row (topic) denotes the probability of each word being in that topic. Returns: bag_of_words: A random variable capturing a sample from the model, of shape [1, num_words]. It represents one generated document as a bag of words. """ topics = ed.Dirichlet(concentration=concentration, name="topics") word_probs = tf.matmul(topics, topics_words) # The observations are bags of words and therefore not one-hot. However, # log_prob of OneHotCategorical computes the probability correctly in # this case. bag_of_words = ed.OneHotCategorical(probs=word_probs, name="bag_of_words") return bag_of_words
def trainable_positive_pointmass(shape, name=None): with tf.variable_scope(None, default_name="trainable_positive_pointmass"): # 没有PointMass分布,用Dirichlet分布代替 return ed.Dirichlet(tf.nn.softplus(tf.get_variable("mean", shape)), name=name)
def lda_variational(bag_of_words): concentration = _clip_dirichlet_parameters(encoder_net(bag_of_words)) return ed.Dirichlet(concentration=concentration, name="topics_posterior")
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 lda_variational(bag_of_words): # concentration = _clip_dirichlet_parameters(tf.contrib.layers.batch_norm(encoder_net(bag_of_words))) concentration = _clip_dirichlet_parameters(encoder_net(bag_of_words)) return ed.Dirichlet(concentration=concentration, name="topics_posterior")