|
| 1 | +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +=======================================================================*/ |
| 15 | +package org.tensorflow.framework.metrics; |
| 16 | + |
| 17 | +import org.tensorflow.Operand; |
| 18 | +import org.tensorflow.framework.losses.Losses; |
| 19 | +import org.tensorflow.framework.metrics.impl.LossMetric; |
| 20 | +import org.tensorflow.framework.metrics.impl.MeanMetricWrapper; |
| 21 | +import org.tensorflow.op.Ops; |
| 22 | +import org.tensorflow.types.family.TNumber; |
| 23 | + |
| 24 | +/** |
| 25 | + * A Metric that computes the categorical cross-entropy loss between true labels and predicted |
| 26 | + * labels. |
| 27 | + * |
| 28 | + * <p>This is the crossentropy metric class to be used when there are multiple label classes (2 or |
| 29 | + * more). The labels should be given as a one_hot representation. eg., When labels values are <code> |
| 30 | + * [2, 0, 1]</code>, the labels Operand contains = <code>[[0, 0, 1], [1, 0, 0], [0, 1, 0]] |
| 31 | + * </code>. |
| 32 | + * |
| 33 | + * @param <U> the data type for the predictions. |
| 34 | + * @param <T> The data type for the metric result |
| 35 | + */ |
| 36 | +public class CategoricalCrossentropy<U extends TNumber, T extends TNumber> |
| 37 | + extends MeanMetricWrapper<U, T> implements LossMetric<T> { |
| 38 | + |
| 39 | + private final boolean fromLogits; |
| 40 | + private final float labelSmoothing; |
| 41 | + private final int axis; |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a CategoricalCrossentropy metric that computes the crossentropy metric between the |
| 45 | + * labels and predictions. |
| 46 | + * |
| 47 | + * <p>Uses a {@link Losses#CHANNELS_LAST} for the channel axis. |
| 48 | + * |
| 49 | + * @param tf the TensorFlow Ops |
| 50 | + * @param name the name of this metric, if null then metric name is {@link Class#getSimpleName()}. |
| 51 | + * @param fromLogits Whether to interpret predictions as a tensor of logit values oras opposed to a probability distribution. |
| 52 | + * @param labelSmoothing value used to smooth labels, When > 0, label values are smoothed, |
| 53 | + * meaning the confidence on label values are relaxed. e.g. <code>labelSmoothing=0.2</code> |
| 54 | + * means that we will use a value of <code>0.1</code> for label <code>0</code> and <code>0.9 |
| 55 | + * </code> for label <code>1</code> |
| 56 | + * @param seed the seed for random number generation. An initializer created with a given seed |
| 57 | + * will always produce the same random tensor for a given shape and data type. |
| 58 | + * @param type the type for the variables and result |
| 59 | + */ |
| 60 | + public CategoricalCrossentropy( |
| 61 | + Ops tf, String name, boolean fromLogits, float labelSmoothing, long seed, Class<T> type) { |
| 62 | + this(tf, name, fromLogits, labelSmoothing, Losses.CHANNELS_LAST, seed, type); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a CategoricalCrossentropy metric that computes the crossentropy metric between the |
| 67 | + * labels and predictions. |
| 68 | + * |
| 69 | + * @param tf the TensorFlow Ops |
| 70 | + * @param name the name of this metric, if null then metric name is {@link Class#getSimpleName()}. |
| 71 | + * @param fromLogits Whether to interpret predictions as a tensor of logit values as opposed to a probability distribution. |
| 72 | + * @param labelSmoothing value used to smooth labels, When > 0, label values are smoothed, |
| 73 | + * meaning the confidence on label values are relaxed. e.g. <code>labelSmoothing=0.2</code> |
| 74 | + * means that we will use a value of <code>0.1</code> for label <code>0</code> and <code>0.9 |
| 75 | + * </code> for label <code>1</code> |
| 76 | + * @param axis Int specifying the channels axis. <code>axis={@link Losses#CHANNELS_LAST}</code> |
| 77 | + * corresponds to data format <code>channels_last</code>, and <code> |
| 78 | + * axis={@link Losses#CHANNELS_FIRST}</code> corresponds to data format <code> |
| 79 | + * channels_first</code>. |
| 80 | + * @param seed the seed for random number generation. An initializer created with a given seed |
| 81 | + * will always produce the same random tensor for a given shape and data type. |
| 82 | + * @param type the type for the variables and result |
| 83 | + */ |
| 84 | + public CategoricalCrossentropy( |
| 85 | + Ops tf, |
| 86 | + String name, |
| 87 | + boolean fromLogits, |
| 88 | + float labelSmoothing, |
| 89 | + int axis, |
| 90 | + long seed, |
| 91 | + Class<T> type) { |
| 92 | + super(tf, name, seed, type); |
| 93 | + setLoss(this); |
| 94 | + this.fromLogits = fromLogits; |
| 95 | + this.labelSmoothing = labelSmoothing; |
| 96 | + this.axis = axis; |
| 97 | + } |
| 98 | + |
| 99 | + /** {@inheritDoc} */ |
| 100 | + @Override |
| 101 | + public <V extends TNumber> Operand<T> call(Operand<V> labels, Operand<T> predictions) { |
| 102 | + return Losses.categoricalCrossentropy( |
| 103 | + getTF(), labels, predictions, fromLogits, labelSmoothing, axis); |
| 104 | + } |
| 105 | +} |
0 commit comments