forked from tensorflow/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from tensorflow/master
Metrics Phase 1 (tensorflow#180)
- Loading branch information
Showing
45 changed files
with
4,497 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
tensorflow-framework/src/main/java/org/tensorflow/framework/metrics/BinaryCrossentropy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
=======================================================================*/ | ||
package org.tensorflow.framework.metrics; | ||
|
||
import org.tensorflow.Operand; | ||
import org.tensorflow.framework.losses.Losses; | ||
import org.tensorflow.framework.metrics.impl.LossMetric; | ||
import org.tensorflow.framework.metrics.impl.MeanMetricWrapper; | ||
import org.tensorflow.op.Ops; | ||
import org.tensorflow.types.family.TNumber; | ||
|
||
/** | ||
* A Metric that computes the binary cross-entropy loss between true labels and predicted labels. | ||
* | ||
* <p>This is the crossentropy metric class to be used when there are only two label classes (0 and | ||
* 1). | ||
* | ||
* @param <U> the data type for the predictions. | ||
* @param <T> The data type for the metric result | ||
*/ | ||
public class BinaryCrossentropy<U extends TNumber, T extends TNumber> | ||
extends MeanMetricWrapper<U, T> implements LossMetric<T> { | ||
|
||
private final boolean fromLogits; | ||
private final float labelSmoothing; | ||
|
||
/** | ||
* Creates a BinaryCrossentropy metric | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param name the name of this metric, if null then metric name is {@link Class#getSimpleName()}. | ||
* @param fromLogits Whether to interpret predictions as a tensor of logit values as opposed to a probability distribution. | ||
* @param labelSmoothing value used to smooth labels, 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 seed the seed for random number generation. An initializer created with a given seed | ||
* will always produce the same random tensor for a given shape and data type. | ||
* @param type the type for the variables and result | ||
*/ | ||
public BinaryCrossentropy( | ||
Ops tf, String name, boolean fromLogits, float labelSmoothing, long seed, Class<T> type) { | ||
super(tf, name, seed, type); | ||
setLoss(this); | ||
this.fromLogits = fromLogits; | ||
this.labelSmoothing = labelSmoothing; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public <V extends TNumber> Operand<T> call(Operand<V> labels, Operand<T> predictions) { | ||
return Losses.binaryCrossentropy(getTF(), labels, predictions, fromLogits, labelSmoothing); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
...low-framework/src/main/java/org/tensorflow/framework/metrics/CategoricalCrossentropy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
=======================================================================*/ | ||
package org.tensorflow.framework.metrics; | ||
|
||
import org.tensorflow.Operand; | ||
import org.tensorflow.framework.losses.Losses; | ||
import org.tensorflow.framework.metrics.impl.LossMetric; | ||
import org.tensorflow.framework.metrics.impl.MeanMetricWrapper; | ||
import org.tensorflow.op.Ops; | ||
import org.tensorflow.types.family.TNumber; | ||
|
||
/** | ||
* A Metric that computes the categorical cross-entropy loss between true labels and predicted | ||
* labels. | ||
* | ||
* <p>This is the crossentropy metric class to be used when there are multiple label classes (2 or | ||
* more). The labels should be given as a one_hot representation. eg., When labels values are <code> | ||
* [2, 0, 1]</code>, the labels Operand contains = <code>[[0, 0, 1], [1, 0, 0], [0, 1, 0]] | ||
* </code>. | ||
* | ||
* @param <U> the data type for the predictions. | ||
* @param <T> The data type for the metric result | ||
*/ | ||
public class CategoricalCrossentropy<U extends TNumber, T extends TNumber> | ||
extends MeanMetricWrapper<U, T> implements LossMetric<T> { | ||
|
||
private final boolean fromLogits; | ||
private final float labelSmoothing; | ||
private final int axis; | ||
|
||
/** | ||
* Creates a CategoricalCrossentropy metric that computes the crossentropy metric between the | ||
* labels and predictions. | ||
* | ||
* <p>Uses a {@link Losses#CHANNELS_LAST} for the channel axis. | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param name the name of this metric, if null then metric name is {@link Class#getSimpleName()}. | ||
* @param fromLogits Whether to interpret predictions as a tensor of logit values oras opposed to a probability distribution. | ||
* @param labelSmoothing value used to smooth labels, When > 0, label values are smoothed, | ||
* meaning the confidence on label values are relaxed. e.g. <code>labelSmoothing=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 seed the seed for random number generation. An initializer created with a given seed | ||
* will always produce the same random tensor for a given shape and data type. | ||
* @param type the type for the variables and result | ||
*/ | ||
public CategoricalCrossentropy( | ||
Ops tf, String name, boolean fromLogits, float labelSmoothing, long seed, Class<T> type) { | ||
this(tf, name, fromLogits, labelSmoothing, Losses.CHANNELS_LAST, seed, type); | ||
} | ||
|
||
/** | ||
* Creates a CategoricalCrossentropy metric that computes the crossentropy metric between the | ||
* labels and predictions. | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param name the name of this metric, if null then metric name is {@link Class#getSimpleName()}. | ||
* @param fromLogits Whether to interpret predictions as a tensor of logit values as opposed to a probability distribution. | ||
* @param labelSmoothing value used to smooth labels, When > 0, label values are smoothed, | ||
* meaning the confidence on label values are relaxed. e.g. <code>labelSmoothing=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 axis Int specifying the channels axis. <code>axis={@link Losses#CHANNELS_LAST}</code> | ||
* corresponds to data format <code>channels_last</code>, and <code> | ||
* axis={@link Losses#CHANNELS_FIRST}</code> corresponds to data format <code> | ||
* channels_first</code>. | ||
* @param seed the seed for random number generation. An initializer created with a given seed | ||
* will always produce the same random tensor for a given shape and data type. | ||
* @param type the type for the variables and result | ||
*/ | ||
public CategoricalCrossentropy( | ||
Ops tf, | ||
String name, | ||
boolean fromLogits, | ||
float labelSmoothing, | ||
int axis, | ||
long seed, | ||
Class<T> type) { | ||
super(tf, name, seed, type); | ||
setLoss(this); | ||
this.fromLogits = fromLogits; | ||
this.labelSmoothing = labelSmoothing; | ||
this.axis = axis; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public <V extends TNumber> Operand<T> call(Operand<V> labels, Operand<T> predictions) { | ||
return Losses.categoricalCrossentropy( | ||
getTF(), labels, predictions, fromLogits, labelSmoothing, axis); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
tensorflow-framework/src/main/java/org/tensorflow/framework/metrics/CategoricalHinge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
=======================================================================*/ | ||
package org.tensorflow.framework.metrics; | ||
|
||
import org.tensorflow.Operand; | ||
import org.tensorflow.framework.losses.Losses; | ||
import org.tensorflow.framework.metrics.impl.LossMetric; | ||
import org.tensorflow.framework.metrics.impl.MeanMetricWrapper; | ||
import org.tensorflow.op.Ops; | ||
import org.tensorflow.types.family.TNumber; | ||
|
||
/** | ||
* A Metric that computes the categorical hinge loss metric between labels and predictions. | ||
* | ||
* @param <U> the data type for the predictions. | ||
* @param <T> The data type for the metric result | ||
*/ | ||
public class CategoricalHinge<U extends TNumber, T extends TNumber> extends MeanMetricWrapper<U, T> | ||
implements LossMetric<T> { | ||
|
||
/** | ||
* Creates a CategoricalHinge metric | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param name the name of this metric, if null then metric name is {@link Class#getSimpleName()}. | ||
* @param seed the seed for random number generation. An initializer created with a given seed | ||
* will always produce the same random tensor for a given shape and data type. | ||
* @param type the type for the variables and result | ||
*/ | ||
public CategoricalHinge(Ops tf, String name, long seed, Class<T> type) { | ||
super(tf, name, seed, type); | ||
setLoss(this); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public <V extends TNumber> Operand<T> call(Operand<V> labels, Operand<T> predictions) { | ||
return Losses.categoricalHinge(getTF(), labels, predictions); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
tensorflow-framework/src/main/java/org/tensorflow/framework/metrics/CosineSimilarity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
=======================================================================*/ | ||
package org.tensorflow.framework.metrics; | ||
|
||
import org.tensorflow.Operand; | ||
import org.tensorflow.framework.metrics.impl.LossMetric; | ||
import org.tensorflow.framework.metrics.impl.MeanMetricWrapper; | ||
import org.tensorflow.op.Ops; | ||
import org.tensorflow.types.family.TNumber; | ||
|
||
/** | ||
* A metric that computes the cosine similarity metric between labels and predictions. | ||
* | ||
* @param <U> the data type for the predictions. | ||
* @param <T> The data type for the metric result. | ||
*/ | ||
public class CosineSimilarity<U extends TNumber, T extends TNumber> extends MeanMetricWrapper<U, T> | ||
implements LossMetric<T> { | ||
public static final int DEFAULT_AXIS = -1; | ||
private final int[] axis; | ||
|
||
/** | ||
* Creates a metric that computes the cosine similarity metric between labels and predictions with | ||
* a default axis, {@link #DEFAULT_AXIS} | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param name the name of this metric, if null then metric name is {@link Class#getSimpleName()}. | ||
* @param seed the seed for random number generation. An initializer created with a given seed | ||
* will always produce the same random tensor for a given shape and data type. | ||
* @param type the type for the variables and result | ||
*/ | ||
public CosineSimilarity(Ops tf, String name, long seed, Class<T> type) { | ||
this(tf, name, DEFAULT_AXIS, seed, type); | ||
} | ||
|
||
/** | ||
* Creates a metric that computes the cosine similarity metric between labels and predictions. | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param name the name of this metric, if null then metric name is {@link Class#getSimpleName()}. | ||
* @param axis The dimension along which the cosine similarity is computed. | ||
* @param seed the seed for random number generation. An initializer created with a given seed | ||
* will always produce the same random tensor for a given shape and data type. | ||
* @param type the type for the variables and result | ||
*/ | ||
public CosineSimilarity(Ops tf, String name, int axis, long seed, Class<T> type) { | ||
this(tf, name, new int[] {axis}, seed, type); | ||
} | ||
/** | ||
* Creates a CosineSimilarity metric | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param name the name of this metric, if null then metric name is {@link Class#getSimpleName()}. | ||
* @param axis The dimension along which the cosine similarity is computed. | ||
* @param seed the seed for random number generation. An initializer created with a given seed | ||
* will always produce the same random tensor for a given shape and data type. | ||
* @param type the type for the variables and result | ||
*/ | ||
public CosineSimilarity(Ops tf, String name, int[] axis, long seed, Class<T> type) { | ||
super(tf, name, seed, type); | ||
this.axis = axis; | ||
setLoss(this); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public <V extends TNumber> Operand<T> call(Operand<V> labels, Operand<T> predictions) { | ||
// NOTE: cosineProximity is a different algorithm than Losses.cosineSimilarity | ||
return Metrics.cosineProximity(getTF(), labels, predictions, axis); | ||
} | ||
} |
Oops, something went wrong.