SparseCategoricalCrossentropy

public class SparseCategoricalCrossentropy

Computes the crossentropy loss between labels and predictions.

Use this crossentropy loss function when there are two or more label classes. The labels are expected to be provided as integers. If you want to provide labels using one-hot representation, please use CategoricalCrossentropy loss. There should be # classes floating point values per feature for predictions and a single floating point value per feature for label.

In the snippet below, there is a single floating point value per example for labels and # classes floating pointing values per example for predictions . The shape of labels is [batch_size] and the shape of predictions is [batch_size, num_classes].

Standalone usage:

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

Calling with sample weight:

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

Using SUM reduction type:

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

Using NONE reduction type:

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

Constants

int AXIS_DEFAULT
boolean FROM_LOGITS_DEFAULT

Inherited Fields

Public Constructors

SparseCategoricalCrossentropy(Ops tf)
Creates a SparseCategoricalCrossentropy loss using getSimpleName() as the loss name, a Loss Reduction of REDUCTION_DEFAULT, and fromLogits=FROM_LOGITS_DEFAULT.
SparseCategoricalCrossentropy(Ops tf, String name)
Creates a SparseCategoricalCrossentropy loss using a Loss Reduction of REDUCTION_DEFAULT, and fromLogits=FROM_LOGITS_DEFAULT.
SparseCategoricalCrossentropy(Ops tf, Reduction reduction)
Creates a SparseCategoricalCrossentropy loss using getSimpleName() as the loss name, with Reduction.AUTO and fromLogits=FROM_LOGITS_DEFAULT.
SparseCategoricalCrossentropy(Ops tf, String name, Reduction reduction)
Creates a SparseCategoricalCrossentropy loss with Reduction.AUTO and fromLogits=FROM_LOGITS_DEFAULT.
SparseCategoricalCrossentropy(Ops tf, String name, boolean fromLogits)
Creates a SparseCategoricalCrossentropy using a Loss Reduction of REDUCTION_DEFAULT, and fromLogits=FROM_LOGITS_DEFAULT.
SparseCategoricalCrossentropy(Ops tf, boolean fromLogits)
Creates a SparseCategoricalCrossentropy loss using getSimpleName() as the loss name, a Loss Reduction of REDUCTION_DEFAULT and fromLogits=FROM_LOGITS_DEFAULT.
SparseCategoricalCrossentropy(Ops tf, boolean fromLogits, Reduction reduction)
Creates a SparseCategoricalCrossentropy loss using getSimpleName() as the loss name,
SparseCategoricalCrossentropy(Ops tf, String name, boolean fromLogits, Reduction reduction, int axis)
Creates a SparseCategoricalCrossentropy

Public Methods

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

Inherited Methods

Constants

public static final int AXIS_DEFAULT

Constant Value: -1

public static final boolean FROM_LOGITS_DEFAULT

Constant Value: false

Public Constructors

public SparseCategoricalCrossentropy (Ops tf)

Creates a SparseCategoricalCrossentropy loss using getSimpleName() as the loss name, a Loss Reduction of REDUCTION_DEFAULT, and fromLogits=FROM_LOGITS_DEFAULT.

Parameters
tf the TensorFlow Ops

public SparseCategoricalCrossentropy (Ops tf, String name)

Creates a SparseCategoricalCrossentropy loss using a Loss Reduction of REDUCTION_DEFAULT, and fromLogits=FROM_LOGITS_DEFAULT.

Parameters
tf the TensorFlow Ops
name the name of this loss function

public SparseCategoricalCrossentropy (Ops tf, Reduction reduction)

Creates a SparseCategoricalCrossentropy loss using getSimpleName() as the loss name, with Reduction.AUTO and fromLogits=FROM_LOGITS_DEFAULT.

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

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

Creates a SparseCategoricalCrossentropy loss with Reduction.AUTO and fromLogits=FROM_LOGITS_DEFAULT.

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

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

Creates a SparseCategoricalCrossentropy using a Loss Reduction of REDUCTION_DEFAULT, and fromLogits=FROM_LOGITS_DEFAULT.

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

public SparseCategoricalCrossentropy (Ops tf, boolean fromLogits)

Creates a SparseCategoricalCrossentropy loss using getSimpleName() as the loss name, a Loss Reduction of REDUCTION_DEFAULT and fromLogits=FROM_LOGITS_DEFAULT.

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

public SparseCategoricalCrossentropy (Ops tf, boolean fromLogits, Reduction reduction)

Creates a SparseCategoricalCrossentropy loss using getSimpleName() as the loss name,

Parameters
tf the TensorFlow Ops
fromLogits Whether to interpret predictions as a tensor of logit values
reduction Type of Reduction to apply to loss.

public SparseCategoricalCrossentropy (Ops tf, String name, boolean fromLogits, Reduction reduction, int axis)

Creates a SparseCategoricalCrossentropy

Parameters
tf the TensorFlow Ops
name the name of this loss function
fromLogits Whether to interpret predictions as a tensor of logit values
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'.

Public Methods

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

Generates an Operand the 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.].