class_docstr: A layer for computing a pairwise distance in Keras models.
  
  With `Model.add_loss`, this layer can be used to build a Keras model with
  graph regularization.
  
  Example:
  
  ```python
  def regularize_model(unregularized_model, inputs):
    features, neighbors, neighbor_weights = inputs
    # Standard logistic regression.
    logits = unregularized_model(features)
    model = tf.keras.Model(inputs=inputs, outputs=logits)
    # Add regularization.
    regularizer = layers.PairwiseDistance(
        configs.DistanceConfig(sum_over_axis=-1))
    graph_loss = regularizer(
        sources=logits,
        targets=unregularized_model(neighbors),
        weights=neighbor_weights)
    model.add_loss(graph_loss)
    model.add_metric(graph_loss, aggregation="mean", name="graph_loss")
    return model
  ```
  
  This layer makes some assumptions about how the input is shaped. Either (a)
  the first dimension of `sources` should divide the first dimension of
  `targets`, with the rest of dimensions being equal or (b) `targets` should
  have an additional neighborhood size dimension at axis -2, the last dimension
  of `sources` and `targets` should match, and all other dimensions of `sources`
  should also match with corresponding dimensions in `targets`. See
  `_replicate_sources` for details.
class_docstr_tok: ["A","layer","for","computing","a","pairwise","distance","in","Keras","models",".","With","`","Model.add_loss","`",",","this","layer","can","be","used","to","build","a","Keras","model","with","graph","regularization",".","Example",":","``","`","python","def","regularize_model","(","unregularized_model",",","inputs",")",":","features",",","neighbors",",","neighbor_weights","=","inputs","#","Standard","logistic","regression",".","logits","=","unregularized_model","(","features",")","model","=","tf.keras.Model","(","inputs=inputs",",","outputs=logits",")","#","Add","regularization",".","regularizer","=","layers.PairwiseDistance","(","configs.DistanceConfig","(","sum_over_axis=-1",")",")","graph_loss","=","regularizer","(","sources=logits",",","targets=unregularized_model","(","neighbors",")",",","weights=neighbor_weights",")","model.add_loss","(","graph_loss",")","model.add_metric","(","graph_loss",",","aggregation=","''","mean","''",",","name=","''","graph_loss","''",")","return","model","``","`","This","layer","makes","some","assumptions","about","how","the","input","is","shaped",".","Either","(","a",")","the","first","dimension","of","`","sources","`","should","divide","the","first","dimension","of","`","targets","`",",","with","the","rest","of","dimensions","being","equal","or","(","b",")","`","targets","`","should","have","an","additional","neighborhood","size","dimension","at","axis","-2",",","the","last","dimension","of","`","sources","`","and","`","targets","`","should","match",",","and","all","other","dimensions","of","`","sources","`","should","also","match","with","corresponding","dimensions","in","`","targets","`",".","See","`","_replicate_sources","`","for","details","."]
docstr: Replicates sources and computes pairwise distance.
  
  Args:
    inputs: Symbolic inputs. Should be (sources, targets) if `weights` is
      non-symbolic. Otherwise, should be (sources, targets, weights).
    weights: If target weights are not symbolic, `weights` should be passed as
      a separate argument. In this case, `inputs` should have length 2.
  
  Returns:
    Pairwise distance tensor.
docstr_tok: ["Replicates","sources","and","computes","pairwise","distance",".","Args",":","inputs",":","Symbolic","inputs",".","Should","be","(","sources",",","targets",")","if","`","weights","`","is","non-symbolic",".","Otherwise",",","should","be","(","sources",",","targets",",","weights",")",".","weights",":","If","target","weights","are","not","symbolic",",","`","weights","`","should","be","passed","as","a","separate","argument",".","In","this","case",",","`","inputs","`","should","have","length","2",".","Returns",":","Pairwise","distance","tensor","."]
code: def call(self, inputs, weights=None):
      """Replicates sources and computes pairwise distance.
  
      Args:
        inputs: Symbolic inputs. Should be (sources, targets) if `weights` is
          non-symbolic. Otherwise, should be (sources, targets, weights).
        weights: If target weights are not symbolic, `weights` should be passed as
          a separate argument. In this case, `inputs` should have length 2.
  
      Returns:
        Pairwise distance tensor.
      """
      if weights is None:
          sources, targets, weights = inputs
      else:
          sources, targets = inputs
  
      return distances.pairwise_distance_wrapper(
          sources=self._replicate_sources(sources, targets),
          targets=targets,
          weights=weights,
          distance_config=self._distance_config,
      )
  
file_path: neural-structured-learning/neural_structured_learning/keras/layers/pairwise_distance.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import attr
  import enum
  import neural_structured_learning.configs
  from neural_structured_learning.lib import distances
  from neural_structured_learning.lib import utils
  import tensorflow
label_desc: Use image node_id 3 for calling the PairwiseDistance obj's underlying member method code with example usage: obj.call(inputs, weights) and returns: distances