- 
                Notifications
    You must be signed in to change notification settings 
- Fork 219
Add Losses #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Losses #129
Changes from 4 commits
c57a2e7
              9cc2675
              2508f5e
              17e96b5
              ee1c48a
              287c96e
              642069c
              249b651
              794cfdc
              fb26c59
              928ef06
              2bc54dd
              951443b
              ebac9e8
              d8f3254
              02573b5
              0bf49fe
              0eae9ee
              b211937
              3e0669e
              914f16f
              7eefbb7
              b87ad16
              c43cd21
              4d9fd24
              d56d8d9
              744e324
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| package org.tensorflow.framework.losses; | ||
|  | ||
| import org.tensorflow.Operand; | ||
| import org.tensorflow.framework.losses.impl.LossesImpl; | ||
| import org.tensorflow.op.Ops; | ||
| import org.tensorflow.types.family.TNumber; | ||
|  | ||
| /** | ||
| * Computes the cross-entropy loss between true labels and predicted labels. | ||
| * | ||
| * <p>Use this cross-entropy loss when there are only two label classes (assumed to be 0 and 1). For | ||
| * each example, there should be a single floating-point value per prediction. | ||
| * | ||
| * <p>Standalone usage: | ||
| * | ||
| * <pre> | ||
| * 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}}); | ||
| * BinaryCrossentropy bce = new BinaryCrossentropy(tf); | ||
| * Operand<TFloat32> result = bce.call(labels, predictions); | ||
| * // produces 0.815 | ||
| * </pre> | ||
| * | ||
| * <p>Calling with sample weight: | ||
| * | ||
| * <pre> | ||
| * Operand<TFloat32> sampleWeight = tf.constant(new float[] {1.f, 0.f}); | ||
| * Operand<TFloat32> result = bce.call(labels, predictions, sampleWeight); | ||
| * // produces 0.458f | ||
| * </pre> | ||
| * | ||
| * <p>Using <code>SUM</code> reduction type: | ||
| * | ||
| * <pre> | ||
| * BinaryCrossentropy bce = new BinaryCrossentropy(tf, Reduction.SUM); | ||
| * Operand<TFloat32> result = bce.call(labels, predictions); | ||
| * // produces 1.630f | ||
| * </pre> | ||
| * | ||
| * <p>Using <code>NONE</code> reduction type: | ||
| * | ||
| * <pre> | ||
| * BinaryCrossentropy bce = new BinaryCrossentropy(tf, Reduction.NONE); | ||
| * Operand<TFloat32> result = bce.call(labels, predictions); | ||
| * // produces [0.916f, 0.714f] | ||
| * </pre> | ||
| * | ||
| */ | ||
| public class BinaryCrossentropy extends Loss { | ||
| public static final boolean FROM_LOGITS_DEFAULT = false; | ||
| public static final float LABEL_SMOOTHING_DEFAULT = 0.0f; | ||
| public static final Reduction REDUCTION_DEFAULT = Reduction.AUTO; | ||
|  | ||
| private final boolean fromLogits; | ||
| private final float labelSmoothing; | ||
|  | ||
| /** | ||
| * Creates a Binary Crossentropy Loss using {@link Class#getSimpleName()} as the loss name, {@link | ||
| * #FROM_LOGITS_DEFAULT} for fromLogits, {@link #LABEL_SMOOTHING_DEFAULT} for labelSmoothing and a | ||
| * Loss Reduction of {@link * Reduction#AUTO} | ||
| * | ||
| * | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| */ | ||
| public BinaryCrossentropy(Ops tf) { | ||
| this(tf, null, FROM_LOGITS_DEFAULT, LABEL_SMOOTHING_DEFAULT, REDUCTION_DEFAULT); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a Binary Crossentropy loss using {@link Class#getSimpleName()} as the loss name, {@link | ||
| * #FROM_LOGITS_DEFAULT} for fromLogits, and {@link #LABEL_SMOOTHING_DEFAULT} for labelSmoothing | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param reduction Type of Reduction to apply to the loss. | ||
| */ | ||
| public BinaryCrossentropy(Ops tf, Reduction reduction) { | ||
| this(tf, null, FROM_LOGITS_DEFAULT, LABEL_SMOOTHING_DEFAULT, reduction); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a Binary Crossentropy loss using using {@link Class#getSimpleName()} as the loss name, | ||
| * labelSmoothing of {@link #LABEL_SMOOTHING_DEFAULT}, a reduction of {@link #REDUCTION_DEFAULT}, | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| */ | ||
| public BinaryCrossentropy(Ops tf, boolean fromLogits) { | ||
| this(tf, null, fromLogits, LABEL_SMOOTHING_DEFAULT, REDUCTION_DEFAULT); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a Binary Crossentropy loss using labelSmoothing of {@link #LABEL_SMOOTHING_DEFAULT} a | ||
| * reduction of {@link #REDUCTION_DEFAULT}. | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param name the name of the loss | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| */ | ||
| public BinaryCrossentropy(Ops tf, String name, boolean fromLogits) { | ||
| this(tf, name, fromLogits, LABEL_SMOOTHING_DEFAULT, REDUCTION_DEFAULT); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a Binary Crossentropy loss using using {@link Class#getSimpleName()} as the loss name, | ||
| * and a reduction of {@link #REDUCTION_DEFAULT}. | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| * @param labelSmoothing A number in the range, [0, 1]. When 0, no smoothing occurs. When > 0, | ||
| * compute the loss between the predicted labels and a smoothed version of the true labels, | ||
| * where the smoothing squeezes the labels towards 0.5. Larger values of label_smoothing | ||
|          | ||
| * correspond to heavier smoothing. | ||
| */ | ||
| public BinaryCrossentropy(Ops tf, boolean fromLogits, float labelSmoothing) { | ||
| this(tf, null, fromLogits, labelSmoothing, REDUCTION_DEFAULT); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a Binary Crossentropy loss using a reduction of {@link #REDUCTION_DEFAULT}. | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param name the name of the loss | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| * @param labelSmoothing A number in the range, [0, 1]. When 0, no smoothing occurs. When > 0, | ||
| * compute the loss between the predicted labels and a smoothed version of the true labels, | ||
| * where the smoothing squeezes the labels towards 0.5. Larger values of label_smoothing | ||
| * correspond to heavier smoothing. | ||
| */ | ||
| public BinaryCrossentropy(Ops tf, String name, boolean fromLogits, float labelSmoothing) { | ||
| this(tf, name, fromLogits, labelSmoothing, REDUCTION_DEFAULT); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a Binary Crossentropy loss | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| * @param labelSmoothing A number in the range, [0, 1]. When 0, no smoothing occurs. When > 0, | ||
| * compute the loss between the predicted labels and a smoothed version of the true labels, | ||
| * where the smoothing squeezes the labels towards 0.5. Larger values of label_smoothing | ||
| * correspond to heavier smoothing. | ||
| * @param reduction Type of Reduction to apply to the loss. | ||
| */ | ||
| public BinaryCrossentropy( | ||
| Ops tf, boolean fromLogits, float labelSmoothing, Reduction reduction) { | ||
| this(tf, null, fromLogits, labelSmoothing, reduction); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a Binary Crossentropy loss | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param name the name of the loss | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| * @param labelSmoothing A number in the range, [0, 1]. When 0, no smoothing occurs. When > 0, | ||
| * compute the loss between the predicted labels and a smoothed version of the true labels, | ||
| * where the smoothing squeezes the labels towards 0.5. Larger values of label_smoothing | ||
| * correspond to heavier smoothing. | ||
| * @param reduction Type of Reduction to apply to the loss. | ||
| */ | ||
| public BinaryCrossentropy( | ||
| Ops tf, String name, boolean fromLogits, float labelSmoothing, Reduction reduction) { | ||
| super(tf, name, reduction); | ||
| this.fromLogits = fromLogits; | ||
| this.labelSmoothing = labelSmoothing; | ||
| } | ||
|  | ||
| /** {@inheritDoc} */ | ||
| @Override | ||
| public <T extends TNumber, U extends TNumber> Operand<T> call( | ||
| Operand<U> labels, Operand<T> predictions, Operand<T> sampleWeights) { | ||
| Operand<T> losses = | ||
| Losses.binaryCrossentropy(tf, labels, predictions, fromLogits, labelSmoothing); | ||
| return LossesImpl.computeWeightedLoss(tf, losses, getReduction(), sampleWeights); | ||
|          | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| package org.tensorflow.framework.losses; | ||
|  | ||
| import org.tensorflow.Operand; | ||
| import org.tensorflow.framework.losses.impl.LossesImpl; | ||
| import org.tensorflow.op.Ops; | ||
| import org.tensorflow.types.family.TNumber; | ||
|  | ||
| /** | ||
| * Computes the crossentropy loss between the labels and predictions. | ||
| * | ||
| * <p>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 | ||
| * {@link SparseCategoricalCrossentropy} loss. There should be <code># classes</code> floating point | ||
| * values per feature. | ||
| * | ||
| * <p>Standalone usage: | ||
| * | ||
| * <pre> | ||
| * 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 | ||
| * </pre> | ||
| * | ||
| * <p>Calling with sample weight: | ||
| * | ||
| * <pre> | ||
| * Operand<TFloat32> sampleWeight = tf.constant(new float[] {0.3f, 0.7f}); | ||
| * Operand<TFloat32> result = cce.call(labels, predictions, sampleWeight); | ||
| * // produces 0.814f | ||
| * </pre> | ||
| * | ||
| * <p>Using <code>SUM</code> reduction type: | ||
| * | ||
| * <pre> | ||
| * CategoricalCrossentropy cce = new CategoricalCrossentropy(tf, Reduction.SUM); | ||
| * Operand<TFloat32> result = cce.call(labels, predictions); | ||
| * // produces 2.354f | ||
| * </pre> | ||
| * | ||
| * <p>Using <code>NONE</code> reduction type: | ||
| * | ||
| * <pre> | ||
| * CategoricalCrossentropy cce = | ||
| * new CategoricalCrossentropy(tf, Reduction.NONE); | ||
| * Operand<TFloat32> result = cce.call(labels, predictions); | ||
| * // produces [0.0513f, 2.303f] | ||
| * </pre> | ||
| */ | ||
| public class CategoricalCrossentropy extends Loss { | ||
| public static final boolean FROM_LOGITS_DEFAULT = false; | ||
| public static final float LABEL_SMOOTHING_DEFAULT = 0.0f; | ||
| public static final Reduction REDUCTION_DEFAULT = Reduction.AUTO; | ||
| public static final int DEFAULT_AXIS = -1; | ||
|  | ||
| private final boolean fromLogits; | ||
| private final float labelSmoothing; | ||
| private final int axis; | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss using {@link Class#getSimpleName()} as the loss name, | ||
| * {@link #FROM_LOGITS_DEFAULT} for fromLogits, {@link #LABEL_SMOOTHING_DEFAULT} for | ||
| * labelSmoothing, a Loss Reduction of {@link * Reduction#AUTO}, and an axis of {@link | ||
|          | ||
| * #DEFAULT_AXIS} | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| */ | ||
| public CategoricalCrossentropy(Ops tf) { | ||
| this(tf, null, FROM_LOGITS_DEFAULT, LABEL_SMOOTHING_DEFAULT, REDUCTION_DEFAULT, DEFAULT_AXIS); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss using {@link #FROM_LOGITS_DEFAULT} for fromLogits, | ||
| * {@link #LABEL_SMOOTHING_DEFAULT} for labelSmoothing, a Loss Reduction of {@link * | ||
| * Reduction#AUTO}, and an axis of {@link #DEFAULT_AXIS} | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param name the name of this loss | ||
| */ | ||
| public CategoricalCrossentropy(Ops tf, String name) { | ||
| this(tf, name, FROM_LOGITS_DEFAULT, LABEL_SMOOTHING_DEFAULT, REDUCTION_DEFAULT, DEFAULT_AXIS); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss using {@link Class#getSimpleName()} as the loss name, | ||
| * {@link #FROM_LOGITS_DEFAULT} for fromLogits, {@link #LABEL_SMOOTHING_DEFAULT} for | ||
| * labelSmoothing and an axis of {@link #DEFAULT_AXIS} | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param reduction Type of Reduction to apply to loss. | ||
| */ | ||
| public CategoricalCrossentropy(Ops tf, Reduction reduction) { | ||
| this(tf, null, FROM_LOGITS_DEFAULT, LABEL_SMOOTHING_DEFAULT, reduction, DEFAULT_AXIS); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss {@link #FROM_LOGITS_DEFAULT} for fromLogits, {@link | ||
| * #LABEL_SMOOTHING_DEFAULT} for labelSmoothing, and an axis of {@link #DEFAULT_AXIS} | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param name the name of this loss | ||
| * @param reduction Type of Reduction to apply to loss. | ||
| */ | ||
| public CategoricalCrossentropy(Ops tf, String name, Reduction reduction) { | ||
| this(tf, name, FROM_LOGITS_DEFAULT, LABEL_SMOOTHING_DEFAULT, reduction, DEFAULT_AXIS); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss using {@link Class#getSimpleName()} as the loss name, | ||
| * {@link #LABEL_SMOOTHING_DEFAULT} for labelSmoothing, a Loss Reduction of {@link * | ||
| * Reduction#AUTO}, and an axis of {@link #DEFAULT_AXIS} | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| */ | ||
| public CategoricalCrossentropy(Ops tf, boolean fromLogits) { | ||
| this(tf, null, fromLogits, LABEL_SMOOTHING_DEFAULT, REDUCTION_DEFAULT, DEFAULT_AXIS); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss using {@link #LABEL_SMOOTHING_DEFAULT} for | ||
| * labelSmoothing, a Loss Reduction of {@link * Reduction#AUTO}, and a channel axis of {@link | ||
| * #DEFAULT_AXIS} | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param name the name of this loss | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| */ | ||
| public CategoricalCrossentropy(Ops tf, String name, boolean fromLogits) { | ||
| this(tf, name, fromLogits, LABEL_SMOOTHING_DEFAULT, REDUCTION_DEFAULT, DEFAULT_AXIS); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss using {@link Class#getSimpleName()} as the loss name, | ||
| * a Loss Reduction of {@link * Reduction#AUTO}, and a channel axis of {@link #DEFAULT_AXIS} | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| * @param labelSmoothing Float in [0, 1]. When 0, no smoothing occurs. When > 0, we compute the | ||
|          | ||
| * loss between the predicted labels and a smoothed version of the true labels, where the | ||
| * smoothing squeezes the labels towards 0.5. Larger values of label_smoothing correspond to | ||
| * heavier smoothing. | ||
| */ | ||
| public CategoricalCrossentropy(Ops tf, boolean fromLogits, float labelSmoothing) { | ||
| this(tf, null, fromLogits, labelSmoothing, REDUCTION_DEFAULT, DEFAULT_AXIS); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss using a Loss Reduction of {@link * Reduction#AUTO}, | ||
| * and a channel axis of {@link #DEFAULT_AXIS} | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param name the name of this loss | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| * @param labelSmoothing Float in [0, 1]. When 0, no smoothing occurs. When > 0, we compute the | ||
| * loss between the predicted labels and a smoothed version of the true labels, where the | ||
| * smoothing squeezes the labels towards 0.5. Larger values of label_smoothing correspond to | ||
|          | ||
| * heavier smoothing. | ||
| */ | ||
| public CategoricalCrossentropy(Ops tf, String name, boolean fromLogits, float labelSmoothing) { | ||
| this(tf, name, fromLogits, labelSmoothing, REDUCTION_DEFAULT, DEFAULT_AXIS); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss using {@link Class#getSimpleName()} as the loss name | ||
| * and a channel axis of {@link #DEFAULT_AXIS} | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| * @param labelSmoothing Float in [0, 1]. When 0, no smoothing occurs. When > 0, we compute the | ||
| * loss between the predicted labels and a smoothed version of the true labels, where the | ||
|          | ||
| * smoothing squeezes the labels towards 0.5. Larger values of label_smoothing correspond to | ||
| * heavier smoothing. | ||
| * @param reduction Type of Reduction to apply to loss. | ||
| */ | ||
| public CategoricalCrossentropy( | ||
| Ops tf, boolean fromLogits, float labelSmoothing, Reduction reduction) { | ||
| this(tf, null, fromLogits, labelSmoothing, reduction, DEFAULT_AXIS); | ||
| } | ||
|  | ||
| /** | ||
| * Creates a categorical cross entropy Loss | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param name the name of this loss | ||
| * @param fromLogits Whether to interpret predictions as a tensor of logit values | ||
| * @param labelSmoothing Float in [0, 1]. When 0, no smoothing occurs. When > 0, we compute the | ||
| * loss between the predicted labels and a smoothed version of the true labels, where the | ||
| * smoothing squeezes the labels towards 0.5. Larger values of label_smoothing correspond to | ||
|          | ||
| * heavier smoothing. | ||
| * @param reduction Type of Reduction to apply to loss. | ||
| * @param axis The channels axis. <code>axis=-1</code> corresponds to data format `Channels Last' | ||
| * and <code>axis=1</code> corresponds to data format 'Channels First'. | ||
| */ | ||
| public CategoricalCrossentropy( | ||
| Ops tf, | ||
| String name, | ||
| boolean fromLogits, | ||
| float labelSmoothing, | ||
| Reduction reduction, | ||
| int axis) { | ||
| super(tf, name, reduction); | ||
| this.fromLogits = fromLogits; | ||
| this.labelSmoothing = labelSmoothing; | ||
| this.axis = axis; | ||
| } | ||
|  | ||
| /** {@inheritDoc} */ | ||
| @Override | ||
| public <T extends TNumber, U extends TNumber> Operand<T> call( | ||
| Operand<U> labels, Operand<T> predictions, Operand<T> sampleWeights) { | ||
| Operand<T> losses = | ||
| Losses.categoricalCrossentropy(tf, labels, predictions, fromLogits, labelSmoothing, axis); | ||
| return LossesImpl.computeWeightedLoss(tf, losses, getReduction(), sampleWeights); | ||
|          | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extraneous
*There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted