-
Notifications
You must be signed in to change notification settings - Fork 202
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
Metrics Phase 1 #180
Metrics Phase 1 #180
Changes from all commits
c57a2e7
09fc07e
a99dcb4
ba294ea
04f419a
ad466ee
092b47d
4887b5b
04eeea6
dcb2414
82f18bf
9aa1511
1097722
bc0f468
41876d5
61af528
c121c07
e9ee98f
9788983
8857a66
34a779f
748f16d
212541b
f0d72d2
8b49c60
20c6e98
d3d7ee9
fe86b0b
0edd114
7d78fd3
02e7ebf
af1b49f
7732601
a737334
253cc73
22cb5b2
4d1aa20
2b7f6ed
3800b71
3045999
9eb5adf
187c17c
050fe28
b640406
3715513
a1c1976
6641fca
fa76043
e136f4d
e00f2ef
bc6c64b
02da963
44cdc35
49370b9
24b4125
43c6b7b
78e9dab
5508969
c662524
512a153
0663c3c
122e06b
b7b14b1
13639d3
561322f
2a13012
36f3a69
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,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); | ||
} | ||
} |
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. | ||
karllessard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure there's a bug in the method called here, return tf.nn.softmaxCrossEntropyWithLogits(tLabels, predictions, -1); I believe the final parameter should be It's not a bug in this PR, of course, but perhaps worth fixing in this PR to reduce process? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In TF Python, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :-/ Isn't that a bug in TF Python? Seems clear that the Interestingly, that latter |
||
getTF(), labels, predictions, fromLogits, labelSmoothing, axis); | ||
} | ||
} |
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); | ||
} | ||
} |
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); | ||
} | ||
} |
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.
Did you intend to add a "See" in front of the links?