Have a question? Connect with the community at the TensorFlow Forum

# 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

### Public Constructors

 (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 ``` (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 ``` (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 ``` (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 ``` (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 ``` (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 ``` (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 ``` (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 ``` (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 ``` (Ops tf, String name, boolean fromLogits, float labelSmoothing, Reduction reduction, int axis) Creates a categorical cross entropy Loss

### Public Methods

 Operand ( Operand labels, Operand predictions, Operand sampleWeights) Generates an Operand that calculates the loss.

## 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 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 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 the name of this loss 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 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 the name of this loss 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 Whether to interpret predictions as a tensor of logit values 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 the name of this loss Whether to interpret predictions as a tensor of logit values 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 Whether to interpret predictions as a tensor of logit values 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 ``` 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 the name of this loss Whether to interpret predictions as a tensor of logit values 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 ``` Type of Reduction to apply to loss. 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.