code: def reduce_with_weights(
      tensor,
      weights=None,
      average_across_batch=True,
      average_across_remaining=False,
      sum_over_batch=False,
      sum_over_remaining=True,
      tensor_rank=None,
  ):
      """Weights and reduces tensor.
  
      Args:
          tensor: A Tensor to weight and reduce, of shape
              `[batch_size, ...]`.
          weights (optional): A Tensor of the same shape and dtype with
              :attr:`tensor`. For example, this is can be a 0-1 tensor
              for masking values of :attr:`tensor``.
          average_across_batch (bool): If set, average the tensor across the
              batch dimension. Must not set `average_across_batch`'
              and `sum_over_batch` at the same time.
          average_across_remaining (bool): If set, average the
              tensor across the
              remaining dimensions. Must not set `average_across_remaining`'
              and `sum_over_remaining` at the same time.
              If :attr:`weights` is given, this is a weighted average.
          sum_over_batch (bool): If set, sum the tensor across the
              batch dimension. Must not set `average_across_batch`
              and `sum_over_batch` at the same time.
          sum_over_remaining (bool): If set, sum the tensor
              across the
              remaining dimension. Must not set `average_across_remaining`
              and `sum_over_remaining` at the same time.
              If :attr:`weights` is given, this is a weighted sum.
          tensor_rank (int, optional): The number of dimensions of
              :attr:`tensor`. If not given, inferred from :attr:`tensor`
              automatically.
  
      Returns:
          A Tensor.
  
      Example:
          .. code-block:: python
  
              x = tf.constant([[10, 10, 2, 2],
                               [20, 2, 2, 2]])
              mask = tf.constant([[1, 1, 0, 0],
                                  [1, 0, 0, 0]])
  
              z = reduce_with_weights(x, weights=mask)
              # z == 20
              # (all 2 in x are masked)
      """
      if tensor_rank is None:
          tensor_rank = get_rank(tensor)
      if tensor_rank is None:
          raise ValueError(
              "Unable to infer the rank of `tensor`. "
              "Please set `tensor_rank` explicitly."
          )
  
      if weights is not None:
          tensor = tensor * weights
  
      if tensor_rank > 1:
          if average_across_remaining and sum_over_remaining:
              raise ValueError(
                  "Only one of `average_across_remaining` and "
                  "`sum_over_remaining` can be set."
              )
          if average_across_remaining:
              if weights is None:
                  tensor = tf.reduce_mean(
                      tensor, axis=np.arange(1, tensor_rank)
                  )
              else:
                  tensor = tf.reduce_sum(
                      tensor, axis=np.arange(1, tensor_rank)
                  )
                  weights = tf.reduce_sum(
                      weights, axis=np.arange(1, tensor_rank)
                  )
                  tensor = tensor / weights
          elif sum_over_remaining:
              tensor = tf.reduce_sum(
                  tensor, axis=np.arange(1, tensor_rank)
              )
  
      if average_across_batch and sum_over_batch:
          raise ValueError(
              "Only one of `average_across_batch` and "
              "`sum_over_batch` can be set."
          )
      if sum_over_batch:
          tensor = tf.reduce_sum(tensor, axis=[0])
      elif average_across_batch:
          tensor = tf.reduce_mean(tensor, axis=[0])
  
      return tensor
  
file_path: texar/texar/tf/utils/shapes.py
imports: import numpy
  import tensorflow
  from tensorflow.python.util import nest
  from tensorflow.python.ops import rnn
  from tensorflow.python.framework import ops
label_desc: Use image node_id 7 for calling a global function with example usage: reduce_with_weights(tensor, weights, average_across_batch, average_across_remaining, sum_over_batch, sum_over_remaining, tensor_rank) and returns: tensor