parameters: (sample_losses,
                     stochastic_tensors=None,
                     name="SurrogateLoss")
docstring: Surrogate loss for stochastic graphs.
  
    This function will call `loss_fn` on each `StochasticTensor`
    upstream of `sample_losses`, passing the losses that it influenced.
  
    Note that currently `surrogate_loss` does not work with `StochasticTensor`s
    instantiated in `while_loop`s or other control structures.
  
    Args:
      sample_losses: a list or tuple of final losses. Each loss should be per
        example in the batch (and possibly per sample); that is, it should have
        dimensionality of 1 or greater. All losses should have the same shape.
      stochastic_tensors: a list of `StochasticTensor`s to add loss terms for.
        If None, defaults to all `StochasticTensor`s in the graph upstream of
        the `Tensor`s in `sample_losses`.
      name: the name with which to prepend created ops.
  
    Returns:
      `Tensor` loss, which is the sum of `sample_losses` and the
      `loss_fn`s returned by the `StochasticTensor`s.
  
    Raises:
      TypeError: if `sample_losses` is not a list or tuple, or if its elements
        are not `Tensor`s.
      ValueError: if any loss in `sample_losses` does not have dimensionality 1
        or greater.
docstring_summary: Surrogate loss for stochastic graphs.
docstring_tokens: Surrogate loss for stochastic graphs .
function: def surrogate_loss(sample_losses,
                     stochastic_tensors=None,
                     name="SurrogateLoss"):
    """Surrogate loss for stochastic graphs.
  
    This function will call `loss_fn` on each `StochasticTensor`
    upstream of `sample_losses`, passing the losses that it influenced.
  
    Note that currently `surrogate_loss` does not work with `StochasticTensor`s
    instantiated in `while_loop`s or other control structures.
  
    Args:
      sample_losses: a list or tuple of final losses. Each loss should be per
        example in the batch (and possibly per sample); that is, it should have
        dimensionality of 1 or greater. All losses should have the same shape.
      stochastic_tensors: a list of `StochasticTensor`s to add loss terms for.
        If None, defaults to all `StochasticTensor`s in the graph upstream of
        the `Tensor`s in `sample_losses`.
      name: the name with which to prepend created ops.
  
    Returns:
      `Tensor` loss, which is the sum of `sample_losses` and the
      `loss_fn`s returned by the `StochasticTensor`s.
  
    Raises:
      TypeError: if `sample_losses` is not a list or tuple, or if its elements
        are not `Tensor`s.
      ValueError: if any loss in `sample_losses` does not have dimensionality 1
        or greater.
    """
    with ops.op_scope(sample_losses, name):
      fixed_losses = []
      if not isinstance(sample_losses, (list, tuple)):
        raise TypeError("sample_losses must be a list or tuple")
      for loss in sample_losses:
        if not isinstance(loss, ops.Tensor):
          raise TypeError("loss is not a Tensor: %s" % loss)
        ndims = loss.get_shape().ndims
        if not (ndims is not None and ndims >= 1):
          raise ValueError("loss must have dimensionality 1 or greater: %s" %
                           loss)
        fixed_losses.append(array_ops.stop_gradient(loss))
  
      stoch_dependencies_map = _stochastic_dependencies_map(
          fixed_losses, stochastic_tensors=stochastic_tensors)
      if not stoch_dependencies_map:
        logging.warn(
            "No collection of Stochastic Tensors found for current graph.")
        return math_ops.add_n(sample_losses)
  
      # Iterate through all of the stochastic dependencies, adding
      # surrogate terms where necessary.
      sample_losses = [ops.convert_to_tensor(loss) for loss in sample_losses]
      loss_terms = sample_losses
      for (stoch_node, dependent_losses) in stoch_dependencies_map.items():
        loss_term = stoch_node.loss(list(dependent_losses))
        if loss_term is not None:
          loss_terms.append(loss_term)
  
      return math_ops.add_n(loss_terms)
function_tokens: def surrogate_loss ( sample_losses , stochastic_tensors = None , name = "SurrogateLoss" ) : with ops . op_scope ( sample_losses , name ) : fixed_losses = [ ] if not isinstance ( sample_losses , ( list , tuple ) ) : raise TypeError ( "sample_losses must be a list or tuple" ) for loss in sample_losses : if not isinstance ( loss , ops . Tensor ) : raise TypeError ( "loss is not a Tensor: %s" % loss ) ndims = loss . get_shape ( ) . ndims if not ( ndims is not None and ndims >= 1 ) : raise ValueError ( "loss must have dimensionality 1 or greater: %s" % loss ) fixed_losses . append ( array_ops . stop_gradient ( loss ) ) stoch_dependencies_map = _stochastic_dependencies_map ( fixed_losses , stochastic_tensors = stochastic_tensors ) if not stoch_dependencies_map : logging . warn ( "No collection of Stochastic Tensors found for current graph." ) return math_ops . add_n ( sample_losses ) # Iterate through all of the stochastic dependencies, adding # surrogate terms where necessary. sample_losses = [ ops . convert_to_tensor ( loss ) for loss in sample_losses ] loss_terms = sample_losses for ( stoch_node , dependent_losses ) in stoch_dependencies_map . items ( ) : loss_term = stoch_node . loss ( list ( dependent_losses ) ) if loss_term is not None : loss_terms . append ( loss_term ) return math_ops . add_n ( loss_terms )