CategoricalCrossentropy

public class CategoricalCrossentropy

Computes the crossentropy loss between the labels and predictions.

Use this crossentropy loss function when there are two or more label classes. We expect labels to be provided in a one_hot representation. If you want to provide labels as integers, please use SparseCategoricalCrossentropy loss. There should be # classes floating point values per feature.

Standalone usage:

    Operand<TFloat32> labels =
        tf.constant(new float[][] { {0, 1, 0}, {0, 0, 1} });
    Operand<TFloat32> predictions =
        tf.constant(new float[][] { {0.05f, 0.95f, 0f}, {0.1f, 0.8f, 0.1f} });
    CategoricalCrossentropy cce = new CategoricalCrossentropy(tf);
    Operand<TFloat32> result = cce.call(labels, predictions);
    // produces 1.177
 

Calling with sample weight:

    Operand<TFloat32> sampleWeight = tf.constant(new float[] {0.3f, 0.7f});
    Operand<TFloat32> result = cce.call(labels, predictions, sampleWeight);
    // produces 0.814f
 

Using SUM reduction type:

    CategoricalCrossentropy cce = new CategoricalCrossentropy(tf, Reduction.SUM);
    Operand<TFloat32> result = cce.call(labels, predictions);
    // produces 2.354f
 

Using NONE reduction type:

    CategoricalCrossentropy cce =
        new CategoricalCrossentropy(tf, Reduction.NONE);
    Operand<TFloat32> result = cce.call(labels, predictions);
    // produces [0.0513f, 2.303f]
 

Constants

int DEFAULT_AXIS
boolean FROM_LOGITS_DEFAULT
float LABEL_SMOOTHING_DEFAULT

Inherited Fields

Public Constructors

CategoricalCrossentropy(Ops tf)
Creates a categorical cross entropy Loss using getSimpleName() as the loss name, FROM_LOGITS_DEFAULT for fromLogits, LABEL_SMOOTHING_DEFAULT for labelSmoothing, a Loss Reduction of REDUCTION_DEFAULT, and an axis of DEFAULT_AXIS
CategoricalCrossentropy(Ops tf, String name)
Creates a categorical cross entropy Loss using FROM_LOGITS_DEFAULT for fromLogits, LABEL_SMOOTHING_DEFAULT for labelSmoothing, a Loss Reduction of REDUCTION_DEFAULT, and an axis of DEFAULT_AXIS
CategoricalCrossentropy(Ops tf, Reduction reduction)
Creates a categorical cross entropy Loss using getSimpleName() as the loss name, FROM_LOGITS_DEFAULT for fromLogits, LABEL_SMOOTHING_DEFAULT for labelSmoothing and an axis of DEFAULT_AXIS
CategoricalCrossentropy(Ops tf, String name, Reduction reduction)
Creates a categorical cross entropy Loss FROM_LOGITS_DEFAULT for fromLogits, LABEL_SMOOTHING_DEFAULT for labelSmoothing, and an axis of DEFAULT_AXIS
CategoricalCrossentropy(Ops tf, boolean fromLogits)
Creates a categorical cross entropy Loss using getSimpleName() as the loss name, LABEL_SMOOTHING_DEFAULT for labelSmoothing, a Loss Reduction of REDUCTION_DEFAULT, and an axis of DEFAULT_AXIS
CategoricalCrossentropy(Ops tf, String name, boolean fromLogits)
Creates a categorical cross entropy Loss using LABEL_SMOOTHING_DEFAULT for labelSmoothing, a Loss Reduction of REDUCTION_DEFAULT, and a channel axis of DEFAULT_AXIS
CategoricalCrossentropy(Ops tf, boolean fromLogits, float labelSmoothing)
Creates a categorical cross entropy Loss using getSimpleName() as the loss name, a Loss Reduction of REDUCTION_DEFAULT, and a channel axis of DEFAULT_AXIS
CategoricalCrossentropy(Ops tf, String name, boolean fromLogits, float labelSmoothing)
Creates a categorical cross entropy Loss using a Loss Reduction of REDUCTION_DEFAULT, and a channel axis of DEFAULT_AXIS
CategoricalCrossentropy(Ops tf, boolean fromLogits, float labelSmoothing, Reduction reduction)
Creates a categorical cross entropy Loss using getSimpleName() as the loss name and a channel axis of DEFAULT_AXIS
CategoricalCrossentropy(Ops tf, String name, boolean fromLogits, float labelSmoothing, Reduction reduction, int axis)
Creates a categorical cross entropy Loss

Public Methods

<T extends TNumber> Operand<T>
call(Operand<? extends TNumber> labels, Operand<T> predictions, Operand<T> sampleWeights)
Generates an Operand that calculates the loss.

Inherited Methods

Constants

public static final int DEFAULT_AXIS

Constant Value: -1

