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
30355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steinerfrom tensorflow.python.util.deprecation import deprecated_args
31adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna Rfrom tensorflow.python.util.tf_export import tf_export
322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
34adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.Reduction")
35ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlowerclass Reduction(object):
36cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  """Types of loss reduction.
37cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower
38cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  Contains the following values:
39cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  `NONE`: Un-reduced weighted losses with the same shape as input.
40cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  `SUM`: Scalar sum of weighted losses.
41cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  `MEAN`: Scalar `SUM` divided by sum of weights.
42cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  `SUM_OVER_BATCH_SIZE`: Scalar `SUM` divided by number of elements in losses.
43cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  `SUM_OVER_NONZERO_WEIGHTS`: Scalar `SUM` divided by number of non-zero
44cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower     weights.
45cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  `SUM_BY_NONZERO_WEIGHTS`: Same as `SUM_OVER_NONZERO_WEIGHTS`.
46cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  """
472d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4820a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower  NONE = "none"
492d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5020a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower  SUM = "weighted_sum"
5120a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower
5220a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower  MEAN = "weighted_mean"
5320a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower
54cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  SUM_OVER_BATCH_SIZE = "weighted_sum_over_batch_size"
55cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower
5620a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower  SUM_BY_NONZERO_WEIGHTS = "weighted_sum_by_nonzero_weights"
57cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  SUM_OVER_NONZERO_WEIGHTS = SUM_BY_NONZERO_WEIGHTS
58ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower
59ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  @classmethod
60ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  def all(cls):
6120a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    return (
6220a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower        cls.NONE,
6320a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower        cls.SUM,
6420a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower        cls.MEAN,
65cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower        cls.SUM_OVER_BATCH_SIZE,
66cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower        cls.SUM_OVER_NONZERO_WEIGHTS,
6720a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower        cls.SUM_BY_NONZERO_WEIGHTS)
68ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower
69ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  @classmethod
70ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  def validate(cls, key):
71ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    if key not in cls.all():
72ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      raise ValueError("Invalid ReductionKey %s." % key)
732d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
742d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
752d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef _safe_div(numerator, denominator, name="value"):
762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Computes a safe divide which returns 0 if the denominator is zero.
772d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
782d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Note that the function contains an additional conditional check that is
792d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  necessary for avoiding situations where the loss is zero causing NaNs to
802d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  creep into the gradient computation.
812d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
822d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
832d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    numerator: An arbitrary `Tensor`.
842d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    denominator: `Tensor` whose shape matches `numerator` and whose values are
852d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      assumed to be non-negative.
862d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    name: An optional name for the returned op.
872d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
882d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
892d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    The element-wise value of the numerator divided by the denominator.
902d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
912d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  return array_ops.where(
922d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      math_ops.greater(denominator, 0),
932d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      math_ops.div(numerator, array_ops.where(
942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen          math_ops.equal(denominator, 0),
952d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen          array_ops.ones_like(denominator), denominator)),
962d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      array_ops.zeros_like(numerator),
972d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      name=name)
982d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
992d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1002d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef _safe_mean(losses, num_present):
1012d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Computes a safe mean of the losses.
1022d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1032d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
1042d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses: `Tensor` whose elements contain individual loss measurements.
1052d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    num_present: The number of measurable elements in `losses`.
1062d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1072d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
1082d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    A scalar representing the mean of `losses`. If `num_present` is zero,
1092d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      then zero is returned.
1102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
1112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  total_loss = math_ops.reduce_sum(losses)
1122d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  return _safe_div(total_loss, num_present)
1132d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1142d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1152d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef _num_present(losses, weights, per_batch=False):
1162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Computes the number of elements in the loss function induced by `weights`.
1172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  A given weights tensor induces different numbers of usable elements in the
1192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `losses` tensor. The `weights` tensor is broadcast across `losses` for all
1202d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  possible dimensions. For example, if `losses` is a tensor of dimension
1212d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `[4, 5, 6, 3]` and `weights` is a tensor of shape `[4, 5]`, then `weights` is,
1222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  in effect, tiled to match the shape of `losses`. Following this effective
1232d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  tile, the total number of present elements is the number of non-zero weights.
1242d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1252d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
1262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses: `Tensor` of shape `[batch_size, d1, ... dN]`.
1272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    weights: `Tensor` of shape `[]`, `[batch_size]` or
1282d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `[batch_size, d1, ... dK]`, where K < N.
1292d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    per_batch: Whether to return the number of elements per batch or as a sum
1302d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      total.
1312d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
1332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    The number of present (non-zero) elements in the losses tensor. If
1342d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `per_batch` is `True`, the value is returned as a tensor of size
1352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `[batch_size]`. Otherwise, a single scalar tensor is returned.
1362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
137c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower  with ops.name_scope(None, "num_present", (losses, weights)) as scope:
138c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    weights = math_ops.to_float(weights)
139c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    present = array_ops.where(
140c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        math_ops.equal(weights, 0.0),
141c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        array_ops.zeros_like(weights),
142c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        array_ops.ones_like(weights))
143c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    present = weights_broadcast_ops.broadcast_weights(present, losses)
144c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    if per_batch:
145c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      return math_ops.reduce_sum(
146c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          present, axis=math_ops.range(1, array_ops.rank(present)),
147b9f548d041ba8d66102c6d195e645051f1bee52fYukun Chen          keepdims=True, name=scope)
148c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    return math_ops.reduce_sum(present, name=scope)
1492d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1502d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
151cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlowerdef _num_elements(losses):
152cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  """Computes the number of elements in `losses` tensor."""
153cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower  with ops.name_scope(None, "num_elements", values=[losses]) as scope:
1547092e612c1ec51b4aeafe9201706331dd4c3199eA. Unique TensorFlower    return math_ops.cast(array_ops.size(losses, name=scope), dtype=losses.dtype)
155cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower
156cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower
157adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.compute_weighted_loss")
1582d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef compute_weighted_loss(
159ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    losses, weights=1.0, scope=None, loss_collection=ops.GraphKeys.LOSSES,
16020a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
1612d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Computes the weighted loss.
1622d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1632d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
1642d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses: `Tensor` of shape `[batch_size, d1, ... dN]`.
16591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
16691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `losses`, and must be broadcastable to `losses` (i.e., all dimensions must
16791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
1682d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: the scope for the operations performed in computing the loss.
1692d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: the loss will be added to these collections.
170ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
1712d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1722d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
17320a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss `Tensor` of the same type as `losses`. If `reduction` is
17420a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    `NONE`, this has the same shape as `losses`; otherwise, it is scalar.
1752d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
1762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
1772d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If `weights` is `None` or the shape is not compatible with
1782d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `losses`, or if the number of dimensions (rank) of either `losses` or
1792d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `weights` is missing.
18020765b3e1ae3b718699592c98aa9805cb874b6d1Patrick Nguyen
18120765b3e1ae3b718699592c98aa9805cb874b6d1Patrick Nguyen  Note:
18220765b3e1ae3b718699592c98aa9805cb874b6d1Patrick Nguyen    When calculating the gradient of a weighted loss contributions from
18320765b3e1ae3b718699592c98aa9805cb874b6d1Patrick Nguyen    both `losses` and `weights` are considered. If your `weights` depend
18420765b3e1ae3b718699592c98aa9805cb874b6d1Patrick Nguyen    on some model parameters but you do not want this to affect the loss
18520765b3e1ae3b718699592c98aa9805cb874b6d1Patrick Nguyen    gradient, you need to apply @{tf.stop_gradient} to `weights` before
18620765b3e1ae3b718699592c98aa9805cb874b6d1Patrick Nguyen    passing them to `compute_weighted_loss`.
1872d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
188ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower  Reduction.validate(reduction)
189c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower  with ops.name_scope(scope, "weighted_loss", (losses, weights)):
190c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    with ops.control_dependencies((
191c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        weights_broadcast_ops.assert_broadcastable(weights, losses),)):
192c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      losses = ops.convert_to_tensor(losses)
193c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      input_dtype = losses.dtype
194c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      losses = math_ops.to_float(losses)
195c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      weights = math_ops.to_float(weights)
196ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      weighted_losses = math_ops.multiply(losses, weights)
19720a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower      if reduction == Reduction.NONE:
19820a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower        loss = weighted_losses
19920a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower      else:
20020a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower        loss = math_ops.reduce_sum(weighted_losses)
20120a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower        if reduction == Reduction.MEAN:
20220a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower          loss = _safe_mean(
20320a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower              loss,
20420a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower              math_ops.reduce_sum(array_ops.ones_like(losses) * weights))
205cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower        elif (reduction == Reduction.SUM_BY_NONZERO_WEIGHTS or
206cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower              reduction == Reduction.SUM_OVER_NONZERO_WEIGHTS):
20720a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower          loss = _safe_mean(loss, _num_present(losses, weights))
208cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower        elif reduction == Reduction.SUM_OVER_BATCH_SIZE:
209cd5d0c63596551bfc42dab5c488eef24a118b38bA. Unique TensorFlower          loss = _safe_mean(loss, _num_elements(losses))
210ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower
211c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      # Convert the result back to the input type.
212ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      loss = math_ops.cast(loss, input_dtype)
213ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      util.add_loss(loss, loss_collection)
214ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      return loss
2152d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
217adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.absolute_difference")
2182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef absolute_difference(
2192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels, predictions, weights=1.0, scope=None,
220ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
22120a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
2222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds an Absolute Difference loss to the training procedure.
2232d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2242d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided, then
2252d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  the loss is simply scaled by the given value. If `weights` is a `Tensor` of
2262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  shape `[batch_size]`, then the total loss for each sample of the batch is
2272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  rescaled by the corresponding element in the `weights` vector. If the shape of
2282d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` matches the shape of `predictions`, then the loss of each
2292d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  measurable element of `predictions` is scaled by the corresponding value of
2302d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights`.
2312d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
2332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor, same dimensions as 'predictions'.
2342d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: The predicted outputs.
23591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
23691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
23791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
2382d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
2392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which this loss will be added.
240ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
2412d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2422d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
24320a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
24420a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    shape as `labels`; otherwise, it is scalar.
2452d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2462d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
2470236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    ValueError: If the shape of `predictions` doesn't match that of
2480236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      `labels` or if the shape of `weights` is invalid or if `labels`
2490236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      or `predictions` is None.
2502d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
2510236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if labels is None:
2520236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("labels must not be None.")
2530236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if predictions is None:
2540236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("predictions must not be None.")
2552d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "absolute_difference",
2561055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
2572d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions = math_ops.to_float(predictions)
2582d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
2591055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    predictions.get_shape().assert_is_compatible_with(labels.get_shape())
2602d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses = math_ops.abs(math_ops.subtract(predictions, labels))
261ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
262ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
2632d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2642d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
265adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.cosine_distance")
266355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner@deprecated_args(None, "dim is deprecated, use axis instead", "dim")
2672d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef cosine_distance(
268355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner    labels, predictions, axis=None, weights=1.0, scope=None,
269ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
270355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS,
271355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner    dim=None):
2722d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a cosine-distance loss to the training procedure.
2732d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2742d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Note that the function assumes that `predictions` and `labels` are already
2752d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  unit-normalized.
2762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2772d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
2782d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: `Tensor` whose shape matches 'predictions'
2792d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: An arbitrary matrix.
280355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner    axis: The dimension along which the cosine distance is computed.
28191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
28291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
28391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
2842d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
2852d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which this loss will be added.
286ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
287355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner    dim: The old (deprecated) name for `axis`.
2882d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2892d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
29020a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
29120a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    shape as `labels`; otherwise, it is scalar.
2922d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
2932d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
2942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If `predictions` shape doesn't match `labels` shape, or
295355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner      `axis`, `labels`, `predictions` or `weights` is `None`.
2962d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
297355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner  if dim is not None:
298355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner    if axis is not None:
299355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner      raise ValueError("Cannot specify both 'axis' and 'dim'")
300355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner    axis = dim
301355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner  if axis is None and dim is None:
302355e25ebcab64e833dfc987638c3e6c79d838266Benoit Steiner    raise ValueError("You must specify 'axis'.")
3030236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if labels is None:
3040236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("labels must not be None.")
3050236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if predictions is None:
3060236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("predictions must not be None.")
3072d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "cosine_distance_loss",
3081055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
3092d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions = math_ops.to_float(predictions)
3102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
3111055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    predictions.get_shape().assert_is_compatible_with(labels.get_shape())
3122d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3132d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    radial_diffs = math_ops.multiply(predictions, labels)
314b9f548d041ba8d66102c6d195e645051f1bee52fYukun Chen    losses = 1 - math_ops.reduce_sum(radial_diffs, axis=(axis,), keepdims=True)
315ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
316ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
3172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
319adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.hinge_loss")
3202d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef hinge_loss(labels, logits, weights=1.0, scope=None,
321ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower               loss_collection=ops.GraphKeys.LOSSES,
32220a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower               reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
3232d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a hinge loss to the training procedure.
3242d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3252d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
3262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor. Its shape should match the shape of
3272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      logits. The values of the tensor are expected to be 0.0 or 1.0.
3282d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    logits: The logits, a float tensor.
32991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
33091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
33191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
3322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
3332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
334ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
3352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
33720a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
33820a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    shape as `labels`; otherwise, it is scalar.
3392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3402d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
3410236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    ValueError: If the shapes of `logits` and `labels` don't match or
3420236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      if `labels` or `logits` is None.
3432d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
3440236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if labels is None:
3450236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("labels must not be None.")
3460236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if logits is None:
3470236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("logits must not be None.")
34871fc957556bb24fbe026c665ce40db5fbbd3f210A. Unique TensorFlower  with ops.name_scope(scope, "hinge_loss", (logits, labels, weights)) as scope:
3491055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits = math_ops.to_float(logits)
3501055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    labels = math_ops.to_float(labels)
3512d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    logits.get_shape().assert_is_compatible_with(labels.get_shape())
3522d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    # We first need to convert binary labels to -1/1 labels (as floats).
3532d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    all_ones = array_ops.ones_like(labels)
3542d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.subtract(2 * labels, all_ones)
3552d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses = nn_ops.relu(
3562d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen        math_ops.subtract(all_ones, math_ops.multiply(labels, logits)))
357ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
358ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
3592d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
3602d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
361adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.huber_loss")
3624b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarramadef huber_loss(labels, predictions, weights=1.0, delta=1.0, scope=None,
3634b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama               loss_collection=ops.GraphKeys.LOSSES,
36420a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower               reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
3654b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  """Adds a Huber Loss term to the training procedure.
3664b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
3674b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  For each value x in `error=labels-predictions`, the following is calculated:
3684b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
3694b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  ```
3704b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    0.5 * x^2                  if |x| <= d
3714b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    0.5 * d^2 + d * (|x| - d)  if |x| > d
3724b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  ```
3734b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
3744b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  where d is `delta`.
3754b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
3764b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  See: https://en.wikipedia.org/wiki/Huber_loss
3774b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
3784b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  `weights` acts as a coefficient for the loss. If a scalar is provided, then
3794b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  the loss is simply scaled by the given value. If `weights` is a tensor of size
3808330cce0556893a7236d87a09e7c495c55197de0Christopher Yeh  `[batch_size]`, then the total loss for each sample of the batch is rescaled
3814b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  by the corresponding element in the `weights` vector. If the shape of
3824b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  `weights` matches the shape of `predictions`, then the loss of each
3834b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  measurable element of `predictions` is scaled by the corresponding value of
3844b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  `weights`.
3854b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
3864b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  Args:
3874b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    labels: The ground truth output tensor, same dimensions as 'predictions'.
3884b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    predictions: The predicted outputs.
3894b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    weights: Optional `Tensor` whose rank is either 0, or the same rank as
3904b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
3914b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama      be either `1`, or the same as the corresponding `losses` dimension).
3924b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    delta: `float`, the point where the huber loss function
3934b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama      changes from a quadratic to linear.
3944b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    scope: The scope for the operations performed in computing the loss.
3954b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    loss_collection: collection to which the loss will be added.
3964b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    reduction: Type of reduction to apply to loss.
3974b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
3984b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  Returns:
39920a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
40020a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    shape as `labels`; otherwise, it is scalar.
4014b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
4024b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  Raises:
4034b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    ValueError: If the shape of `predictions` doesn't match that of `labels` or
4040236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      if the shape of `weights` is invalid.  Also if `labels` or
4050236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower     `predictions` is None.
4064b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  """
4070236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if labels is None:
4080236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("labels must not be None.")
4090236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if predictions is None:
4100236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("predictions must not be None.")
4114b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama  with ops.name_scope(scope, "huber_loss",
4124b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama                      (predictions, labels, weights)) as scope:
4134b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    predictions = math_ops.to_float(predictions)
4144b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    labels = math_ops.to_float(labels)
4154b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    predictions.get_shape().assert_is_compatible_with(labels.get_shape())
4164b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    error = math_ops.subtract(predictions, labels)
4174b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    abs_error = math_ops.abs(error)
4184b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    quadratic = math_ops.minimum(abs_error, delta)
4194b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    # The following expression is the same in value as
4204b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    # tf.maximum(abs_error - delta, 0), but importantly the gradient for the
4214b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    # expression when abs_error == delta is 0 (for tf.maximum it would be 1).
4224b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    # This is necessary to avoid doubling the gradient, since there is already a
4234b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    # nonzero contribution to the gradient from the quadratic term.
4244b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    linear = (abs_error - quadratic)
42526114e0dc164a797fcebba149b61a1dde89c3a09Yuefeng Zhou    losses = 0.5 * quadratic * quadratic + delta * linear
4264b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama    return compute_weighted_loss(
4274b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama        losses, weights, scope, loss_collection, reduction=reduction)
4284b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
4294b046fb5aac1c204c9a48ba92091be97eee477c7Sergio Guadarrama
430adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.log_loss")
4312d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef log_loss(labels, predictions, weights=1.0, epsilon=1e-7, scope=None,
432ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower             loss_collection=ops.GraphKeys.LOSSES,
43320a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower             reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
4342d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a Log Loss term to the training procedure.
4352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided, then
4372d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  the loss is simply scaled by the given value. If `weights` is a tensor of size
4388330cce0556893a7236d87a09e7c495c55197de0Christopher Yeh  `[batch_size]`, then the total loss for each sample of the batch is rescaled
4392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  by the corresponding element in the `weights` vector. If the shape of
4402d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` matches the shape of `predictions`, then the loss of each
4412d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  measurable element of `predictions` is scaled by the corresponding value of
4422d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights`.
4432d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4442d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
4452d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor, same dimensions as 'predictions'.
4462d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: The predicted outputs.
44791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
44891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
44991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
4502d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    epsilon: A small increment to add to avoid taking a log of zero.
4512d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
4522d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
453ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
4542d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4552d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
45620a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
45720a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    shape as `labels`; otherwise, it is scalar.
4582d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4592d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
4602d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `predictions` doesn't match that of `labels` or
4610236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      if the shape of `weights` is invalid.  Also if `labels` or `predictions`
4620236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      is None.
4632d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
4640236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if labels is None:
4650236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("labels must not be None.")
4660236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if predictions is None:
4670236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("predictions must not be None.")
4682d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "log_loss",
4691055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
4702d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions = math_ops.to_float(predictions)
4712d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
4721055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    predictions.get_shape().assert_is_compatible_with(labels.get_shape())
4732d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    losses = -math_ops.multiply(
4742d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen        labels,
4752d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen        math_ops.log(predictions + epsilon)) - math_ops.multiply(
4762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen            (1 - labels), math_ops.log(1 - predictions + epsilon))
477ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
478ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
4792d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4802d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
481ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower# TODO(b/37208492): Add reduction arg.
482adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.mean_pairwise_squared_error")
483ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlowerdef mean_pairwise_squared_error(
484ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    labels, predictions, weights=1.0, scope=None,
485ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES):
4862d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a pairwise-errors-squared loss to the training procedure.
4872d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4882d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Unlike `mean_squared_error`, which is a measure of the differences between
4892d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding elements of `predictions` and `labels`,
4902d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `mean_pairwise_squared_error` is a measure of the differences between pairs of
4912d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding elements of `predictions` and `labels`.
4922d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4932d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  For example, if `labels`=[a, b, c] and `predictions`=[x, y, z], there are
4942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  three pairs of differences are summed to compute the loss:
4952d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss = [ ((a-b) - (x-y)).^2 + ((a-c) - (x-z)).^2 + ((b-c) - (y-z)).^2 ] / 3
4962d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
4972d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Note that since the inputs are of shape `[batch_size, d0, ... dN]`, the
4982d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding pairs are computed within each batch sample but not across
4992d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  samples within a batch. For example, if `predictions` represents a batch of
5002d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  16 grayscale images of dimension [batch_size, 100, 200], then the set of pairs
5012d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  is drawn from each image, but not across images.
5022d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5032d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided, then
5042d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  the loss is simply scaled by the given value. If `weights` is a tensor of size
5058330cce0556893a7236d87a09e7c495c55197de0Christopher Yeh  `[batch_size]`, then the total loss for each sample of the batch is rescaled
5062d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  by the corresponding element in the `weights` vector.
5072d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5082d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
5092d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor, whose shape must match the shape of
5102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `predictions`.
5112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: The predicted outputs, a tensor of size
5122d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `[batch_size, d0, .. dN]` where N+1 is the total number of dimensions in
5132d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `predictions`.
5142d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    weights: Coefficients for the loss a scalar, a tensor of shape
5152d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `[batch_size]` or a tensor whose shape matches `predictions`.
5162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
5172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
5182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
520ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    A scalar `Tensor` that returns the weighted loss.
5212d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
5232d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `predictions` doesn't match that of `labels` or
524d9f93c42a50b1f1401d9c186eac0ae8dc9093c3bJianwei Xie      if the shape of `weights` is invalid.  Also if `labels` or `predictions`
5250236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      is None.
5262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
5270236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if labels is None:
5280236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("labels must not be None.")
5290236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if predictions is None:
5300236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("predictions must not be None.")
5312d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "mean_pairwise_squared_error",
5321055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
533c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    weights = math_ops.to_float(weights)
5342d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
535c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower    with ops.control_dependencies((
536c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower        weights_broadcast_ops.assert_broadcastable(weights, labels),)):
537c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      predictions = math_ops.to_float(predictions)
538c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      predictions.get_shape().assert_is_compatible_with(labels.get_shape())
5392d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
540c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      diffs = math_ops.subtract(predictions, labels)
5412d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
542c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      reduction_indices = math_ops.range(1, array_ops.rank(diffs))
5432d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
544c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      sum_squares_diff_per_batch = math_ops.reduce_sum(
545c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          math_ops.square(diffs), reduction_indices=reduction_indices,
546b9f548d041ba8d66102c6d195e645051f1bee52fYukun Chen          keepdims=True)
547c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      num_present_per_batch = _num_present(diffs, weights, per_batch=True)
5482d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
549c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      term1 = 2.0 * _safe_div(sum_squares_diff_per_batch,
550d90054e7c0f41f4bab81df0548577a73b939a87aMichael Case                              num_present_per_batch - 1)
5512d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
552c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      sum_diff = math_ops.reduce_sum(
553b9f548d041ba8d66102c6d195e645051f1bee52fYukun Chen          diffs, reduction_indices=reduction_indices, keepdims=True)
554b4cf62578ecbff2185f81987d1c41f3cd5cb9e19Gunhan Gulsoy      term2 = 2.0 * _safe_div(
555b4cf62578ecbff2185f81987d1c41f3cd5cb9e19Gunhan Gulsoy          math_ops.square(sum_diff),
556d90054e7c0f41f4bab81df0548577a73b939a87aMichael Case          math_ops.multiply(num_present_per_batch, num_present_per_batch - 1))
5572d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
558ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      weighted_losses = math_ops.multiply(term1 - term2, weights)
559ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower      loss = math_ops.reduce_sum(weighted_losses)
5602d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
561c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      mean_loss = array_ops.where(
562c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          math_ops.reduce_sum(num_present_per_batch) > 0,
563c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          loss,
564c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          array_ops.zeros_like(loss),
565c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower          name="value")
566c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      util.add_loss(mean_loss, loss_collection)
567c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower      return mean_loss
5682d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5692d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
570adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.mean_squared_error")
571ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlowerdef mean_squared_error(
572ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    labels, predictions, weights=1.0, scope=None,
573ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
57420a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
5752d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Adds a Sum-of-Squares loss to the training procedure.
5762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5772d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided, then
5782d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  the loss is simply scaled by the given value. If `weights` is a tensor of size
5798330cce0556893a7236d87a09e7c495c55197de0Christopher Yeh  `[batch_size]`, then the total loss for each sample of the batch is rescaled
5802d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  by the corresponding element in the `weights` vector. If the shape of
5812d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` matches the shape of `predictions`, then the loss of each
5822d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  measurable element of `predictions` is scaled by the corresponding value of
5832d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights`.
5842d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5852d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
5862d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels: The ground truth output tensor, same dimensions as 'predictions'.
5872d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions: The predicted outputs.
58891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
58991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
59091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
5912d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
5922d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
593ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
5942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5952d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
59620a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
59720a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    shape as `labels`; otherwise, it is scalar.
5982d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
5992d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
6002d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `predictions` doesn't match that of `labels` or
6010236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      if the shape of `weights` is invalid.  Also if `labels` or `predictions`
6020236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      is None.
6032d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
6040236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if labels is None:
6050236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("labels must not be None.")
6060236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if predictions is None:
6070236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("predictions must not be None.")
6082d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "mean_squared_error",
6091055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (predictions, labels, weights)) as scope:
6102d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    predictions = math_ops.to_float(predictions)
6112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    labels = math_ops.to_float(labels)
6121055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    predictions.get_shape().assert_is_compatible_with(labels.get_shape())
6132d44813882ea2307c029223e6ab50ea847e411a1Sergio Guadarrama    losses = math_ops.squared_difference(predictions, labels)
614ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
615ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
6162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6172d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
618adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.sigmoid_cross_entropy")
6192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef sigmoid_cross_entropy(
6202d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    multi_class_labels, logits, weights=1.0, label_smoothing=0, scope=None,
621ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
62220a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
6232d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Creates a cross-entropy loss using tf.nn.sigmoid_cross_entropy_with_logits.
6242d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6252d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided,
6262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  then the loss is simply scaled by the given value. If `weights` is a
6272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  tensor of shape `[batch_size]`, then the loss weights apply to each
6282d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding sample.
6292d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6302d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  If `label_smoothing` is nonzero, smooth the labels towards 1/2:
6312d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6322d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      new_multiclass_labels = multiclass_labels * (1 - label_smoothing)
6332d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                              + 0.5 * label_smoothing
6342d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6352d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
6362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    multi_class_labels: `[batch_size, num_classes]` target integer labels in
6372d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `(0, 1)`.
63820a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    logits: Float `[batch_size, num_classes]` logits outputs of the network.
63991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or the same rank as
64091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
64191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      be either `1`, or the same as the corresponding `losses` dimension).
6422d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    label_smoothing: If greater than `0` then smooth the labels.
6432d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: The scope for the operations performed in computing the loss.
6442d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
645ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
6462d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6472d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
64820a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss `Tensor` of the same type as `logits`. If `reduction` is
64920a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    `NONE`, this has the same shape as `logits`; otherwise, it is scalar.
6502d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6512d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
6522d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `logits` doesn't match that of
6532d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      `multi_class_labels` or if the shape of `weights` is invalid, or if
6540236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      `weights` is None.  Also if `multi_class_labels` or `logits` is None.
6552d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
6560236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if multi_class_labels is None:
6570236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("multi_class_labels must not be None.")
6580236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if logits is None:
6590236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("logits must not be None.")
6602d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "sigmoid_cross_entropy_loss",
6611055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (logits, multi_class_labels, weights)) as scope:
6621055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits = ops.convert_to_tensor(logits)
6632d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    multi_class_labels = math_ops.cast(multi_class_labels, logits.dtype)
6641055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits.get_shape().assert_is_compatible_with(multi_class_labels.get_shape())
6652d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6662d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    if label_smoothing > 0:
6672d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      multi_class_labels = (multi_class_labels * (1 - label_smoothing) +
6682d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                            0.5 * label_smoothing)
6692d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
670333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke    losses = nn.sigmoid_cross_entropy_with_logits(labels=multi_class_labels,
671333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke                                                  logits=logits,
6722d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                                                  name="xentropy")
673ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
674ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
6752d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6762d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
677adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.softmax_cross_entropy")
6782d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyendef softmax_cross_entropy(
6792d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    onehot_labels, logits, weights=1.0, label_smoothing=0, scope=None,
680ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
68120a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
6822d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Creates a cross-entropy loss using tf.nn.softmax_cross_entropy_with_logits.
6832d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6842d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided,
6852d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  then the loss is simply scaled by the given value. If `weights` is a
6862d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  tensor of shape `[batch_size]`, then the loss weights apply to each
6872d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding sample.
6882d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6892d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  If `label_smoothing` is nonzero, smooth the labels towards 1/num_classes:
6902d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      new_onehot_labels = onehot_labels * (1 - label_smoothing)
6912d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                          + label_smoothing / num_classes
6922d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
6932d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
6942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    onehot_labels: `[batch_size, num_classes]` target one-hot-encoded labels.
695fe8406149feec453250905965a14285465cd2063Shanqing Cai    logits: `[batch_size, num_classes]` logits outputs of the network .
6967cd98b0a0dd2551071f417bb212a6b7b3ef6e6a3A. Unique TensorFlower    weights: Optional `Tensor` whose rank is either 0, or rank 1 and is
6977cd98b0a0dd2551071f417bb212a6b7b3ef6e6a3A. Unique TensorFlower      broadcastable to the loss which is a `Tensor` of shape `[batch_size]`.
6982d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    label_smoothing: If greater than 0 then smooth the labels.
6992d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: the scope for the operations performed in computing the loss.
7002d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
701ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
7022d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
7032d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
70420a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss `Tensor` of the same type as `logits`. If `reduction` is
70520a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    `NONE`, this has shape `[batch_size]`; otherwise, it is scalar.
7062d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
7072d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
7082d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    ValueError: If the shape of `logits` doesn't match that of `onehot_labels`
7090236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      or if the shape of `weights` is invalid or if `weights` is None.  Also if
7100236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      `onehot_labels` or `logits` is None.
7112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
7120236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if onehot_labels is None:
7130236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("onehot_labels must not be None.")
7140236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if logits is None:
7150236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("logits must not be None.")
7162d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "softmax_cross_entropy_loss",
7171055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower                      (logits, onehot_labels, weights)) as scope:
7181055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits = ops.convert_to_tensor(logits)
7192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    onehot_labels = math_ops.cast(onehot_labels, logits.dtype)
7201055b6a81e2c58773c1db415c5bc5a8b3b9b74c7A. Unique TensorFlower    logits.get_shape().assert_is_compatible_with(onehot_labels.get_shape())
7212d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
7222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    if label_smoothing > 0:
7232d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      num_classes = math_ops.cast(
7242d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen          array_ops.shape(onehot_labels)[1], logits.dtype)
7252d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      smooth_positives = 1.0 - label_smoothing
7262d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      smooth_negatives = label_smoothing / num_classes
7272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen      onehot_labels = onehot_labels * smooth_positives + smooth_negatives
7282d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
729a7c225c89e4169492f0eef57c913463b976dd44eYong Tang    onehot_labels = array_ops.stop_gradient(
730a7c225c89e4169492f0eef57c913463b976dd44eYong Tang        onehot_labels, name="labels_stop_gradient")
731a7c225c89e4169492f0eef57c913463b976dd44eYong Tang    losses = nn.softmax_cross_entropy_with_logits_v2(
732a7c225c89e4169492f0eef57c913463b976dd44eYong Tang        labels=onehot_labels, logits=logits, name="xentropy")
733a7c225c89e4169492f0eef57c913463b976dd44eYong Tang
734ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
735ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
7362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
7372d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
73891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower# TODO(ptucker): Merge this with similar method in metrics_impl.
73991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlowerdef _remove_squeezable_dimensions(
74091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels, predictions, weights=None, expected_rank_diff=0):
74191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  """Internal version of _remove_squeezable_dimensions which handles weights.
74291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
74391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  Squeezes `predictions` and `labels` if their ranks differ from expected by
74491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  exactly 1.
74591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  Squeezes `weights` if its rank is 1 more than the new rank of `predictions`
74691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
74791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  This will use static shape if available. Otherwise, it will add graph
74891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  operations, which could result in a performance hit.
74991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
75091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  Args:
75191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels: Label values, a `Tensor` whose dimensions match `predictions`.
75291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    predictions: Predicted values, a `Tensor` of arbitrary dimensions.
75391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights: Optional weight `Tensor`. It will be squeezed if it's not scalar,
75491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      and its rank is 1 more than the new rank of `labels`.
75591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    expected_rank_diff: Expected result of `rank(predictions) - rank(labels)`.
75691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
75791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  Returns:
75891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    Tuple of `predictions`, `labels` and `weights`, possibly with the last
75991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    dimension squeezed.
76091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  """
76191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  labels, predictions = confusion_matrix.remove_squeezable_dimensions(
76291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      labels, predictions, expected_rank_diff=expected_rank_diff)
76391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
76491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  if weights is not None:
76591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights = ops.convert_to_tensor(weights)
76691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels_rank = labels.get_shape().ndims
76791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights_shape = weights.get_shape()
76891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    weights_rank = weights_shape.ndims
76991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
77091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    if (labels_rank is not None) and (weights_rank is not None):
77191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      # Use static rank.
77291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      rank_diff = weights_rank - labels_rank
77391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      if rank_diff == 1:
77491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower        weights = array_ops.squeeze(weights, [-1])
77591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      return labels, predictions, weights
77691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
77791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    # Use dynamic rank.
77891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    rank_diff = array_ops.rank(weights) - array_ops.rank(labels)
77991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    if (weights_rank is None) or (
780c2bf66ad48c733ce678d43daaebbb7f9a42fc87cJianwei Xie        weights_rank > 0 and weights_shape.dims[-1].is_compatible_with(1)):
78191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      weights = control_flow_ops.cond(
78291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower          math_ops.equal(1, rank_diff),
78391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower          lambda: array_ops.squeeze(weights, [-1]),
78491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower          lambda: weights)
78591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
78691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower  return labels, predictions, weights
78791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
78891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower
789adc9ee7150c45830eb0857f6b9e935b9672b7bb1Anna R@tf_export("losses.sparse_softmax_cross_entropy")
790ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlowerdef sparse_softmax_cross_entropy(
791ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    labels, logits, weights=1.0, scope=None,
792ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    loss_collection=ops.GraphKeys.LOSSES,
79320a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
7942d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """Cross-entropy loss using `tf.nn.sparse_softmax_cross_entropy_with_logits`.
7952d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
7962d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  `weights` acts as a coefficient for the loss. If a scalar is provided,
7972d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  then the loss is simply scaled by the given value. If `weights` is a
7988330cce0556893a7236d87a09e7c495c55197de0Christopher Yeh  tensor of shape `[batch_size]`, then the loss weights apply to each
7992d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  corresponding sample.
8002d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
8012d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Args:
80291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels: `Tensor` of shape `[d_0, d_1, ..., d_{r-1}]` (where `r` is rank of
80391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `labels` and result) and dtype `int32` or `int64`. Each entry in `labels`
80491a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      must be an index in `[0, num_classes)`. Other values will raise an
80591a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      exception when this op is run on CPU, and return `NaN` for corresponding
80691a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      loss and gradient rows on GPU.
80791a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    logits: Unscaled log probabilities of shape
80891a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower      `[d_0, d_1, ..., d_{r-1}, num_classes]` and dtype `float32` or `float64`.
80923992bb091457f3e881ae1413d04c2aebbccfa2fA. Unique TensorFlower    weights: Coefficients for the loss. This must be scalar or broadcastable to
81023992bb091457f3e881ae1413d04c2aebbccfa2fA. Unique TensorFlower      `labels` (i.e. same rank and each dimension is either 1 or the same).
8112d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    scope: the scope for the operations performed in computing the loss.
8122d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen    loss_collection: collection to which the loss will be added.
813ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    reduction: Type of reduction to apply to loss.
8142d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
8152d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Returns:
81620a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    Weighted loss `Tensor` of the same type as `logits`. If `reduction` is
81720a52139efd7ddf34740db99d993c66976fc94c1A. Unique TensorFlower    `NONE`, this has the same shape as `labels`; otherwise, it is scalar.
8182d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen
8192d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  Raises:
8200236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    ValueError: If the shapes of `logits`, `labels`, and `weights` are
8210236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower      incompatible, or if any of them are None.
8222d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  """
8230236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if labels is None:
8240236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("labels must not be None.")
8250236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower  if logits is None:
8260236a36390c2bca73795c999cbd3123f631e6cf7A. Unique TensorFlower    raise ValueError("logits must not be None.")
8272d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen  with ops.name_scope(scope, "sparse_softmax_cross_entropy_loss",
828c9844ffe212ed18b6f3a4349ba666661280dcb82A. Unique TensorFlower                      (logits, labels, weights)) as scope:
82991a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    # As documented above in Args, labels contain class IDs and logits contains
83091a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    # 1 probability per class ID, so we expect rank(logits) - rank(labels) == 1;
83191a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    # therefore, expected_rank_diff=1.
83291a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower    labels, logits, weights = _remove_squeezable_dimensions(
83391a6f2f4d6b4cc74726021c084454bf576af4f76A. Unique TensorFlower        labels, logits, weights, expected_rank_diff=1)
834333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke    losses = nn.sparse_softmax_cross_entropy_with_logits(labels=labels,
835333dc32ff79af21484695157f3d141dc776f7c02Martin Wicke                                                         logits=logits,
8362d8a85420ae211f9d6cd1fb473ac004b95bfe692Patrick Nguyen                                                         name="xentropy")
837ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower    return compute_weighted_loss(
838ace2aa7b6f1381163d8187f1bf78b393cef4701fA. Unique TensorFlower        losses, weights, scope, loss_collection, reduction=reduction)
839