Skip to content
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) #9

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -69,7 +69,7 @@
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 int DEFAULT_AXIS = -1;
public static final int DEFAULT_AXIS = Losses.CHANNELS_LAST;

private final boolean fromLogits;
private final float labelSmoothing;
Expand Down Expand Up @@ -203,8 +203,9 @@ public CategoricalCrossentropy(
* 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 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'.
* @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".
* {@link Losses#CHANNELS_LAST} and {@link Losses#CHANNELS_FIRST}
* @throws IllegalArgumentException if labelSmoothing is not in the inclusive range of 0. - 1.
*/
public CategoricalCrossentropy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class Losses {
/** Default Fuzz factor. */
public static final float EPSILON = 1e-7f;

public static final int CHANNELS_LAST = -1;
public static final int CHANNELS_FIRST = 1;

/**
* Calculates the mean absolute error between labels and predictions.
*
Expand Down Expand Up @@ -239,7 +242,7 @@ public static <T extends TNumber, U extends TNumber> Operand<T> categoricalCross
tLabels = smoothCategoricalLabels(tf, tLabels, labelSmoothing);
}
if (fromLogits) {
return tf.nn.softmaxCrossEntropyWithLogits(tLabels, predictions, -1);
return tf.nn.softmaxCrossEntropyWithLogits(tLabels, predictions, axis);
}
/* TODO
if (!(predictions instanceof Variable) && (!tf.scope().env().isEager())) {
Expand Down
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 &gt; 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.
*
* @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 &gt; 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 &gt; 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);
}
}
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);
}
}
Loading