losses_impl.py revision ace2aa7b6f1381163d8187f1bf78b393cef4701f
12d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
22d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen#
32d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# Licensed under the Apache License, Version 2.0 (the "License");
42d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# you may not use this file except in compliance with the License.
52d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# You may obtain a copy of the License at
62d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen#
72d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen#     http://www.apache.org/licenses/LICENSE-2.0
82d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen#
92d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# Unless required by applicable law or agreed to in writing, software
102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# distributed under the License is distributed on an "AS IS" BASIS,
112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# See the License for the specific language governing permissions and
132d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# limitations under the License.
142d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen# ==============================================================================
152d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen"""Implementation of Loss operations for use in neural networks."""
162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyenfrom __future__ import absolute_import
182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyenfrom __future__ import division
192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyenfrom __future__ import print_function
202d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
212d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyenfrom tensorflow.python.framework import ops
222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyenfrom tensorflow.python.ops import array_ops
2391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlowerfrom tensorflow.python.ops import confusion_matrix
2491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlowerfrom tensorflow.python.ops import control_flow_ops
252d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyenfrom tensorflow.python.ops import math_ops
262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyenfrom tensorflow.python.ops import nn
272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyenfrom tensorflow.python.ops import nn_ops
28c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlowerfrom tensorflow.python.ops import weights_broadcast_ops
292d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyenfrom tensorflow.python.ops.losses import util
302d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
312d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
32ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower# TODO(ptucker): Per-example? Divided by batch_size? Divided by sum of weights?
33ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlowerclass Reduction(object):
34ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  """Types of loss reduction."""
352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
36ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  # Batch sum of weighted losses.
37ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  WEIGHTED_SUM = "weighted_sum"
382d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
39ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  # `WEIGHTED_SUM` divided by number of non-zero weights.
40ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  WEIGHTED_SUM_BY_NONZERO_WEIGHTS = "weighted_sum_by_nonzero_weights"
41ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower
42ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  @classmethod
43ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  def all(cls):
44ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return (cls.WEIGHTED_SUM, cls.WEIGHTED_SUM_BY_NONZERO_WEIGHTS)
45ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower
46ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  @classmethod
47ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  def validate(cls, key):
48ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    if key not in cls.all():
49ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      raise ValueError("Invalid ReductionKey %s." % key)
502d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
512d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
522d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef _safe_div(numerator, denominator, name="value"):
532d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Computes a safe divide which returns 0 if the denominator is zero.
542d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
552d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Note that the function contains an additional conditional check that is
562d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  necessary for avoiding situations where the loss is zero causing NaNs to
572d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  creep into the gradient computation.
582d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
592d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
602d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    numerator: An arbitrary `Tensor`.
612d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    denominator: `Tensor` whose shape matches `numerator` and whose values are
622d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      assumed to be non-negative.
632d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    name: An optional name for the returned op.
642d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
652d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
662d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    The element-wise value of the numerator divided by the denominator.
672d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
682d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  return array_ops.where(
692d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      math_ops.greater(denominator, 0),
702d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      math_ops.div(numerator, array_ops.where(
712d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen          math_ops.equal(denominator, 0),
722d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen          array_ops.ones_like(denominator), denominator)),
732d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      array_ops.zeros_like(numerator),
742d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      name=name)
752d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
772d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef _safe_mean(losses, num_present):
782d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Computes a safe mean of the losses.
792d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
802d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
812d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses: `Tensor` whose elements contain individual loss measurements.
822d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    num_present: The number of measurable elements in `losses`.
832d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
842d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
852d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    A scalar representing the mean of `losses`. If `num_present` is zero,
862d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      then zero is returned.
872d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
882d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  total_loss = math_ops.reduce_sum(losses)
892d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  return _safe_div(total_loss, num_present)
902d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
912d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
922d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef _num_present(losses, weights, per_batch=False):
932d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Computes the number of elements in the loss function induced by `weights`.
942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
952d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  A given weights tensor induces different numbers of usable elements in the
962d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `losses` tensor. The `weights` tensor is broadcast across `losses` for all
972d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  possible dimensions. For example, if `losses` is a tensor of dimension
982d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `[4, 5, 6, 3]` and `weights` is a tensor of shape `[4, 5]`, then `weights` is,
992d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  in effect, tiled to match the shape of `losses`. Following this effective
1002d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  tile, the total number of present elements is the number of non-zero weights.
1012d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1022d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
1032d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses: `Tensor` of shape `[batch_size, d1, ... dN]`.
1042d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    weights: `Tensor` of shape `[]`, `[batch_size]` or
1052d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `[batch_size, d1, ... dK]`, where K < N.
1062d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    per_batch: Whether to return the number of elements per batch or as a sum
1072d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      total.
1082d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1092d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
1102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    The number of present (non-zero) elements in the losses tensor. If
1112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `per_batch` is `True`, the value is returned as a tensor of size
1122d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `[batch_size]`. Otherwise, a single scalar tensor is returned.
1132d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
114c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower  with ops.name_scope(None, "num_present", (losses, weights)) as scope:
115c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    weights = math_ops.to_float(weights)
116c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    present = array_ops.where(
117c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        math_ops.equal(weights, 0.0),
118c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        array_ops.zeros_like(weights),
119c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        array_ops.ones_like(weights))
120c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    present = weights_broadcast_ops.broadcast_weights(present, losses)
121c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    if per_batch:
122c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      return math_ops.reduce_sum(
123c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          present, axis=math_ops.range(1, array_ops.rank(present)),
124c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          keep_dims=True, name=scope)
125c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    return math_ops.reduce_sum(present, name=scope)
1262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1282d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef compute_weighted_loss(
129ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    losses, weights=1.0, scope=None, loss_collection=ops.GraphKeys.LOSSES,
130ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction=Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS):
1312d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Computes the weighted loss.
1322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
1342d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses: `Tensor` of shape `[batch_size, d1, ... dN]`.
13591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
13691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `losses`, and must be broadcastable to `losses` (i.e., all dimensions must
13791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
1382d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: the scope for the operations performed in computing the loss.
1392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: the loss will be added to these collections.
140ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
1412d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1422d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
1432d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    A scalar `Tensor` that returns the weighted loss.
1442d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1452d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
1462d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If `weights` is `None` or the shape is not compatible with
1472d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `losses`, or if the number of dimensions (rank) of either `losses` or
1482d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `weights` is missing.
1492d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
150ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  Reduction.validate(reduction)
151c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower  with ops.name_scope(scope, "weighted_loss", (losses, weights)):
152c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    with ops.control_dependencies((
153c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        weights_broadcast_ops.assert_broadcastable(weights, losses),)):
154c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      losses = ops.convert_to_tensor(losses)
155c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      input_dtype = losses.dtype
156c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      losses = math_ops.to_float(losses)
157c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      weights = math_ops.to_float(weights)
158ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      weighted_losses = math_ops.multiply(losses, weights)
159ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      loss = math_ops.reduce_sum(weighted_losses)
160ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      if reduction == Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS:
161ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        loss = _safe_mean(loss, _num_present(losses, weights))
162ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower
163c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      # Convert the result back to the input type.
164ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      loss = math_ops.cast(loss, input_dtype)
165ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      util.add_loss(loss, loss_collection)
166ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      return loss
1672d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1682d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1692d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef absolute_difference(
1702d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels, predictions, weights=1.0, scope=None,
171ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
172ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction=Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS):
1732d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds an Absolute Difference loss to the training procedure.
1742d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1752d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided, then
1762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  the loss is simply scaled by the given value. If `weights` is a `Tensor` of
1772d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  shape `[batch_size]`, then the total loss for each sample of the batch is
1782d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  rescaled by the corresponding element in the `weights` vector. If the shape of
1792d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` matches the shape of `predictions`, then the loss of each
1802d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  measurable element of `predictions` is scaled by the corresponding value of
1812d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights`.
1822d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1832d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
1842d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor, same dimensions as 'predictions'.
1852d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: The predicted outputs.
18691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
18791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
18891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
1892d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
1902d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which this loss will be added.
191ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
1922d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1932d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
194ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
1952d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1962d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
1972d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `predictions` doesn't match that of `labels` or
1982d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      if the shape of `weights` is invalid.
1992d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
2002d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "absolute_difference",
2011055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
2022d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions = math_ops.to_float(predictions)
2032d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
2041055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    predictions.get_shape().assert_is_compatible_with(labels.get_shape())
2052d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses = math_ops.abs(math_ops.subtract(predictions, labels))
206ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
207ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
2082d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2092d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef cosine_distance(
2112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels, predictions, dim=None, weights=1.0, scope=None,
212ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
213ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction=Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS):
2142d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a cosine-distance loss to the training procedure.
2152d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Note that the function assumes that `predictions` and `labels` are already
2172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  unit-normalized.
2182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
2202d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: `Tensor` whose shape matches 'predictions'
2212d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: An arbitrary matrix.
2222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    dim: The dimension along which the cosine distance is computed.
22391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
22491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
22591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
2262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
2272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which this loss will be added.
228ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
2292d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2302d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
231ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
2322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
2342d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If `predictions` shape doesn't match `labels` shape, or
2352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `weights` is `None`.
2362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
2372d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  if dim is None:
2382d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    raise ValueError("`dim` cannot be None.")
2392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "cosine_distance_loss",
2401055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
2412d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions = math_ops.to_float(predictions)
2422d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
2431055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    predictions.get_shape().assert_is_compatible_with(labels.get_shape())
2442d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2452d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    radial_diffs = math_ops.multiply(predictions, labels)
246c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    losses = 1 - math_ops.reduce_sum(radial_diffs, axis=(dim,), keep_dims=True)
247ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
248ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
2492d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2502d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2512d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef hinge_loss(labels, logits, weights=1.0, scope=None,
252ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower               loss_collection=ops.GraphKeys.LOSSES,
253ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower               reduction=Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS):
2542d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a hinge loss to the training procedure.
2552d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2562d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
2572d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor. Its shape should match the shape of
2582d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      logits. The values of the tensor are expected to be 0.0 or 1.0.
2592d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    logits: The logits, a float tensor.
26091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
26191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
26291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
2632d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
2642d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
265ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
2662d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2672d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
268ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
2692d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2702d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
2712d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shapes of `logits` and `labels` don't match.
2722d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
2731055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower  with ops.name_scope(scope, "hinge_loss", (logits, labels)) as scope:
2741055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits = math_ops.to_float(logits)
2751055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    labels = math_ops.to_float(labels)
2762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    logits.get_shape().assert_is_compatible_with(labels.get_shape())
2772d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    # We first need to convert binary labels to -1/1 labels (as floats).
2782d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    all_ones = array_ops.ones_like(labels)
2792d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.subtract(2 * labels, all_ones)
2802d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses = nn_ops.relu(
2812d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen        math_ops.subtract(all_ones, math_ops.multiply(labels, logits)))
282ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
283ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
2842d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2852d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2862d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef log_loss(labels, predictions, weights=1.0, epsilon=1e-7, scope=None,
287ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower             loss_collection=ops.GraphKeys.LOSSES,
288ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower             reduction=Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS):
2892d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a Log Loss term to the training procedure.
2902d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2912d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided, then
2922d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  the loss is simply scaled by the given value. If `weights` is a tensor of size
2932d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  [batch_size], then the total loss for each sample of the batch is rescaled
2942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  by the corresponding element in the `weights` vector. If the shape of
2952d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` matches the shape of `predictions`, then the loss of each
2962d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  measurable element of `predictions` is scaled by the corresponding value of
2972d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights`.
2982d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2992d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
3002d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor, same dimensions as 'predictions'.
3012d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: The predicted outputs.
30291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
30391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
30491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
3052d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    epsilon: A small increment to add to avoid taking a log of zero.
3062d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
3072d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
308ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
3092d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
311ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
3122d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3132d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
3142d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `predictions` doesn't match that of `labels` or
3152d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      if the shape of `weights` is invalid.
3162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
3172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "log_loss",
3181055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
3192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions = math_ops.to_float(predictions)
3202d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
3211055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    predictions.get_shape().assert_is_compatible_with(labels.get_shape())
3222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses = -math_ops.multiply(
3232d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen        labels,
3242d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen        math_ops.log(predictions + epsilon)) - math_ops.multiply(
3252d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen            (1 - labels), math_ops.log(1 - predictions + epsilon))
326ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
327ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
3282d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3292d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
330ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower# TODO(b/37208492): Add reduction arg.
331ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlowerdef mean_pairwise_squared_error(
332ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    labels, predictions, weights=1.0, scope=None,
333ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES):
3342d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a pairwise-errors-squared loss to the training procedure.
3352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Unlike `mean_squared_error`, which is a measure of the differences between
3372d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding elements of `predictions` and `labels`,
3382d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `mean_pairwise_squared_error` is a measure of the differences between pairs of
3392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding elements of `predictions` and `labels`.
3402d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3412d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  For example, if `labels`=[a, b, c] and `predictions`=[x, y, z], there are
3422d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  three pairs of differences are summed to compute the loss:
3432d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss = [ ((a-b) - (x-y)).^2 + ((a-c) - (x-z)).^2 + ((b-c) - (y-z)).^2 ] / 3
3442d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3452d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Note that since the inputs are of shape `[batch_size, d0, ... dN]`, the
3462d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding pairs are computed within each batch sample but not across
3472d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  samples within a batch. For example, if `predictions` represents a batch of
3482d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  16 grayscale images of dimension [batch_size, 100, 200], then the set of pairs
3492d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  is drawn from each image, but not across images.
3502d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3512d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided, then
3522d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  the loss is simply scaled by the given value. If `weights` is a tensor of size
3532d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  [batch_size], then the total loss for each sample of the batch is rescaled
3542d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  by the corresponding element in the `weights` vector.
3552d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3562d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
3572d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor, whose shape must match the shape of
3582d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `predictions`.
3592d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: The predicted outputs, a tensor of size
3602d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `[batch_size, d0, .. dN]` where N+1 is the total number of dimensions in
3612d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `predictions`.
3622d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    weights: Coefficients for the loss a scalar, a tensor of shape
3632d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `[batch_size]` or a tensor whose shape matches `predictions`.
3642d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
3652d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
3662d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3672d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
368ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
3692d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3702d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
3712d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `predictions` doesn't match that of `labels` or
3722d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      if the shape of `weights` is invalid.
3732d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
3742d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "mean_pairwise_squared_error",
3751055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
376c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    weights = math_ops.to_float(weights)
3772d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
378c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    with ops.control_dependencies((
379c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        weights_broadcast_ops.assert_broadcastable(weights, labels),)):
380c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      predictions = math_ops.to_float(predictions)
381c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      predictions.get_shape().assert_is_compatible_with(labels.get_shape())
3822d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
383c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      diffs = math_ops.subtract(predictions, labels)
3842d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
385c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      reduction_indices = math_ops.range(1, array_ops.rank(diffs))
3862d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
387c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      sum_squares_diff_per_batch = math_ops.reduce_sum(
388c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          math_ops.square(diffs), reduction_indices=reduction_indices,
389c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          keep_dims=True)
390c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      num_present_per_batch = _num_present(diffs, weights, per_batch=True)
3912d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
392c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      term1 = 2.0 * _safe_div(sum_squares_diff_per_batch,
393c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower                              num_present_per_batch)
3942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
395c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      sum_diff = math_ops.reduce_sum(
396c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          diffs, reduction_indices=reduction_indices, keep_dims=True)
397c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      term2 = 2.0 * _safe_div(math_ops.square(sum_diff),
398c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower                              math_ops.square(num_present_per_batch))
3992d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
400ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      weighted_losses = math_ops.multiply(term1 - term2, weights)
401ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      loss = math_ops.reduce_sum(weighted_losses)
4022d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
403c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      mean_loss = array_ops.where(
404c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          math_ops.reduce_sum(num_present_per_batch) > 0,
405c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          loss,
406c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          array_ops.zeros_like(loss),
407c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          name="value")
408c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      util.add_loss(mean_loss, loss_collection)
409c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      return mean_loss
4102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
412ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlowerdef mean_squared_error(
413ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    labels, predictions, weights=1.0, scope=None,
414ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
415ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction=Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS):
4162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a Sum-of-Squares loss to the training procedure.
4172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided, then
4192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  the loss is simply scaled by the given value. If `weights` is a tensor of size
4202d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  [batch_size], then the total loss for each sample of the batch is rescaled
4212d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  by the corresponding element in the `weights` vector. If the shape of
4222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` matches the shape of `predictions`, then the loss of each
4232d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  measurable element of `predictions` is scaled by the corresponding value of
4242d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights`.
4252d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
4272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor, same dimensions as 'predictions'.
4282d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: The predicted outputs.
42991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
43091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
43191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
4322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
4332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
434ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
4352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
437ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
4382d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
4402d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `predictions` doesn't match that of `labels` or
4412d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      if the shape of `weights` is invalid.
4422d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
4432d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "mean_squared_error",
4441055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
4452d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions = math_ops.to_float(predictions)
4462d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
4471055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    predictions.get_shape().assert_is_compatible_with(labels.get_shape())
4482d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses = math_ops.square(math_ops.subtract(predictions, labels))
449ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
450ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
4512d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4522d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4532d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef sigmoid_cross_entropy(
4542d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    multi_class_labels, logits, weights=1.0, label_smoothing=0, scope=None,
455ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
456ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction=Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS):
4572d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Creates a cross-entropy loss using tf.nn.sigmoid_cross_entropy_with_logits.
4582d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4592d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided,
4602d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  then the loss is simply scaled by the given value. If `weights` is a
4612d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  tensor of shape `[batch_size]`, then the loss weights apply to each
4622d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding sample.
4632d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4642d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  If `label_smoothing` is nonzero, smooth the labels towards 1/2:
4652d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4662d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      new_multiclass_labels = multiclass_labels * (1 - label_smoothing)
4672d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                              + 0.5 * label_smoothing
4682d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4692d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
4702d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    multi_class_labels: `[batch_size, num_classes]` target integer labels in
4712d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `(0, 1)`.
4722d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    logits: `[batch_size, num_classes]` logits outputs of the network.
47391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
47491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
47591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
4762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    label_smoothing: If greater than `0` then smooth the labels.
4772d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
4782d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
479ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
4802d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4812d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
482ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
4832d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4842d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
4852d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `logits` doesn't match that of
4862d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `multi_class_labels` or if the shape of `weights` is invalid, or if
4872d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `weights` is None.
4882d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
4892d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "sigmoid_cross_entropy_loss",
4901055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (logits, multi_class_labels, weights)) as scope:
4911055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits = ops.convert_to_tensor(logits)
4922d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    multi_class_labels = math_ops.cast(multi_class_labels, logits.dtype)
4931055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits.get_shape().assert_is_compatible_with(multi_class_labels.get_shape())
4942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4952d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    if label_smoothing > 0:
4962d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      multi_class_labels = (multi_class_labels * (1 - label_smoothing) +
4972d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                            0.5 * label_smoothing)
4982d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
499333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke    losses = nn.sigmoid_cross_entropy_with_logits(labels=multi_class_labels,
500333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke                                                  logits=logits,
5012d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                                                  name="xentropy")
502ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
503ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
5042d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5052d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5062d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef softmax_cross_entropy(
5072d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    onehot_labels, logits, weights=1.0, label_smoothing=0, scope=None,
508ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
509ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction=Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS):
5102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Creates a cross-entropy loss using tf.nn.softmax_cross_entropy_with_logits.
5112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5122d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided,
5132d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  then the loss is simply scaled by the given value. If `weights` is a
5142d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  tensor of shape `[batch_size]`, then the loss weights apply to each
5152d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding sample.
5162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  If `label_smoothing` is nonzero, smooth the labels towards 1/num_classes:
5182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      new_onehot_labels = onehot_labels * (1 - label_smoothing)
5192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                          + label_smoothing / num_classes
5202d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5212d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
5222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    onehot_labels: `[batch_size, num_classes]` target one-hot-encoded labels.
5232d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    logits: [batch_size, num_classes] logits outputs of the network .
52491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
52591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `onehot_labels`, and must be broadcastable to `onehot_labels` (i.e., all
52691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      dimensions must be either `1`, or the same as the corresponding `losses`
52791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      dimension).
5282d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    label_smoothing: If greater than 0 then smooth the labels.
5292d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: the scope for the operations performed in computing the loss.
5302d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
531ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
5322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
534ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
5352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
5372d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `logits` doesn't match that of `onehot_labels`
5382d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      or if the shape of `weights` is invalid or if `weights` is None.
5392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
5402d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "softmax_cross_entropy_loss",
5411055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (logits, onehot_labels, weights)) as scope:
5421055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits = ops.convert_to_tensor(logits)
5432d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    onehot_labels = math_ops.cast(onehot_labels, logits.dtype)
5441055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits.get_shape().assert_is_compatible_with(onehot_labels.get_shape())
5452d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5462d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    if label_smoothing > 0:
5472d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      num_classes = math_ops.cast(
5482d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen          array_ops.shape(onehot_labels)[1], logits.dtype)
5492d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      smooth_positives = 1.0 - label_smoothing
5502d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      smooth_negatives = label_smoothing / num_classes
5512d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      onehot_labels = onehot_labels * smooth_positives + smooth_negatives
5522d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
553333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke    losses = nn.softmax_cross_entropy_with_logits(labels=onehot_labels,
554333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke                                                  logits=logits,
5552d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                                                  name="xentropy")
556ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
557ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
5582d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5592d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
56091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower# TODO(ptucker): Merge this with similar method in metrics_impl.
56191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlowerdef _remove_squeezable_dimensions(
56291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels, predictions, weights=None, expected_rank_diff=0):
56391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  """Internal version of _remove_squeezable_dimensions which handles weights.
56491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
56591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  Squeezes `predictions` and `labels` if their ranks differ from expected by
56691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  exactly 1.
56791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  Squeezes `weights` if its rank is 1 more than the new rank of `predictions`
56891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
56991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  This will use static shape if available. Otherwise, it will add graph
57091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  operations, which could result in a performance hit.
57191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
57291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  Args:
57391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels: Label values, a `Tensor` whose dimensions match `predictions`.
57491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    predictions: Predicted values, a `Tensor` of arbitrary dimensions.
57591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional weight `Tensor`. It will be squeezed if it's not scalar,
57691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      and its rank is 1 more than the new rank of `labels`.
57791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    expected_rank_diff: Expected result of `rank(predictions) - rank(labels)`.
57891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
57991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  Returns:
58091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    Tuple of `predictions`, `labels` and `weights`, possibly with the last
58191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    dimension squeezed.
58291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  """
58391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  labels, predictions = confusion_matrix.remove_squeezable_dimensions(
58491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      labels, predictions, expected_rank_diff=expected_rank_diff)
58591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
58691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  if weights is not None:
58791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights = ops.convert_to_tensor(weights)
58891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels_rank = labels.get_shape().ndims
58991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights_shape = weights.get_shape()
59091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights_rank = weights_shape.ndims
59191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
59291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    if (labels_rank is not None) and (weights_rank is not None):
59391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      # Use static rank.
59491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      rank_diff = weights_rank - labels_rank
59591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      if rank_diff == 1:
59691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower        weights = array_ops.squeeze(weights, [-1])
59791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      return labels, predictions, weights
59891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
59991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    # Use dynamic rank.
60091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    rank_diff = array_ops.rank(weights) - array_ops.rank(labels)
60191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    if (weights_rank is None) or (
60291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower        weights_shape.dims[-1].is_compatible_with(1)):
60391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      weights = control_flow_ops.cond(
60491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower          math_ops.equal(1, rank_diff),
60591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower          lambda: array_ops.squeeze(weights, [-1]),
60691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower          lambda: weights)
60791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
60891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  return labels, predictions, weights
60991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
61091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
611ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlowerdef sparse_softmax_cross_entropy(
612ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    labels, logits, weights=1.0, scope=None,
613ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
614ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction=Reduction.WEIGHTED_SUM_BY_NONZERO_WEIGHTS):
6152d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Cross-entropy loss using `tf.nn.sparse_softmax_cross_entropy_with_logits`.
6162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided,
6182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  then the loss is simply scaled by the given value. If `weights` is a
6192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  tensor of shape [`batch_size`], then the loss weights apply to each
6202d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding sample.
6212d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
62391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels: `Tensor` of shape `[d_0, d_1, ..., d_{r-1}]` (where `r` is rank of
62491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels` and result) and dtype `int32` or `int64`. Each entry in `labels`
62591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      must be an index in `[0, num_classes)`. Other values will raise an
62691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      exception when this op is run on CPU, and return `NaN` for corresponding
62791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      loss and gradient rows on GPU.
62891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    logits: Unscaled log probabilities of shape
62991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `[d_0, d_1, ..., d_{r-1}, num_classes]` and dtype `float32` or `float64`.
63091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Coefficients for the loss. This must be scalar or of same rank as
63191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`
6322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: the scope for the operations performed in computing the loss.
6332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
634ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
6352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
637ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
6382d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
6402d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shapes of logits, labels, and weight are incompatible, or
6412d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      if `weights` is None.
6422d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
6432d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "sparse_softmax_cross_entropy_loss",
644c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower                      (logits, labels, weights)) as scope:
64591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    # As documented above in Args, labels contain class IDs and logits contains
64691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    # 1 probability per class ID, so we expect rank(logits) - rank(labels) == 1;
64791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    # therefore, expected_rank_diff=1.
64891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels, logits, weights = _remove_squeezable_dimensions(
64991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower        labels, logits, weights, expected_rank_diff=1)
650333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke    losses = nn.sparse_softmax_cross_entropy_with_logits(labels=labels,
651333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke                                                         logits=logits,
6522d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                                                         name="xentropy")
653ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
654ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
655