public static final boolean FROM_LOGITS_DEFAULT

Constant Value: false

public static final float LABEL_SMOOTHING_DEFAULT

Constant Value: 0.0

Public Constructors

public CategoricalCrossentropy (Ops tf)

Creates a categorical cross entropy Loss using getSimpleName() as the loss name, FROM_LOGITS_DEFAULT for fromLogits, LABEL_SMOOTHING_DEFAULT for labelSmoothing, a Loss Reduction of REDUCTION_DEFAULT, and an axis of DEFAULT_AXIS

Parameters
tf the TensorFlow Ops

public CategoricalCrossentropy (Ops tf, String name)

Creates a categorical cross entropy Loss using FROM_LOGITS_DEFAULT for fromLogits, LABEL_SMOOTHING_DEFAULT for labelSmoothing, a Loss Reduction of REDUCTION_DEFAULT, and an axis of DEFAULT_AXIS

Parameters
tf the TensorFlow Ops
name the name of this loss

public CategoricalCrossentropy (Ops tf, Reduction reduction)

Creates a categorical cross entropy Loss using getSimpleName() as the loss name, FROM_LOGITS_DEFAULT for fromLogits, LABEL_SMOOTHING_DEFAULT for labelSmoothing and an axis of DEFAULT_AXIS

Parameters
tf the TensorFlow Ops
reduction Type of Reduction to apply to loss.

public CategoricalCrossentropy (Ops tf, String name, Reduction reduction)

Creates a categorical cross entropy Loss FROM_LOGITS_DEFAULT for fromLogits, LABEL_SMOOTHING_DEFAULT for labelSmoothing, and an axis of DEFAULT_AXIS

Parameters
tf the TensorFlow Ops
name the name of this loss
reduction Type of Reduction to apply to loss.

public CategoricalCrossentropy (Ops tf, boolean fromLogits)

Creates a categorical cross entropy Loss using getSimpleName() as the loss name, LABEL_SMOOTHING_DEFAULT for labelSmoothing, a Loss Reduction of REDUCTION_DEFAULT, and an axis of DEFAULT_AXIS

Parameters
tf the TensorFlow Ops
fromLogits Whether to interpret predictions as a tensor of logit values

public CategoricalCrossentropy (Ops tf, String name, boolean fromLogits)

Creates a categorical cross entropy Loss using LABEL_SMOOTHING_DEFAULT for labelSmoothing, a Loss Reduction of REDUCTION_DEFAULT, and a channel axis of DEFAULT_AXIS

Parameters
tf the TensorFlow Ops
name the name of this loss
fromLogits Whether to interpret predictions as a tensor of logit values

public CategoricalCrossentropy (Ops tf, boolean fromLogits, float labelSmoothing)

Creates a categorical cross entropy Loss using getSimpleName() as the loss name, a Loss Reduction of REDUCTION_DEFAULT, and a channel axis of DEFAULT_AXIS

Parameters
tf the TensorFlow Ops
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

public CategoricalCrossentropy (Ops tf, String name, boolean fromLogits, float labelSmoothing)

Creates a categorical cross entropy Loss using a Loss Reduction of REDUCTION_DEFAULT, and a channel axis of DEFAULT_AXIS

Parameters
tf the TensorFlow Ops
name the name of this loss
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

public CategoricalCrossentropy (Ops tf, boolean fromLogits, float labelSmoothing, Reduction reduction)

Creates a categorical cross entropy Loss using getSimpleName() as the loss name and a channel axis of DEFAULT_AXIS

Parameters
tf the TensorFlow Ops
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. x=0.2 means that we will use a value of 0.1 for label 0 and 0.9 for label 1
reduction Type of Reduction to apply to loss.

public CategoricalCrossentropy (Ops tf, String name, boolean fromLogits, float labelSmoothing, Reduction reduction, int axis)

Creates a categorical cross entropy Loss

Parameters
tf the TensorFlow Ops
name the name of this loss
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
reduction Type of Reduction to apply to loss.
axis The channels axis. axis=-1 corresponds to data format "Channels Last" and axis=1 corresponds to data format "Channels First". CHANNELS_LAST and CHANNELS_FIRST
Throws
IllegalArgumentException if labelSmoothing is not in the inclusive range of 0. - 1.

Public Methods

public Operand<T> call (Operand<? extends TNumber> labels, Operand<T> predictions, Operand<T> sampleWeights)

Generates an Operand that calculates the loss.

If run in Graph mode, the computation will throw TFInvalidArgumentException if the predictions values are outside the range o [0. to 1.]. In Eager Mode, this call will throw IllegalArgumentException, if the predictions values are outside the range o [0. to 1.]

Parameters
labels the truth values or labels
predictions the predictions, values must be in the range [0. to 1.] inclusive.
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
Throws
IllegalArgumentException if the predictions are outside the range [0.-1.].