Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 226
Metrics Phase 1#180
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
Uh oh!
There was an error while loading. Please reload this page.
Metrics Phase 1 #180
Changes from all commits
c57a2e709fc07ea99dcb4ba294ea04f419aad466ee092b47d4887b5b04eeea6dcb241482f18bf9aa15111097722bc0f46841876d561af528c121c07e9ee98f97889838857a6634a779f748f16d212541bf0d72d28b49c6020c6e98d3d7ee9fe86b0b0edd1147d78fd302e7ebfaf1b49f7732601a737334253cc7322cb5b24d1aa202b7f6ed3800b7130459999eb5adf187c17c050fe28b6404063715513a1c19766641fcafa76043e136f4de00f2efbc6c64b02da96344cdc3549370b924b412543c6b7b78e9dab5508969c662524512a1530663c3c122e06bb7b14b113639d3561322f2a1301236f3a69File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!There was an error while loading. Please reload this page. | ||
| * | ||
| * @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( | ||
Contributor 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, returntf.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? ContributorAuthor 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, Contributor 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); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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?