aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/losses
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2016-12-12 11:54:33 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-12-12 12:04:21 -0800
commit552a64130924f16d448f655a8a325c839ad6cbcc (patch)
treeeda86c1502645fdfb974b4de0f705fcc724b7d0c /tensorflow/contrib/losses
parent2eaaadae5a0afc0a92ed81cca550d57bb9b29cc1 (diff)
Remove deprecated weight and target args.
Change: 141792366
Diffstat (limited to 'tensorflow/contrib/losses')
-rw-r--r--tensorflow/contrib/losses/python/losses/loss_ops.py210
-rw-r--r--tensorflow/contrib/losses/python/losses/loss_ops_test.py207
2 files changed, 51 insertions, 366 deletions
diff --git a/tensorflow/contrib/losses/python/losses/loss_ops.py b/tensorflow/contrib/losses/python/losses/loss_ops.py
index 780def4269..49eed50ed1 100644
--- a/tensorflow/contrib/losses/python/losses/loss_ops.py
+++ b/tensorflow/contrib/losses/python/losses/loss_ops.py
@@ -21,7 +21,6 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
-from tensorflow.contrib.framework import deprecated_args
from tensorflow.contrib.framework.python.ops import add_arg_scope
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
@@ -46,35 +45,6 @@ __all__ = ["absolute_difference",
"sparse_softmax_cross_entropy"]
-# TODO(b/32171727): Remove when deprecated `targets` is removed.
-def _labels(labels, targets):
- if labels is None:
- labels = targets
- elif targets is not None:
- raise ValueError("Can not specify both `labels` and `targets`.")
- if labels is None:
- raise ValueError("Must provide 1 of `labels` and `targets`.")
- return labels
-
-
-# TODO(b/32171727): Remove when deprecated `weight` is removed.
-_WEIGHT_SENTINEL = object()
-
-
-# TODO(b/32171727): Remove when deprecated `weight` is removed. Also, restore
-# weights=1.0 as default in all calling fns.
-def _weights(weights, weight):
- if weights is _WEIGHT_SENTINEL:
- weights = weight
- elif weight is not _WEIGHT_SENTINEL:
- raise ValueError("Can not specify both `weights` and `weight`.")
- if weights is None:
- raise ValueError("`weights` cannot be None.")
- if weights is _WEIGHT_SENTINEL:
- weights = 1.0
- return weights
-
-
def _scale_losses(losses, weights):
"""Computes the scaled loss.
@@ -142,17 +112,13 @@ def _safe_mean(losses, num_present):
@deprecated("2016-12-30", "Use tf.losses.compute_weighted_loss instead.")
-@deprecated_args(
- "2016-11-25", "`weight` is being deprecated, use `weights`.", "weight")
-def compute_weighted_loss(
- losses, weights=_WEIGHT_SENTINEL, scope=None, weight=_WEIGHT_SENTINEL):
+def compute_weighted_loss(losses, weights=1.0, scope=None):
"""Computes the weighted loss.
Args:
losses: A tensor of size [batch_size, d1, ... dN].
weights: A tensor of size [1] or [batch_size, d1, ... dK] where K < N.
scope: the scope for the operations performed in computing the loss.
- weight: Deprecated alias for `weights`.
Returns:
A scalar `Tensor` that returns the weighted loss.
@@ -162,7 +128,6 @@ def compute_weighted_loss(
`losses`, or if the number of dimensions (rank) of either `losses` or
`weights` is missing.
"""
- weights = _weights(weights, weight)
with ops.name_scope(scope, "weighted_loss", [losses, weights]):
losses = ops.convert_to_tensor(losses)
input_dtype = losses.dtype
@@ -173,7 +138,7 @@ def compute_weighted_loss(
raise ValueError("losses.get_shape().ndims cannot be None")
weights_shape = weights.get_shape()
if weights_shape.ndims is None:
- raise ValueError("weight.get_shape().ndims cannot be None")
+ raise ValueError("weights.get_shape().ndims cannot be None")
if weights_shape.ndims > 1 and weights_shape.dims[-1].is_compatible_with(1):
weights = array_ops.squeeze(weights, [-1])
@@ -227,7 +192,7 @@ def _num_present(losses, weights, per_batch=False):
math_ops.to_float(math_ops.not_equal(weights, 0)),
reduction_indices=reduction_indices)
- # Next, determine the number of elements that weight would broadcast to:
+ # Next, determine the number of elements that weights would broadcast to:
broadcast_dims = array_ops.slice(array_ops.shape(losses),
[weights.get_shape().ndims], [-1])
num_to_broadcast = math_ops.to_float(math_ops.reduce_prod(broadcast_dims))
@@ -300,23 +265,16 @@ def get_total_loss(add_regularization_losses=True, name="total_loss"):
@deprecated("2016-12-30", "Use tf.losses.absolute_difference instead.")
-@deprecated_args(
- "2016-11-25",
- "`targets` is being deprecated, use `labels`."
- " `weight` is being deprecated, use `weights`.",
- "targets", "weight")
-def absolute_difference(
- predictions, labels=None, weights=_WEIGHT_SENTINEL, scope=None,
- targets=None, weight=_WEIGHT_SENTINEL):
+def absolute_difference(predictions, labels=None, weights=1.0, scope=None):
"""Adds an Absolute Difference loss to the training procedure.
- `weight` acts as a coefficient for the loss. If a scalar is provided, then the
- loss is simply scaled by the given value. If `weight` is a tensor of size
+ `weights` acts as a coefficient for the loss. If a scalar is provided, then
+ the loss is simply scaled by the given value. If `weights` is a tensor of size
[batch_size], then the total loss for each sample of the batch is rescaled
- by the corresponding element in the `weight` vector. If the shape of
- `weight` matches the shape of `predictions`, then the loss of each
+ by the corresponding element in the `weights` vector. If the shape of
+ `weights` matches the shape of `predictions`, then the loss of each
measurable element of `predictions` is scaled by the corresponding value of
- `weight`.
+ `weights`.
Args:
predictions: The predicted outputs.
@@ -324,18 +282,14 @@ def absolute_difference(
weights: Coefficients for the loss a scalar, a tensor of shape
[batch_size] or a tensor whose shape matches `predictions`.
scope: The scope for the operations performed in computing the loss.
- targets: Deprecated alias for `labels`.
- weight: Deprecated alias for `weights`.
Returns:
A scalar `Tensor` representing the loss value.
Raises:
ValueError: If the shape of `predictions` doesn't match that of `labels` or
- if the shape of `weight` is invalid.
+ if the shape of `weights` is invalid.
"""
- labels = _labels(labels, targets)
- weights = _weights(weights, weight)
with ops.name_scope(scope, "absolute_difference",
[predictions, labels, weights]) as scope:
predictions.get_shape().assert_is_compatible_with(labels.get_shape())
@@ -346,15 +300,12 @@ def absolute_difference(
@deprecated("2016-12-30", "Use tf.losses.sigmoid_cross_entropy instead.")
-@deprecated_args(
- "2016-11-25", "`weight` is being deprecated, use `weights`", "weight")
def sigmoid_cross_entropy(
- logits, multi_class_labels, weights=_WEIGHT_SENTINEL, label_smoothing=0,
- scope=None, weight=_WEIGHT_SENTINEL):
+ logits, multi_class_labels, weights=1.0, label_smoothing=0, scope=None):
"""Creates a cross-entropy loss using tf.nn.sigmoid_cross_entropy_with_logits.
- `weight` acts as a coefficient for the loss. If a scalar is provided,
- then the loss is simply scaled by the given value. If `weight` is a
+ `weights` acts as a coefficient for the loss. If a scalar is provided,
+ then the loss is simply scaled by the given value. If `weights` is a
tensor of size [`batch_size`], then the loss weights apply to each
corresponding sample.
@@ -365,22 +316,20 @@ def sigmoid_cross_entropy(
Args:
logits: [batch_size, num_classes] logits outputs of the network .
- multi_class_labels: [batch_size, num_classes] target labels in (0, 1).
+ multi_class_labels: [batch_size, num_classes] labels in (0, 1).
weights: Coefficients for the loss. The tensor must be a scalar, a tensor of
shape [batch_size] or shape [batch_size, num_classes].
label_smoothing: If greater than 0 then smooth the labels.
scope: The scope for the operations performed in computing the loss.
- weight: Deprecated alias for `weights`.
Returns:
A scalar `Tensor` representing the loss value.
Raises:
ValueError: If the shape of `logits` doesn't match that of
- `multi_class_labels` or if the shape of `weight` is invalid, or if
- `weight` is None.
+ `multi_class_labels` or if the shape of `weights` is invalid, or if
+ `weights` is None.
"""
- weights = _weights(weights, weight)
with ops.name_scope(scope, "sigmoid_cross_entropy_loss",
[logits, multi_class_labels, weights]) as scope:
logits.get_shape().assert_is_compatible_with(multi_class_labels.get_shape())
@@ -397,15 +346,12 @@ def sigmoid_cross_entropy(
@deprecated("2016-12-30", "Use tf.losses.softmax_cross_entropy instead.")
-@deprecated_args(
- "2016-11-25", "`weight` is being deprecated, use `weights`", "weight")
def softmax_cross_entropy(
- logits, onehot_labels, weights=_WEIGHT_SENTINEL, label_smoothing=0,
- scope=None, weight=_WEIGHT_SENTINEL):
+ logits, onehot_labels, weights=1.0, label_smoothing=0, scope=None):
"""Creates a cross-entropy loss using tf.nn.softmax_cross_entropy_with_logits.
- `weight` acts as a coefficient for the loss. If a scalar is provided,
- then the loss is simply scaled by the given value. If `weight` is a
+ `weights` acts as a coefficient for the loss. If a scalar is provided,
+ then the loss is simply scaled by the given value. If `weights` is a
tensor of size [`batch_size`], then the loss weights apply to each
corresponding sample.
@@ -415,21 +361,19 @@ def softmax_cross_entropy(
Args:
logits: [batch_size, num_classes] logits outputs of the network .
- onehot_labels: [batch_size, num_classes] target one_hot_encoded labels.
+ onehot_labels: [batch_size, num_classes] one-hot-encoded labels.
weights: Coefficients for the loss. The tensor must be a scalar or a tensor
of shape [batch_size].
label_smoothing: If greater than 0 then smooth the labels.
scope: the scope for the operations performed in computing the loss.
- weight: Deprecated alias for `weights`.
Returns:
A scalar `Tensor` representing the loss value.
Raises:
ValueError: If the shape of `logits` doesn't match that of `onehot_labels`
- or if the shape of `weight` is invalid or if `weight` is None.
+ or if the shape of `weights` is invalid or if `weights` is None.
"""
- weights = _weights(weights, weight)
with ops.name_scope(scope, "softmax_cross_entropy_loss",
[logits, onehot_labels, weights]) as scope:
logits.get_shape().assert_is_compatible_with(onehot_labels.get_shape())
@@ -449,35 +393,29 @@ def softmax_cross_entropy(
@deprecated("2016-12-30", "Use tf.losses.sparse_softmax_cross_entropy instead.")
-@deprecated_args(
- "2016-11-25", "`weight` is being deprecated, use `weights`", "weight")
-def sparse_softmax_cross_entropy(
- logits, labels, weights=_WEIGHT_SENTINEL, scope=None,
- weight=_WEIGHT_SENTINEL):
+def sparse_softmax_cross_entropy(logits, labels, weights=1.0, scope=None):
"""Cross-entropy loss using `tf.nn.sparse_softmax_cross_entropy_with_logits`.
- `weight` acts as a coefficient for the loss. If a scalar is provided,
- then the loss is simply scaled by the given value. If `weight` is a
+ `weights` acts as a coefficient for the loss. If a scalar is provided,
+ then the loss is simply scaled by the given value. If `weights` is a
tensor of size [`batch_size`], then the loss weights apply to each
corresponding sample.
Args:
logits: [batch_size, num_classes] logits outputs of the network .
- labels: [batch_size, 1] or [batch_size] target labels of dtype `int32` or
- `int64` in the range `[0, num_classes)`.
+ labels: [batch_size, 1] or [batch_size] labels of dtype `int32` or `int64`
+ in the range `[0, num_classes)`.
weights: Coefficients for the loss. The tensor must be a scalar or a tensor
of shape [batch_size] or [batch_size, 1].
scope: the scope for the operations performed in computing the loss.
- weight: Deprecated alias for `weights`.
Returns:
A scalar `Tensor` representing the loss value.
Raises:
- ValueError: If the shapes of logits, labels, and weight are incompatible, or
- if `weight` is None.
+ ValueError: If the shapes of `logits`, `labels`, and `weights` are
+ incompatible, or if `weights` is None.
"""
- weights = _weights(weights, weight)
with ops.name_scope(scope, "sparse_softmax_cross_entropy_loss",
[logits, labels, weights]) as scope:
labels = array_ops.reshape(labels, shape=[array_ops.shape(labels)[0]])
@@ -489,23 +427,16 @@ def sparse_softmax_cross_entropy(
@deprecated("2016-12-30", "Use tf.losses.log_loss instead.")
-@deprecated_args(
- "2016-11-25",
- "`targets` is being deprecated, use `labels`."
- " `weight` is being deprecated, use `weights`.",
- "targets", "weight")
-def log_loss(
- predictions, labels=None, weights=_WEIGHT_SENTINEL, epsilon=1e-7,
- scope=None, targets=None, weight=_WEIGHT_SENTINEL):
+def log_loss(predictions, labels=None, weights=1.0, epsilon=1e-7, scope=None):
"""Adds a Log Loss term to the training procedure.
- `weight` acts as a coefficient for the loss. If a scalar is provided, then the
- loss is simply scaled by the given value. If `weight` is a tensor of size
+ `weights` acts as a coefficient for the loss. If a scalar is provided, then
+ the loss is simply scaled by the given value. If `weights` is a tensor of size
[batch_size], then the total loss for each sample of the batch is rescaled
- by the corresponding element in the `weight` vector. If the shape of
- `weight` matches the shape of `predictions`, then the loss of each
+ by the corresponding element in the `weights` vector. If the shape of
+ `weights` matches the shape of `predictions`, then the loss of each
measurable element of `predictions` is scaled by the corresponding value of
- `weight`.
+ `weights`.
Args:
predictions: The predicted outputs.
@@ -514,18 +445,14 @@ def log_loss(
[batch_size] or a tensor whose shape matches `predictions`.
epsilon: A small increment to add to avoid taking a log of zero.
scope: The scope for the operations performed in computing the loss.
- targets: Deprecated alias for `labels`.
- weight: Deprecated alias for `weights`.
Returns:
A scalar `Tensor` representing the loss value.
Raises:
ValueError: If the shape of `predictions` doesn't match that of `labels` or
- if the shape of `weight` is invalid.
+ if the shape of `weights` is invalid.
"""
- labels = _labels(labels, targets)
- weights = _weights(weights, weight)
with ops.name_scope(scope, "log_loss",
[predictions, labels, weights]) as scope:
predictions.get_shape().assert_is_compatible_with(labels.get_shape())
@@ -539,9 +466,7 @@ def log_loss(
@deprecated("2016-12-30", "Use tf.losses.hinge_loss instead.")
-@deprecated_args(
- "2016-11-25", "`target` is being deprecated, use `labels`.", "target")
-def hinge_loss(logits, labels=None, scope=None, target=None):
+def hinge_loss(logits, labels=None, scope=None):
"""Method that returns the loss tensor for hinge loss.
Args:
@@ -549,16 +474,14 @@ def hinge_loss(logits, labels=None, scope=None, target=None):
labels: The ground truth output tensor. Its shape should match the shape of
logits. The values of the tensor are expected to be 0.0 or 1.0.
scope: The scope for the operations performed in computing the loss.
- target: Deprecated alias for `labels`.
Returns:
- A `Tensor` of same shape as logits and target representing the loss values
- across the batch.
+ A `Tensor` of same shape as `logits` and `labels` representing the loss
+ values across the batch.
Raises:
ValueError: If the shapes of `logits` and `labels` don't match.
"""
- labels = _labels(labels, target)
with ops.name_scope(scope, "hinge_loss", [logits, labels]) as scope:
logits.get_shape().assert_is_compatible_with(labels.get_shape())
# We first need to convert binary labels to -1/1 labels (as floats).
@@ -569,23 +492,16 @@ def hinge_loss(logits, labels=None, scope=None, target=None):
@deprecated("2016-12-30", "Use tf.losses.mean_squared_error instead.")
-@deprecated_args(
- "2016-11-25",
- "`targets` is being deprecated, use `labels`."
- " `weight` is being deprecated, use `weights`.",
- "targets", "weight")
-def mean_squared_error(
- predictions, labels=None, weights=_WEIGHT_SENTINEL, scope=None,
- targets=None, weight=_WEIGHT_SENTINEL):
+def mean_squared_error(predictions, labels=None, weights=1.0, scope=None):
"""Adds a Sum-of-Squares loss to the training procedure.
- `weight` acts as a coefficient for the loss. If a scalar is provided, then the
- loss is simply scaled by the given value. If `weight` is a tensor of size
+ `weights` acts as a coefficient for the loss. If a scalar is provided, then
+ the loss is simply scaled by the given value. If `weights` is a tensor of size
[batch_size], then the total loss for each sample of the batch is rescaled
- by the corresponding element in the `weight` vector. If the shape of
- `weight` matches the shape of `predictions`, then the loss of each
+ by the corresponding element in the `weights` vector. If the shape of
+ `weights` matches the shape of `predictions`, then the loss of each
measurable element of `predictions` is scaled by the corresponding value of
- `weight`.
+ `weights`.
Args:
predictions: The predicted outputs.
@@ -593,18 +509,14 @@ def mean_squared_error(
weights: Coefficients for the loss a scalar, a tensor of shape
[batch_size] or a tensor whose shape matches `predictions`.
scope: The scope for the operations performed in computing the loss.
- targets: Deprecated alias for `labels`.
- weight: Deprecated alias for `weights`.
Returns:
A scalar `Tensor` representing the loss value.
Raises:
ValueError: If the shape of `predictions` doesn't match that of `labels` or
- if the shape of `weight` is invalid.
+ if the shape of `weights` is invalid.
"""
- labels = _labels(labels, targets)
- weights = _weights(weights, weight)
with ops.name_scope(scope, "mean_squared_error",
[predictions, labels, weights]) as scope:
predictions.get_shape().assert_is_compatible_with(labels.get_shape())
@@ -615,14 +527,8 @@ def mean_squared_error(
@deprecated("2016-12-30", "Use tf.losses.mean_pairwise_squared_error instead.")
-@deprecated_args(
- "2016-11-25",
- "`targets` is being deprecated, use `labels`."
- " `weight` is being deprecated, use `weights`.",
- "targets", "weight")
def mean_pairwise_squared_error(
- predictions, labels=None, weights=_WEIGHT_SENTINEL, scope=None,
- targets=None, weight=_WEIGHT_SENTINEL):
+ predictions, labels=None, weights=1.0, scope=None):
"""Adds a pairwise-errors-squared loss to the training procedure.
Unlike `mean_squared_error`, which is a measure of the differences between
@@ -640,10 +546,10 @@ def mean_pairwise_squared_error(
16 grayscale images of dimension [batch_size, 100, 200], then the set of pairs
is drawn from each image, but not across images.
- `weight` acts as a coefficient for the loss. If a scalar is provided, then the
- loss is simply scaled by the given value. If `weight` is a tensor of size
+ `weights` acts as a coefficient for the loss. If a scalar is provided, then
+ the loss is simply scaled by the given value. If `weights` is a tensor of size
[batch_size], then the total loss for each sample of the batch is rescaled
- by the corresponding element in the `weight` vector.
+ by the corresponding element in the `weights` vector.
Args:
predictions: The predicted outputs, a tensor of size [batch_size, d0, .. dN]
@@ -653,18 +559,14 @@ def mean_pairwise_squared_error(
weights: Coefficients for the loss a scalar, a tensor of shape [batch_size]
or a tensor whose shape matches `predictions`.
scope: The scope for the operations performed in computing the loss.
- targets: Deprecated alias for `labels`.
- weight: Deprecated alias for `weights`.
Returns:
A scalar `Tensor` representing the loss value.
Raises:
ValueError: If the shape of `predictions` doesn't match that of `labels` or
- if the shape of `weight` is invalid.
+ if the shape of `weights` is invalid.
"""
- labels = _labels(labels, targets)
- weights = _weights(weights, weight)
with ops.name_scope(scope, "mean_pairwise_squared_error",
[predictions, labels, weights]) as scope:
predictions.get_shape().assert_is_compatible_with(labels.get_shape())
@@ -705,14 +607,8 @@ def mean_pairwise_squared_error(
@deprecated("2016-12-30", "Use tf.losses.cosine_distance instead.")
-@deprecated_args(
- "2016-11-25",
- "`targets` is being deprecated, use `labels`."
- " `weight` is being deprecated, use `weights`.",
- "targets", "weight")
def cosine_distance(
- predictions, labels=None, dim=None, weights=_WEIGHT_SENTINEL, scope=None,
- targets=None, weight=_WEIGHT_SENTINEL):
+ predictions, labels=None, dim=None, weights=1.0, scope=None):
"""Adds a cosine-distance loss to the training procedure.
Note that the function assumes that `predictions` and `labels` are already
@@ -725,8 +621,6 @@ def cosine_distance(
weights: Coefficients for the loss a scalar, a tensor of shape
[batch_size] or a tensor whose shape matches `predictions`.
scope: The scope for the operations performed in computing the loss.
- targets: Deprecated alias for `labels`.
- weight: Deprecated alias for `weights`.
Returns:
A scalar `Tensor` representing the loss value.
@@ -735,8 +629,6 @@ def cosine_distance(
ValueError: If `predictions` shape doesn't match `labels` shape, or
`weights` is `None`.
"""
- labels = _labels(labels, targets)
- weights = _weights(weights, weight)
if dim is None:
raise ValueError("`dim` cannot be None.")
with ops.name_scope(scope, "cosine_distance_loss",
diff --git a/tensorflow/contrib/losses/python/losses/loss_ops_test.py b/tensorflow/contrib/losses/python/losses/loss_ops_test.py
index 75785f00a1..6cfa521294 100644
--- a/tensorflow/contrib/losses/python/losses/loss_ops_test.py
+++ b/tensorflow/contrib/losses/python/losses/loss_ops_test.py
@@ -35,10 +35,6 @@ class AbsoluteDifferenceLossTest(tf.test.TestCase):
with self.assertRaises(ValueError):
tf.contrib.losses.absolute_difference(
self._predictions, self._predictions, weights=None)
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.absolute_difference(
- self._predictions, self._predictions, weight=None)
def testAllCorrectNoLossWeight(self):
loss = tf.contrib.losses.absolute_difference(
@@ -59,29 +55,6 @@ class AbsoluteDifferenceLossTest(tf.test.TestCase):
with self.test_session():
self.assertAlmostEqual(5.5 * weights, loss.eval(), 3)
- # TODO(b/32171727): Remove when deprecated args are removed.
- def testDeprecatedArgs(self):
- w = 2.3
- weighted_loss_tensors = (
- tf.contrib.losses.absolute_difference(
- self._predictions, self._labels, w),
- tf.contrib.losses.absolute_difference(
- self._predictions, self._labels, weights=w),
- tf.contrib.losses.absolute_difference(
- self._predictions, self._labels, weight=w),
- tf.contrib.losses.absolute_difference(
- self._predictions, labels=self._labels, weights=w),
- tf.contrib.losses.absolute_difference(
- self._predictions, labels=self._labels, weight=w),
- tf.contrib.losses.absolute_difference(
- self._predictions, targets=self._labels, weights=w),
- tf.contrib.losses.absolute_difference(
- self._predictions, targets=self._labels, weight=w),
- )
- with self.test_session():
- for weighted_loss_tensor in weighted_loss_tensors:
- self.assertAlmostEqual(5.5 * w, weighted_loss_tensor.eval(), 3)
-
def testNonZeroLossWithScalarTensorWeight(self):
weights = 2.3
loss = tf.contrib.losses.absolute_difference(
@@ -137,9 +110,6 @@ class SoftmaxCrossEntropyLossTest(tf.test.TestCase):
with self.test_session():
with self.assertRaises(ValueError):
tf.contrib.losses.softmax_cross_entropy(logits, labels, weights=None)
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.softmax_cross_entropy(logits, labels, weight=None)
def testAllCorrect(self):
with self.test_session():
@@ -178,27 +148,6 @@ class SoftmaxCrossEntropyLossTest(tf.test.TestCase):
loss = tf.contrib.losses.softmax_cross_entropy(logits, labels, weights)
self.assertAlmostEqual(weights * 10.0, loss.eval(), 3)
- # TODO(b/32171727): Remove when deprecated args are removed.
- def testDeprecatedArgs(self):
- logits = tf.constant([[10.0, 0.0, 0.0],
- [0.0, 10.0, 0.0],
- [0.0, 0.0, 10.0]])
- labels = tf.constant([[0, 0, 1],
- [1, 0, 0],
- [0, 1, 0]])
- w = 2.3
- with self.test_session():
- loss_tensors = (
- tf.contrib.losses.softmax_cross_entropy(logits, labels, w),
- tf.contrib.losses.softmax_cross_entropy(logits, labels, weights=w),
- tf.contrib.losses.softmax_cross_entropy(logits, labels, weight=w),
- tf.contrib.losses.softmax_cross_entropy(
- logits, onehot_labels=labels, weights=w),
- tf.contrib.losses.softmax_cross_entropy(
- logits, onehot_labels=labels, weight=w))
- for loss_tensor in loss_tensors:
- self.assertAlmostEqual(w * 10.0, loss_tensor.eval(), 3)
-
def testNonZeroLossWithScalarTensorWeight(self):
logits = tf.constant([[10.0, 0.0, 0.0],
[0.0, 10.0, 0.0],
@@ -263,10 +212,6 @@ class SoftmaxCrossEntropyLossTest(tf.test.TestCase):
with self.assertRaises(ValueError):
tf.contrib.losses.softmax_cross_entropy(
logits, labels, weights=weights).eval()
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.softmax_cross_entropy(
- logits, labels, weight=weights).eval()
def testSoftmaxLabelSmoothing(self):
with self.test_session():
@@ -301,10 +246,6 @@ class SparseSoftmaxCrossEntropyLossTest(tf.test.TestCase):
with self.assertRaises(ValueError):
tf.contrib.losses.sparse_softmax_cross_entropy(
logits, labels, weights=None)
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels, weight=None)
def testAllCorrectInt32Labels(self):
with self.test_session():
@@ -380,28 +321,6 @@ class SparseSoftmaxCrossEntropyLossTest(tf.test.TestCase):
logits, labels, weights)
self.assertAlmostEqual(weights * 10.0, loss.eval(), 3)
- # TODO(b/32171727): Remove when deprecated args are removed.
- def testDeprecatedArgs(self):
- logits = tf.constant([[10.0, 0.0, 0.0],
- [0.0, 10.0, 0.0],
- [0.0, 0.0, 10.0]])
- labels = tf.constant([[2], [0], [1]])
- w = 2.3
- with self.test_session():
- loss_tensors = (
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels, w),
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels, weights=w),
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels, weight=w),
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels=labels, weights=w),
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels=labels, weight=w))
- for loss_tensor in loss_tensors:
- self.assertAlmostEqual(w * 10.0, loss_tensor.eval(), 3)
-
def testNonZeroLossWithScalarTensorWeight(self):
logits = tf.constant([[10.0, 0.0, 0.0],
[0.0, 10.0, 0.0],
@@ -470,10 +389,6 @@ class SparseSoftmaxCrossEntropyLossTest(tf.test.TestCase):
with self.assertRaises(ValueError):
tf.contrib.losses.sparse_softmax_cross_entropy(
logits, labels, weights=weights).eval()
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels, weight=weights).eval()
def testInconsistentWeightSizeRaisesException(self):
"""The weight tensor has incorrect number of elements."""
@@ -487,10 +402,6 @@ class SparseSoftmaxCrossEntropyLossTest(tf.test.TestCase):
with self.assertRaises(ValueError):
tf.contrib.losses.sparse_softmax_cross_entropy(
logits, labels, weights=weights).eval()
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels, weight=weights).eval()
def testInconsistentLabelSizeRaisesException(self):
"""The label tensor has incorrect number of elements."""
@@ -504,10 +415,6 @@ class SparseSoftmaxCrossEntropyLossTest(tf.test.TestCase):
with self.assertRaises(ValueError):
tf.contrib.losses.sparse_softmax_cross_entropy(
logits, labels, weights=weights).eval()
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels, weight=weights).eval()
def testInconsistentWeightShapeRaisesException(self):
"""The weight tensor has incorrect shape."""
@@ -522,10 +429,6 @@ class SparseSoftmaxCrossEntropyLossTest(tf.test.TestCase):
with self.assertRaises(ValueError):
tf.contrib.losses.sparse_softmax_cross_entropy(
logits, labels, weights=weights).eval()
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels, weight=weights).eval()
def testInconsistentLabelShapeRaisesException(self):
"""The label tensor has incorrect shape."""
@@ -540,10 +443,6 @@ class SparseSoftmaxCrossEntropyLossTest(tf.test.TestCase):
with self.assertRaises(tf.errors.InvalidArgumentError):
tf.contrib.losses.sparse_softmax_cross_entropy(
logits, labels, weights=weights).eval()
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(tf.errors.InvalidArgumentError):
- tf.contrib.losses.sparse_softmax_cross_entropy(
- logits, labels, weight=weights).eval()
class SigmoidCrossEntropyLossTest(tf.test.TestCase):
@@ -687,9 +586,6 @@ class LogLossTest(tf.test.TestCase):
with self.test_session():
with self.assertRaises(ValueError):
tf.contrib.losses.log_loss(self._labels, self._labels, weights=None)
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.log_loss(self._labels, self._labels, weight=None)
def testAllCorrectNoLossWeight(self):
loss = tf.contrib.losses.log_loss(self._labels, self._labels)
@@ -717,26 +613,6 @@ class LogLossTest(tf.test.TestCase):
self.assertAlmostEqual(weights * -np.sum(self._expected_losses) / 6.0,
loss.eval(), 3)
- # TODO(b/32171727): Remove when deprecated args are removed.
- def testDeprecatedArgs(self):
- w = 2.3
- loss_tensors = (
- tf.contrib.losses.log_loss(self._predictions, self._labels, w),
- tf.contrib.losses.log_loss(self._predictions, self._labels, weights=w),
- tf.contrib.losses.log_loss(self._predictions, self._labels, weight=w),
- tf.contrib.losses.log_loss(
- self._predictions, labels=self._labels, weights=w),
- tf.contrib.losses.log_loss(
- self._predictions, labels=self._labels, weight=w),
- tf.contrib.losses.log_loss(
- self._predictions, targets=self._labels, weights=w),
- tf.contrib.losses.log_loss(
- self._predictions, targets=self._labels, weight=w))
- with self.test_session():
- for loss_tensor in loss_tensors:
- self.assertAlmostEqual(
- w * -np.sum(self._expected_losses) / 6.0, loss_tensor.eval(), 3)
-
def testNonZeroLossWithScalarTensorWeight(self):
weights = 2.3
loss = tf.contrib.losses.log_loss(
@@ -908,9 +784,6 @@ class MeanSquaredErrorTest(tf.test.TestCase):
with self.assertRaises(ValueError):
tf.contrib.losses.mean_squared_error(
self._predictions, self._predictions, weights=None)
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- tf.contrib.losses.mean_squared_error(
- self._predictions, self._predictions, weight=None)
def testAllCorrectNoLossWeight(self):
loss = tf.contrib.losses.mean_squared_error(
@@ -938,28 +811,6 @@ class MeanSquaredErrorTest(tf.test.TestCase):
with self.test_session():
self.assertAlmostEqual(49.5 * weights, loss.eval(), 3)
- # TODO(b/32171727): Remove when deprecated args are removed.
- def testDeprecatedArgs(self):
- w = 2.3
- loss_tensors = (
- tf.contrib.losses.mean_squared_error(
- self._predictions, self._labels, tf.constant(w)),
- tf.contrib.losses.mean_squared_error(
- self._predictions, self._labels, weights=tf.constant(w)),
- tf.contrib.losses.mean_squared_error(
- self._predictions, self._labels, weight=tf.constant(w)),
- tf.contrib.losses.mean_squared_error(
- self._predictions, labels=self._labels, weights=tf.constant(w)),
- tf.contrib.losses.mean_squared_error(
- self._predictions, labels=self._labels, weight=tf.constant(w)),
- tf.contrib.losses.mean_squared_error(
- self._predictions, targets=self._labels, weights=tf.constant(w)),
- tf.contrib.losses.mean_squared_error(
- self._predictions, targets=self._labels, weight=tf.constant(w)))
- with self.test_session():
- for loss_tensor in loss_tensors:
- self.assertAlmostEqual(49.5 * w, loss_tensor.eval(), 3)
-
def testNonZeroLossWithOneDimBatchSpecificWeights(self):
weights = tf.constant([1.2, 3.4], shape=[2,])
loss = tf.contrib.losses.mean_squared_error(
@@ -1025,12 +876,6 @@ class MeanPairwiseSquaresErrorTest(tf.test.TestCase):
predictions=tf.constant(self._labels),
labels=tf.constant(self._labels),
weights=None)
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.mean_pairwise_squared_error(
- predictions=tf.constant(self._labels),
- labels=tf.constant(self._labels),
- weight=None)
def testAllCorrectNoLossWeight(self):
loss = tf.contrib.losses.mean_pairwise_squared_error(
@@ -1116,34 +961,6 @@ class MeanPairwiseSquaresErrorTest(tf.test.TestCase):
})
self.assertAlmostEqual(weights * np.sum(self._expected_losses), loss, 3)
- # TODO(b/32171727): Remove when deprecated args are removed.
- def testDeprecatedArgs(self):
- w = 2.3
- tf_predictions = tf.placeholder(tf.float32, shape=self._predictions.shape)
- tf_labels = tf.placeholder(tf.float32, shape=self._labels.shape)
- loss_tensors = (
- tf.contrib.losses.mean_pairwise_squared_error(
- tf_predictions, tf_labels, tf.constant(w)),
- tf.contrib.losses.mean_pairwise_squared_error(
- tf_predictions, tf_labels, weights=tf.constant(w)),
- tf.contrib.losses.mean_pairwise_squared_error(
- tf_predictions, tf_labels, weight=tf.constant(w)),
- tf.contrib.losses.mean_pairwise_squared_error(
- tf_predictions, labels=tf_labels, weights=tf.constant(w)),
- tf.contrib.losses.mean_pairwise_squared_error(
- tf_predictions, labels=tf_labels, weight=tf.constant(w)),
- tf.contrib.losses.mean_pairwise_squared_error(
- tf_predictions, targets=tf_labels, weights=tf.constant(w)),
- tf.contrib.losses.mean_pairwise_squared_error(
- tf_predictions, targets=tf_labels, weight=tf.constant(w)))
- with self.test_session() as sess:
- for loss_tensor in loss_tensors:
- loss = sess.run(loss_tensor, feed_dict={
- tf_predictions: self._predictions,
- tf_labels: self._labels,
- })
- self.assertAlmostEqual(w * np.sum(self._expected_losses), loss, 3)
-
def testNonZeroLossWithOneDimBatchSpecificWeights(self):
weights = np.asarray([2.0, 1.0]).reshape((2, 1))
expected_losses = np.multiply(weights, self._expected_losses)
@@ -1217,13 +1034,6 @@ class CosineDistanceLossTest(tf.test.TestCase):
labels=tf.constant(self._labels),
dim=2,
weights=None)
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- with self.assertRaises(ValueError):
- tf.contrib.losses.cosine_distance(
- predictions=tf.constant(self._labels),
- labels=tf.constant(self._labels),
- dim=2,
- weight=None)
def testAllCorrectNoWeights(self):
loss = tf.contrib.losses.cosine_distance(
@@ -1318,23 +1128,6 @@ class CosineDistanceLossTest(tf.test.TestCase):
class ComputeWeightedLossTest(tf.test.TestCase):
- # TODO(b/32171727): Remove when deprecated `weight` is removed.
- def testDeprecatedArgs(self):
- losses = (1.2, 0.4, -1.0, -1.1)
- expected_loss = -0.5 / 4
- weights = 2.0
- expected_weighted_loss = weights * expected_loss
- with self.test_session():
- loss_tensor = tf.contrib.losses.compute_weighted_loss(losses)
- self.assertAllClose(expected_loss, loss_tensor.eval(), atol=1e-3)
- weighted_loss_tensors = (
- tf.contrib.losses.compute_weighted_loss(losses, weights),
- tf.contrib.losses.compute_weighted_loss(losses, weights=weights),
- tf.contrib.losses.compute_weighted_loss(losses, weight=weights))
- for weighted_loss_tensor in weighted_loss_tensors:
- self.assertAllClose(
- expected_weighted_loss, weighted_loss_tensor.eval(), atol=1e-3)
-
def testHingeLoss(self):
logits = tf.constant([1.2, 0.4, -1.0, -1.1])
labels = tf.constant([1.0, 0.0, 0.0, 1.0])