class_docstr: Tests for `pairwise_distance_lib.PairwiseDistance`.
class_docstr_tok: ["Tests","for","`","pairwise_distance_lib.PairwiseDistance","`","."]
docstr: Tests when sources and targets have the same rank.
docstr_tok: ["Tests","when","sources","and","targets","have","the","same","rank","."]
code: def testReplicateSources(self):
      """Tests when sources and targets have the same rank."""
  
      def _make_model(sources_shape, targets_shape):
          """Makes a model where `sources` and `targets` have the same rank."""
          sources = tf.keras.Input(sources_shape, name="sources")
          targets = tf.keras.Input(targets_shape, name="targets")
          outputs = pairwise_distance_lib.PairwiseDistance(
              configs.DistanceConfig(
                  distance_type=configs.DistanceType.KL_DIVERGENCE,
                  reduction=tf.compat.v1.losses.Reduction.NONE,
                  sum_over_axis=-1,
              )
          )(sources, targets)
          return tf.keras.Model(
              inputs=[sources, targets], outputs=outputs
          )
  
      model = _make_model(sources_shape=(4,), targets_shape=(4,))
      # Test when first dimension of targets is a multiple of the batch size.
      sources = np.array(
          [
              [0.1, 0.2, 0.3, 0.4],
              [0.25, 0.25, 0.25, 0.25],
          ]
      )
      targets = np.array(
          [
              [0.6, 0.2, 0.1, 0.1],
              [0.4, 0.5, 0.075, 0.025],
              [0.001, 0.333, 0.333, 0.333],
              [0.9, 0.05, 0.03, 0.02],
          ]
      )
      kl_divergence = self.evaluate(model([sources, targets]))
      expected = np.sum(
          special.kl_div(np.repeat(sources, 2, axis=0), targets),
          -1,
          keepdims=True,
      )
      self.assertAllClose(kl_divergence, expected, _ERR_TOL)
      # Test when that the shapes are not compatible.
      with self.assertRaisesRegex(
          ValueError, "Shapes\\s.+\\sare incompatible"
      ):
          self.evaluate(model([sources, targets[1:]]))
      # And also the case when targets has a neighborhood size dimension.
      model = _make_model(sources_shape=(4,), targets_shape=(2, 4))
      self.assertAllClose(
          self.evaluate(model([sources, targets.reshape((-1, 2, 4))])),
          expected.reshape((-1, 2, 1)),
          _ERR_TOL,
      )
  
file_path: neural-structured-learning/neural_structured_learning/keras/layers/pairwise_distance_test.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import neural_structured_learning.configs
  from neural_structured_learning.keras.layers import pairwise_distance
  import numpy
  from scipy import special
  import tensorflow
  from tensorflow.python.framework import test_util
label_desc: Use image node_id 6 for calling the PairwiseDistanceTest obj's underlying member method code with example usage: obj.testReplicateSources() and returns: tf