code: def mask_and_reduce(
      sequence,
      sequence_length,
      rank=2,
      average_across_batch=True,
      average_across_timesteps=False,
      average_across_remaining=False,
      sum_over_batch=False,
      sum_over_timesteps=True,
      sum_over_remaining=True,
      dtype=None,
      time_major=False,
  ):
      """Masks out sequence entries that are beyond the respective sequence
      lengths, and reduces (average or sum) away dimensions.
  
      This is a combination of :func:`~texar.tf.utils.shapes.mask_sequences`
      and :func:`~texar.tf.losses.losses_utils.reduce_batch_time`.
  
      Args:
          sequence: A Tensor of sequence values.
              If `time_major=False` (default), this must be a Tensor of shape
              `[batch_size, max_time, d_2, ..., d_rank]`, where the rank of
              the Tensor is specified with :attr:`rank`.
              The batch and time dimensions are exchanged if `time_major` is True.
          sequence_length: A Tensor of shape `[batch_size]`. Time steps beyond
              the respective sequence lengths will be made zero. If `None`,
              not masking is performed.
          rank (int): The rank of :attr:`sequence`. Must be >= 2. Default is 2,
              i.e., `sequence` is a 2D Tensor consisting of batch and time
              dimensions.
          average_across_timesteps (bool): If set, average the sequence across
              the time dimension. Must not set `average_across_timesteps`
              and `sum_over_timesteps` at the same time.
          average_across_batch (bool): If set, average the sequence 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 sequence across the
              remaining dimensions. Must not set `average_across_remaining`'
              and `sum_over_remaining` at the same time.
          sum_over_timesteps (bool): If set, sum the loss across the
              time dimension. Must not set `average_across_timesteps`
              and `sum_over_timesteps` at the same time.
          sum_over_batch (bool): If set, sum the loss 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 loss across the
              remaining dimension. Must not set `average_across_remaining`
              and `sum_over_remaining` at the same time.
          time_major (bool): The shape format of the inputs. If `True`,
              :attr:`sequence` must have shape `[max_time, batch_size, ...]`.
              If `False` (default), `sequence` must have
              shape `[batch_size, max_time, ...]`.
          dtype (dtype): Type of :attr:`sequence`. If `None`, infer from
              :attr:`sequence` automatically.
  
      Returns
          A Tensor containing the masked and reduced sequence.
      """
      if rank < 2:
          raise ValueError("`rank` must be >= 2.")
  
      if time_major:
          sequence = rnn._transpose_batch_time(sequence)
  
      if sequence_length is not None:
          sequence = mask_sequences(
              sequence,
              sequence_length,
              dtype=dtype,
              time_major=False,
              tensor_rank=rank,
          )
  
      if rank > 2:
          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:
              sequence = tf.reduce_mean(
                  sequence, axis=np.arange(2, rank)
              )
          elif sum_over_remaining:
              sequence = tf.reduce_sum(
                  sequence, axis=np.arange(2, rank)
              )
  
      sequence = reduce_batch_time(
          sequence,
          sequence_length,
          average_across_batch,
          average_across_timesteps,
          sum_over_batch,
          sum_over_timesteps,
      )
  
      reduce_time = average_across_timesteps or sum_over_timesteps
      reduce_batch = average_across_batch or sum_over_batch
      if not reduce_time and not reduce_batch and time_major:
          sequence = rnn._transpose_batch_time(sequence)
  
      return sequence
  
imports: import numpy
  import tensorflow
  from tensorflow.python.ops import rnn
  from texar.tf.utils.shapes import mask_sequences
label_desc: Use image node_id 1 for calling a global function with example usage: mask_and_reduce(sequence, sequence_length, rank, average_across_batch, average_across_timesteps, average_across_remaining, sum_over_batch, sum_over_timesteps, sum_over_remaining, dtype, time_major) and returns: sequence