Skip to content

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

Merged
merged 27 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c57a2e7
Merge pull request #3 from tensorflow/master
JimClarke5 Oct 8, 2020
9cc2675
Initial checkin to rebase to Initialziers to pick up changes to ndarr…
JimClarke5 Oct 5, 2020
2508f5e
Initial Checkin for losses
JimClarke5 Oct 8, 2020
17e96b5
Fix reshape in sparseCategoricalCrossentropy()
JimClarke5 Oct 8, 2020
ee1c48a
Apply various fixes to JavaDoc
JimClarke5 Oct 11, 2020
287c96e
Change Tuple to LossTuple
JimClarke5 Oct 11, 2020
642069c
Repair JavaDOx
JimClarke5 Oct 11, 2020
249b651
Fixed AllAxis to hanlde dynamic shape when static shape rank is unknown.
JimClarke5 Oct 11, 2020
794cfdc
change method name allAxis to allAxes
JimClarke5 Oct 11, 2020
fb26c59
change private method binaryCrossentropy to binaryCrossentropyHelper
JimClarke5 Oct 13, 2020
928ef06
Fixed squeezeOrExpandDimensions to make sure the updated labels, pred…
JimClarke5 Oct 13, 2020
2bc54dd
Fix JavaDoc,
JimClarke5 Oct 27, 2020
951443b
Fix unused imports and add @SuppressWarnings("unchecked") for casts.
JimClarke5 Oct 27, 2020
ebac9e8
Add copyright
JimClarke5 Oct 29, 2020
d8f3254
Add CastHelper and used that for all casts
JimClarke5 Oct 29, 2020
02573b5
Fix JavaDoc, change snake case to camel case.
JimClarke5 Nov 9, 2020
0bf49fe
Change class LossesImpl to LossesHelper
JimClarke5 Nov 11, 2020
0eae9ee
Remove commented out JavaDoc
JimClarke5 Nov 12, 2020
b211937
Changed method name from smoothLabelsBinaryX to smoothBinaryLabels,
JimClarke5 Nov 13, 2020
3e0669e
Fixed JavaDoc for labelSmoothing
JimClarke5 Nov 13, 2020
914f16f
Fixed JavaDoc to change label_smoothing to labelSmoothing.
JimClarke5 Nov 13, 2020
7eefbb7
Fix formatting
JimClarke5 Nov 13, 2020
b87ad16
replace label_smoothing with labelSmoothing.
JimClarke5 Nov 13, 2020
c43cd21
Add copyright to test cases
JimClarke5 Nov 16, 2020
4d9fd24
Fix copyright to attribute TensorFlow Authors.
JimClarke5 Nov 16, 2020
d56d8d9
Fix typo on broadcast in JavaDoc
JimClarke5 Nov 16, 2020
744e324
Fix typo on broadcast in JavaDoc
JimClarke5 Nov 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public BinaryCrossentropy(Ops tf, String name, boolean fromLogits) {
* @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
* where the smoothing squeezes the labels towards 0.5. Larger values of labelSmoothing
* correspond to heavier smoothing.
*/
public BinaryCrossentropy(Ops tf, boolean fromLogits, float labelSmoothing) {
Expand All @@ -140,7 +140,7 @@ public BinaryCrossentropy(Ops tf, boolean fromLogits, float labelSmoothing) {
* @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
* where the smoothing squeezes the labels towards 0.5. Larger values of labelSmoothing
* correspond to heavier smoothing.
*/
public BinaryCrossentropy(Ops tf, String name, boolean fromLogits, float labelSmoothing) {
Expand All @@ -154,7 +154,7 @@ public BinaryCrossentropy(Ops tf, String name, boolean fromLogits, float labelSm
* @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
* where the smoothing squeezes the labels towards 0.5. Larger values of labelSmoothing
* correspond to heavier smoothing.
* @param reduction Type of Reduction to apply to the loss.
*/
Expand All @@ -170,26 +170,28 @@ public BinaryCrossentropy(Ops tf, boolean fromLogits, float labelSmoothing, Redu
* @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
* where the smoothing squeezes the labels towards 0.5. Larger values of labelSmoothing
* correspond to heavier smoothing.
* @param reduction Type of Reduction to apply to the loss.
* @throws IllegalArgumentException if labelSmoothing is not in the inclusive range of 0. - 1.
*/
public BinaryCrossentropy(
Ops tf, String name, boolean fromLogits, float labelSmoothing, Reduction reduction) {
super(tf, name, reduction);
if(labelSmoothing < 0 || labelSmoothing > 1)
throw new IllegalArgumentException("labelSmoothing must be >= 0. and <= 1, found " + labelSmoothing);
if (labelSmoothing < 0 || labelSmoothing > 1)
throw new IllegalArgumentException(
"labelSmoothing must be >= 0. and <= 1, found " + labelSmoothing);
this.fromLogits = fromLogits;
this.labelSmoothing = labelSmoothing;
}

/**
* Generates an Operand that calculates the loss.
*
* If run in Graph mode, the computation will throw {@link org.tensorflow.exceptions.TFInvalidArgumentException}
* if the predictions values are outside the range o [0. to 1.]. In Eager Mode, this call
* will throw {@link IllegalArgumentException}, if the predictions values are outside the range o [0. to 1.]
* <p>If run in Graph mode, the computation will throw {@link
* org.tensorflow.exceptions.TFInvalidArgumentException} if the predictions values are outside the
* range o [0. to 1.]. In Eager Mode, this call will throw {@link IllegalArgumentException}, if
* the predictions values are outside the range o [0. to 1.]
*
* @param labels the truth values or labels
* @param predictions the predictions, values must be in the range [0. to 1.] inclusive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.tensorflow.framework.losses.impl.LossesHelper;
import org.tensorflow.op.Ops;
import org.tensorflow.types.family.TNumber;

import static org.tensorflow.framework.utils.CastHelper.cast;

/**
Expand Down Expand Up @@ -137,8 +138,8 @@ public CategoricalCrossentropy(Ops tf, boolean fromLogits) {

/**
* Creates a categorical cross entropy Loss using {@link #LABEL_SMOOTHING_DEFAULT} for
* labelSmoothing, a Loss Reduction of {@link Loss#REDUCTION_DEFAULT}, and a channel axis of {@link
* #DEFAULT_AXIS}
* labelSmoothing, a Loss Reduction of {@link Loss#REDUCTION_DEFAULT}, and a channel axis of
* {@link #DEFAULT_AXIS}
*
* @param tf the TensorFlow Ops
* @param name the name of this loss
Expand Down Expand Up @@ -169,10 +170,9 @@ public CategoricalCrossentropy(Ops tf, boolean fromLogits, float labelSmoothing)
* @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 labelSmoothing Float in <code>[0, 1]</code>. When <code>&gt; 0</code>, label values are smoothed, meaning the
* confidence on label values are relaxed. e.g. <code>label_smoothing=0.2<code> means that we will use a
* value of </code>0.1<code> for label </code>0<code> and </code>0.9<code> for label </code>1<code>
*/
public CategoricalCrossentropy(Ops tf, String name, boolean fromLogits, float labelSmoothing) {
this(tf, name, fromLogits, labelSmoothing, REDUCTION_DEFAULT, DEFAULT_AXIS);
Expand All @@ -184,10 +184,9 @@ public CategoricalCrossentropy(Ops tf, String name, boolean fromLogits, float la
*
* @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 labelSmoothing Float in <code>[0, 1]</code>. When <code>&gt; 0</code>, label values are smoothed, meaning the
* confidence on label values are relaxed. e.g. <code>label_smoothing=0.2<code> means that we will use a
* alue of </code>0.1<code> for label </code>0<code> and </code>0.9<code> for label </code>1<code>
* @param reduction Type of Reduction to apply to loss.
*/
public CategoricalCrossentropy(
Expand All @@ -201,10 +200,9 @@ public CategoricalCrossentropy(
* @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 labelSmoothing Float in <code>[0, 1]</code>. When <code>&gt; 0</code>, label values are smoothed, meaning the
* confidence on label values are relaxed. e.g. <code>label_smoothing=0.2<code> means that we will use a
* value of </code>0.1<code> for label </code>0<code> and </code>0.9<code> for label </code>1<code>
* @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'.
Expand All @@ -218,8 +216,9 @@ public CategoricalCrossentropy(
Reduction reduction,
int axis) {
super(tf, name, reduction);
if(labelSmoothing < 0 || labelSmoothing > 1)
throw new IllegalArgumentException("labelSmoothing must be >= 0. and <= 1, found " + labelSmoothing);
if (labelSmoothing < 0 || labelSmoothing > 1)
throw new IllegalArgumentException(
"labelSmoothing must be >= 0. and <= 1, found " + labelSmoothing);
this.fromLogits = fromLogits;
this.labelSmoothing = labelSmoothing;
this.axis = axis;
Expand All @@ -228,9 +227,10 @@ public CategoricalCrossentropy(
/**
* Generates an Operand that calculates the loss.
*
* If run in Graph mode, the computation will throw {@link org.tensorflow.exceptions.TFInvalidArgumentException}
* if the predictions values are outside the range o [0. to 1.]. In Eager Mode, this call
* will throw {@link IllegalArgumentException}, if the predictions values are outside the range o [0. to 1.]
* <p>If run in Graph mode, the computation will throw {@link
* org.tensorflow.exceptions.TFInvalidArgumentException} if the predictions values are outside the
* range o [0. to 1.]. In Eager Mode, this call will throw {@link IllegalArgumentException}, if
* the predictions values are outside the range o [0. to 1.]
*
* @param labels the truth values or labels
* @param predictions the predictions, values must be in the range [0. to 1.] inclusive.
Expand All @@ -248,23 +248,24 @@ public CategoricalCrossentropy(
*/
@Override
public <T extends TNumber, U extends TNumber> Operand<T> call(
Operand<U> labels, Operand<T> predictions, Operand<T> sampleWeights) {
Operand<U> labels, Operand<T> predictions, Operand<T> sampleWeights) {
Operand<T> lPredictions;
if (!fromLogits) {
// add predictions range check for 0 - 1
lPredictions =
LossesHelper.rangeCheck(
getTF(),
"predictions range check [0-1]",
predictions,
cast(getTF(), getTF().constant(0), predictions.asOutput().dataType()),
cast(getTF(), getTF().constant(1), predictions.asOutput().dataType()));
LossesHelper.rangeCheck(
getTF(),
"predictions range check [0-1]",
predictions,
cast(getTF(), getTF().constant(0), predictions.asOutput().dataType()),
cast(getTF(), getTF().constant(1), predictions.asOutput().dataType()));

} else {
lPredictions = predictions;
}
Operand<T> losses =
Losses.categoricalCrossentropy(getTF(), labels, lPredictions, fromLogits, labelSmoothing, axis);
Losses.categoricalCrossentropy(
getTF(), labels, lPredictions, fromLogits, labelSmoothing, axis);
return LossesHelper.computeWeightedLoss(getTF(), losses, getReduction(), sampleWeights);
}
}
Loading