Class Huber

java.lang.Object
org.tensorflow.framework.losses.Huber
All Implemented Interfaces:
Loss

public class Huber extends Object
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.

Standalone usage:

   Operand<TFloat32> labels =
       tf.constant(new float[][] {{0.f, 1.f}, {0.f, 0.f}});
   Operand<TFloat32> predictions =
       tf.constant(new float[][] {{0.6f, 0.4f}, {0.4f, 0.6f}});
   Huber huberLoss = new Huber(tf);
   Operand<TFloat32> result = huberLoss.call(Ops tf, labels, predictions);
   // produces 0.155

Calling with sample weight:

   Operand<TFloat32> sampleWeight = tf.constant(new float[] {1.f, 0.f});
   Operand<TFloat32> result = huberLoss.call(Ops tf, labels, predictions, sampleWeight);
   // produces 0.09f

Using SUM reduction type:

   Huber huberLoss = new Huber(tf, Reduction.SUM);
   Operand<TFloat32> result = huberLoss.call(Ops tf, labels, predictions);
   // produces 0.32f

Using NONE reduction type:

   Huber huberLoss = new Huber(tf, Reduction.NONE);
   Operand<TFloat32> result = huberLoss.call(Ops tf, labels, predictions);
   // produces [0.18f, 0.13f]
See Also:
  • Field Details

    • DELTA_DEFAULT

      public static final float DELTA_DEFAULT
      See Also:
    • REDUCTION_DEFAULT

      public static final Reduction REDUCTION_DEFAULT
    • reduction

      protected final Reduction reduction
  • Constructor Details

    • Huber

      public Huber()
      Creates a Huber AbstractLoss using Class.getSimpleName() as the loss name, DELTA_DEFAULT as the delta and a AbstractLoss Reduction of AbstractLoss.REDUCTION_DEFAULT
    • Huber

      public Huber(String name)
      Creates a Huber AbstractLoss using DELTA_DEFAULT as the delta and a AbstractLoss Reduction of AbstractLoss.REDUCTION_DEFAULT
      Parameters:
      name - the name of the loss, if null then Class.getSimpleName() is used.
    • Huber

      public Huber(Reduction reduction)
      Creates a Huber AbstractLoss using Class.getSimpleName() as the loss name and and DELTA_DEFAULT as the delta
      Parameters:
      reduction - Type of Reduction to apply to the loss.
    • Huber

      public Huber(String name, Reduction reduction)
      Creates a Huber AbstractLoss using DELTA_DEFAULT as the delta
      Parameters:
      name - the name of the loss, if null then Class.getSimpleName() is used.
      reduction - Type of Reduction to apply to the loss.
    • Huber

      public Huber(String name, float delta, Reduction reduction)
      Creates a Huber AbstractLoss
      Parameters:
      name - the name of the loss, if null then Class.getSimpleName() is used.
      delta - the point where the Huber loss function changes from quadratic to linear.
      reduction - Type of Reduction to apply to the loss.
  • Method Details

    • call

      public <T extends TNumber> Operand<T> call(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions, Operand<T> sampleWeights)
      Generates an Operand that calculates the loss.
      Type Parameters:
      T - The data type of the predictions, sampleWeights and loss.
      Parameters:
      tf - the TensorFlow Ops
      labels - the truth values or labels
      predictions - the predictions
      sampleWeights - Optional sampleWeights acts as a coefficient for the loss. If a scalar is provided, then the loss is simply scaled by the given value. If SampleWeights is a tensor of size [batch_size], then the total loss for each sample of the batch is rescaled by the corresponding element in the SampleWeights vector. If the shape of SampleWeights is [batch_size, d0, .. dN-1] (or can be broadcast to this shape), then each loss element of predictions is scaled by the corresponding value of SampleWeights. (Note on dN-1: all loss functions reduce by 1 dimension, usually axis=-1.)
      Returns:
      the loss
    • call

      public <T extends TNumber> Operand<T> call(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions)
      Calculates the loss
      Type Parameters:
      T - The data type of the predictions and loss.
      Parameters:
      tf - the TensorFlow Ops
      labels - the truth values or labels
      predictions - the predictions
      Returns:
      the loss
    • getReduction

      public Reduction getReduction()
      Gets the loss reduction
      Returns:
      the loss reduction
    • getName

      public String getName()
      Gets the name for this loss
      Returns:
      the name for this loss