Home | History | Annotate | Download | only in ops
      1 # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 # ==============================================================================
     15 """Deprecated Wrappers for Neural Net (NN) Cross Entropy Operations."""
     16 
     17 from __future__ import absolute_import
     18 from __future__ import division
     19 from __future__ import print_function
     20 
     21 from tensorflow.python.ops import nn
     22 
     23 
     24 # TODO(b/33392402): Formally deprecate this API.
     25 # After LSC (see b/33392402#comment1), this API will be deprecated and callers
     26 # will be suggested to use the (updated version of)
     27 # tf.nn.softmax_cross_entropy_with_logits.
     28 def deprecated_flipped_softmax_cross_entropy_with_logits(logits,
     29                                                          labels,
     30                                                          dim=-1,
     31                                                          name=None):
     32   """Computes softmax cross entropy between `logits` and `labels`.
     33 
     34   This function diffs from tf.nn.softmax_cross_entropy_with_logits only in the
     35   argument order.
     36 
     37   Measures the probability error in discrete classification tasks in which the
     38   classes are mutually exclusive (each entry is in exactly one class).  For
     39   example, each CIFAR-10 image is labeled with one and only one label: an image
     40   can be a dog or a truck, but not both.
     41 
     42   **NOTE:**  While the classes are mutually exclusive, their probabilities
     43   need not be.  All that is required is that each row of `labels` is
     44   a valid probability distribution.  If they are not, the computation of the
     45   gradient will be incorrect.
     46 
     47   If using exclusive `labels` (wherein one and only
     48   one class is true at a time), see `sparse_softmax_cross_entropy_with_logits`.
     49 
     50   **WARNING:** This op expects unscaled logits, since it performs a `softmax`
     51   on `logits` internally for efficiency.  Do not call this op with the
     52   output of `softmax`, as it will produce incorrect results.
     53 
     54   `logits` and `labels` must have the same shape `[batch_size, num_classes]`
     55   and the same dtype (either `float16`, `float32`, or `float64`).
     56 
     57   Args:
     58     logits: Unscaled log probabilities.
     59     labels: Each row `labels[i]` must be a valid probability distribution.
     60     dim: The class dimension. Defaulted to -1 which is the last dimension.
     61     name: A name for the operation (optional).
     62 
     63   Returns:
     64     A 1-D `Tensor` of length `batch_size` of the same type as `logits` with the
     65     softmax cross entropy loss.
     66   """
     67   return nn.softmax_cross_entropy_with_logits(
     68       labels=labels, logits=logits, dim=dim, name=name)
     69 
     70 
     71 # TODO(b/33392402): Formally deprecate this API.
     72 # After LSC (see b/33392402#comment1), this API will be deprecated and callers
     73 # will be suggested to use the (updated version of)
     74 # tf.nn.sparse_softmax_cross_entropy_with_logits.
     75 def deprecated_flipped_sparse_softmax_cross_entropy_with_logits(logits,
     76                                                                 labels,
     77                                                                 name=None):
     78   """Computes sparse softmax cross entropy between `logits` and `labels`.
     79 
     80   This function diffs from tf.nn.sparse_softmax_cross_entropy_with_logits only
     81   in the argument order.
     82 
     83   Measures the probability error in discrete classification tasks in which the
     84   classes are mutually exclusive (each entry is in exactly one class).  For
     85   example, each CIFAR-10 image is labeled with one and only one label: an image
     86   can be a dog or a truck, but not both.
     87 
     88   **NOTE:**  For this operation, the probability of a given label is considered
     89   exclusive.  That is, soft classes are not allowed, and the `labels` vector
     90   must provide a single specific index for the true class for each row of
     91   `logits` (each minibatch entry).  For soft softmax classification with
     92   a probability distribution for each entry, see
     93   `softmax_cross_entropy_with_logits`.
     94 
     95   **WARNING:** This op expects unscaled logits, since it performs a softmax
     96   on `logits` internally for efficiency.  Do not call this op with the
     97   output of `softmax`, as it will produce incorrect results.
     98 
     99   A common use case is to have logits of shape `[batch_size, num_classes]` and
    100   labels of shape `[batch_size]`. But higher dimensions are supported.
    101 
    102   Args:
    103 
    104     logits: Unscaled log probabilities of rank `r` and shape
    105       `[d_0, d_1, ..., d_{r-2}, num_classes]` and dtype `float32` or `float64`.
    106     labels: `Tensor` of shape `[d_0, d_1, ..., d_{r-2}]` and dtype `int32` or
    107       `int64`. Each entry in `labels` must be an index in `[0, num_classes)`.
    108       Other values will raise an exception when this op is run on CPU, and
    109       return `NaN` for corresponding corresponding loss and gradient rows
    110       on GPU.
    111     name: A name for the operation (optional).
    112 
    113   Returns:
    114     A `Tensor` of the same shape as `labels` and of the same type as `logits`
    115     with the softmax cross entropy loss.
    116 
    117   Raises:
    118     ValueError: If logits are scalars (need to have rank >= 1) or if the rank
    119       of the labels is not equal to the rank of the logits minus one.
    120   """
    121   return nn.sparse_softmax_cross_entropy_with_logits(
    122       labels=labels, logits=logits, name=name)
    123 
    124 
    125 # TODO(b/33392402): Formally deprecate this API.
    126 # After LSC (see b/33392402#comment1), this API will be deprecated and callers
    127 # will be suggested to use the (updated version of)
    128 # tf.nn.sigmoid_cross_entropy_with_logits.
    129 def deprecated_flipped_sigmoid_cross_entropy_with_logits(logits,
    130                                                          targets,
    131                                                          name=None):
    132   """Computes sigmoid cross entropy given `logits`.
    133 
    134   This function diffs from tf.nn.sigmoid_cross_entropy_with_logits only in the
    135   argument order.
    136 
    137   Measures the probability error in discrete classification tasks in which each
    138   class is independent and not mutually exclusive.  For instance, one could
    139   perform multilabel classification where a picture can contain both an elephant
    140   and a dog at the same time.
    141 
    142   For brevity, let `x = logits`, `z = targets`.  The logistic loss is
    143 
    144         z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
    145       = z * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))
    146       = z * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))
    147       = z * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))
    148       = (1 - z) * x + log(1 + exp(-x))
    149       = x - x * z + log(1 + exp(-x))
    150 
    151   For x < 0, to avoid overflow in exp(-x), we reformulate the above
    152 
    153         x - x * z + log(1 + exp(-x))
    154       = log(exp(x)) - x * z + log(1 + exp(-x))
    155       = - x * z + log(1 + exp(x))
    156 
    157   Hence, to ensure stability and avoid overflow, the implementation uses this
    158   equivalent formulation
    159 
    160       max(x, 0) - x * z + log(1 + exp(-abs(x)))
    161 
    162   `logits` and `targets` must have the same type and shape.
    163 
    164   Args:
    165     logits: A `Tensor` of type `float32` or `float64`.
    166     targets: A `Tensor` of the same type and shape as `logits`.
    167     name: A name for the operation (optional).
    168 
    169   Returns:
    170     A `Tensor` of the same shape as `logits` with the componentwise
    171     logistic losses.
    172 
    173   Raises:
    174     ValueError: If `logits` and `targets` do not have the same shape.
    175   """
    176   return nn.sigmoid_cross_entropy_with_logits(
    177       labels=targets, logits=logits, name=name)
    178