Esempio n. 1
0
 def compute_gaussian_likelihoods(lhs, rhs):
     """Computes sample likelihoods."""
     num_lhs_samples = lhs.shape.as_list()[-2] - 2
     num_rhs_samples = rhs.shape.as_list()[-2] - 2
     lhs_means, lhs_stddevs, lhs_samples = tf.split(
         lhs, [1, 1, num_lhs_samples], axis=-2)
     rhs_means, rhs_stddevs, rhs_samples = tf.split(
         rhs, [1, 1, num_rhs_samples], axis=-2)
     rhs_likelihoods = distance_utils.compute_gaussian_likelihoods(
         lhs_means,
         lhs_stddevs,
         rhs_samples,
         min_stddev=distance_kernel_kwargs.get(
             distance_kernel + '_min_stddev', None),
         max_squared_mahalanobis_distance=distance_kernel_kwargs.
         get(distance_kernel + '_max_squared_mahalanobis_distance',
             None),
         smoothing=distance_kernel_kwargs.get(
             distance_kernel + '_smoothing', None))
     lhs_likelihoods = distance_utils.compute_gaussian_likelihoods(
         rhs_means,
         rhs_stddevs,
         lhs_samples,
         l2_distance_computer=l2_distance_computer,
         min_stddev=distance_kernel_kwargs.get(
             distance_kernel + '_min_stddev', None),
         max_squared_mahalanobis_distance=distance_kernel_kwargs.
         get(distance_kernel + '_max_squared_mahalanobis_distance',
             None),
         smoothing=distance_kernel_kwargs.get(
             distance_kernel + '_smoothing', None))
     return (rhs_likelihoods + lhs_likelihoods) / 2.0
 def test_compute_gaussian_likelihoods(self):
   # Shape = [2, 1, 1].
   means = tf.constant([[[1.0]], [[2.0]]])
   # Shape = [2, 1, 1].
   stddevs = tf.constant([[[1.0]], [[2.0]]])
   # Shape = [2, 3, 1].
   samples = tf.constant([[[1.0], [2.0], [3.0]], [[2.0], [4.0], [6.0]]])
   # Shape = [2, 3, 1].
   likelihoods = distance_utils.compute_gaussian_likelihoods(
       means, stddevs, samples)
   self.assertAllClose(
       likelihoods, [[[1.0], [0.3173], [0.0455]], [[1.0], [0.3173], [0.0455]]],
       atol=1e-4)