code: def aggregate(loss, weights=None, mode="mean"):
      """Aggregates an element- or item-wise loss to a scalar loss.
  
      Parameters
      ----------
      loss : Theano tensor
          The loss expression to aggregate.
      weights : Theano tensor, optional
          The weights for each element or item, must be broadcastable to
          the same shape as `loss` if given. If omitted, all elements will
          be weighted the same.
      mode : {'mean', 'sum', 'normalized_sum'}
          Whether to aggregate by averaging, by summing or by summing and
          dividing by the total weights (which requires `weights` to be given).
  
      Returns
      -------
      Theano scalar
          A scalar loss expression suitable for differentiation.
  
      Notes
      -----
      By supplying binary weights (i.e., only using values 0 and 1), this
      function can also be used for masking out particular entries in the
      loss expression. Note that masked entries still need to be valid
      values, not-a-numbers (NaNs) will propagate through.
  
      When applied to batch-wise loss expressions, setting `mode` to
      ``'normalized_sum'`` ensures that the loss per batch is of a similar
      magnitude, independent of associated weights. However, it means that
      a given data point contributes more to the loss when it shares a batch
      with low-weighted or masked data points than with high-weighted ones.
      """
      if weights is not None:
          loss = loss * weights
      if mode == "mean":
          return loss.mean()
      elif mode == "sum":
          return loss.sum()
      elif mode == "normalized_sum":
          if weights is None:
              raise ValueError(
                  "require weights for mode='normalized_sum'"
              )
          return loss.sum() / weights.sum()
      else:
          raise ValueError(
              "mode must be 'mean', 'sum' or 'normalized_sum', "
              "got %r" % mode
          )
  
file_path: Lasagne/lasagne/objectives.py
imports: import theano.tensor
  from .utils import as_theano_expression
label_desc: Use image node_id 5 for calling a global function with example usage: aggregate(loss, weights, mode) and returns: loss, loss, unknown