Class Losses

java.lang.Object
org.tensorflow.framework.losses.Losses

public class Losses extends Object
Built-in loss functions.
  • Field Details

  • Constructor Details

    • Losses

      public Losses()
  • Method Details

    • meanAbsoluteError

      public static <T extends TNumber> Operand<T> meanAbsoluteError(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Calculates the mean absolute error between labels and predictions.

      loss = reduceMean(abs(labels - predictions))

      Type Parameters:
      T - the data type of the predictions and result
      Parameters:
      tf - The TensorFlow Ops
      labels - the labels
      predictions - the predictions
      Returns:
      the mean absolute error
    • meanSquaredError

      public static <T extends TNumber> Operand<T> meanSquaredError(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Computes the mean squared error between labels and predictions.

      loss = reduceMean(square(labels - predictions))

      Type Parameters:
      T - the data type of the predictions and result
      Parameters:
      tf - The TensorFlow Ops
      labels - the labels
      predictions - the predictions
      Returns:
      the mean squared error
    • meanAbsolutePercentageError

      public static <T extends TNumber> Operand<T> meanAbsolutePercentageError(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Calculates the mean absolute percentage error between labels and predictions.

      loss = 100 * reduceMean(abs((labels - predictions) / labels))

      Type Parameters:
      T - the data type of the predictions and result
      Parameters:
      tf - The TensorFlow Ops
      labels - the labels
      predictions - the predictions
      Returns:
      the mean absolute percentage error
    • meanSquaredLogarithmicError

      public static <T extends TNumber> Operand<T> meanSquaredLogarithmicError(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Calculates the mean squared logarithmic error between labels and predictions.

      loss = reduceMean(square(log(labels + 1) - log(predictions + 1)))

      Type Parameters:
      T - the data type of the predictions and result
      Parameters:
      tf - The TensorFlow Ops
      labels - the labels
      predictions - the predictions
      Returns:
      the mean squared logarithmic percentage error
    • binaryCrossentropy

      public static <T extends TNumber> Operand<T> binaryCrossentropy(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions, boolean fromLogits, float labelSmoothing)
      Computes the binary crossentropy loss between labels and predictions.
      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets
      predictions - the predictions
      fromLogits - Whether to interpret predictions as a tensor of logit values
      labelSmoothing - A number in the range [0, 1]. When 0, no smoothing occurs. When > 0, compute the loss between the predicted labels and a smoothed version of the true labels, where the smoothing squeezes the labels towards 0.5. Larger values of labelSmoothing correspond to heavier smoothing.
      Returns:
      the binary crossentropy loss.
    • categoricalCrossentropy

      public static <T extends TNumber> Operand<T> categoricalCrossentropy(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions, boolean fromLogits, float labelSmoothing, int axis)
      Computes the categorical crossentropy loss between labels and predictions.
      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets
      predictions - the predictions
      fromLogits - Whether to interpret predictions as a tensor of logit values
      labelSmoothing - Float in [0, 1]. When > 0, label values are smoothed, meaning the confidence on label values are relaxed. e.g. labelSmoothing=0.2 means that we will use a value of 0.1 for label 0 and 0.9 for label 1
      axis - the
      Returns:
      the categorical crossentropy loss.
    • categoricalHinge

      public static <T extends TNumber> Operand<T> categoricalHinge(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Computes the categorical hinge loss between labels and predictions.
      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets, values are expected to be 0 or 1.
      predictions - the predictions
      Returns:
      the categorical hinge loss
    • cosineSimilarity

      public static <T extends TNumber> Operand<T> cosineSimilarity(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions, int[] axis)
      Computes the cosine similarity loss between labels and predictions.

      Note that it is a number between -1 and 1, which is different from the mathematical definition of cosine similarity where 1 represents similar vectors, and 0 represents dissimilar vectors. In this function, the numbers are inverted in a range of -1 to 1. When it is a negative number between -1 and 0, 0 indicates orthogonality and values closer to -1 indicate greater similarity. The values closer to 1 indicate greater dissimilarity. This makes it usable as a loss function in a setting where you try to maximize the proximity between predictions and targets. If either labels or predictions is a zero vector, cosine similarity will be 0 regardless of the proximity between predictions and targets.

      loss = -sum(l2Norm(labels) * l2Norm(predictions))

      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets
      predictions - the predictions
      axis - Axis along which to determine similarity.
      Returns:
      the cosine similarity loss
    • hinge

      public static <T extends TNumber> Operand<T> hinge(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Computes the hinge loss between labels and predictions

      loss = reduceMean(maximum(1 - labels * predictions, 0))

      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets, values are expected to be -1 or 1. If binary (0 or 1) labels are provided, they will be converted to -1 or 1.
      predictions - the predictions
      Returns:
      the hinge loss
    • huber

      public static <T extends TNumber> Operand<T> huber(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions, float delta)
      Computes the Huber loss between labels and predictions.

      For each value x in error = labels - predictions:

          loss = 0.5 * x^2                  if |x| <= d
          loss = 0.5 * d^2 + d * (|x| - d)  if |x| > d
      

      where d is delta.

      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets
      predictions - the predictions
      delta - the point where the Huber loss function changes from quadratic to linear.
      Returns:
      the Huber loss
    • kullbackLeiblerDivergence

      public static <T extends TNumber> Operand<T> kullbackLeiblerDivergence(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Computes the Kullback-Leibler divergence loss between labels and predictions.
      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets
      predictions - the predictions
      Returns:
      the Kullback-Leibler divergence loss
      See Also:
    • logCosh

      public static <T extends TNumber> Operand<T> logCosh(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Computes the hyperbolic cosine loss between labels and predictions.

      log(cosh(x)) is approximately equal to (x ** 2) / 2 for small x and to abs(x) - log(2) for large x. This means that 'logCosh' works mostly like the mean squared error, but will not be so strongly affected by the occasional wildly incorrect prediction.

      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets
      predictions - the predictions
      Returns:
      the hyperbolic cosine divergence loss
    • poisson

      public static <T extends TNumber> Operand<T> poisson(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Computes the Poisson loss between labels and predictions.

      The Poisson loss is the mean of the elements of the Tensor predictions - labels * log(predictions).

      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets
      predictions - the predictions
      Returns:
      the Poisson loss
    • sparseCategoricalCrossentropy

      public static <T extends TNumber> Operand<T> sparseCategoricalCrossentropy(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions, boolean fromLogits, int axis)
      Computes the sparse categorical crossentropy loss between labels and predictions.
      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets
      predictions - the predictions
      fromLogits - Whether predictions is expected to be logits. By default, it is assumed that predictions encodes a probability distribution.
      axis - The dimension along which the entropy is computed.
      Returns:
      the sparse categorical crossentropy loss
    • squaredHinge

      public static <T extends TNumber> Operand<T> squaredHinge(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Computes the squared hinge loss between labels and predictions.

      loss = reduceMean(square(maximum(1 - labels * predictions, 0)))

      Type Parameters:
      T - the data type of the predictions and labels
      Parameters:
      tf - the TensorFlow Ops
      labels - true targets, values are expected to be -1 or 1. If binary (0 or 1) labels are * provided, they will be converted to -1 or 1.
      predictions - the predictions
      Returns:
      the squared hinge loss
    • l2Normalize

      public static <T extends TNumber> Operand<T> l2Normalize(Ops tf, Operand<T> x, int[] axis)
      Normalizes along dimension axis using an L2 norm.
      Type Parameters:
      T - the data type for the input and the result
      Parameters:
      tf - The TensorFlow Ops
      x - the input
      axis - Dimension along which to normalize.
      Returns:
      the normalized values based on L2 norm