MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / cosine_distance

Function cosine_distance

tensorflow/python/ops/losses/losses_impl.py:261–312  ·  view source on GitHub ↗

Adds a cosine-distance loss to the training procedure. Note that the function assumes that `predictions` and `labels` are already unit-normalized. Args: labels: `Tensor` whose shape matches 'predictions' predictions: An arbitrary matrix. axis: The dimension along which the cosine

(
    labels, predictions, axis=None, weights=1.0, scope=None,
    loss_collection=ops.GraphKeys.LOSSES,
    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS,
    dim=None)

Source from the content-addressed store, hash-verified

259@tf_export(v1=["losses.cosine_distance"])
260@deprecated_args(None, "dim is deprecated, use axis instead", "dim")
261def cosine_distance(
262 labels, predictions, axis=None, weights=1.0, scope=None,
263 loss_collection=ops.GraphKeys.LOSSES,
264 reduction=Reduction.SUM_BY_NONZERO_WEIGHTS,
265 dim=None):
266 """Adds a cosine-distance loss to the training procedure.
267
268 Note that the function assumes that `predictions` and `labels` are already
269 unit-normalized.
270
271 Args:
272 labels: `Tensor` whose shape matches 'predictions'
273 predictions: An arbitrary matrix.
274 axis: The dimension along which the cosine distance is computed.
275 weights: Optional `Tensor` whose rank is either 0, or the same rank as
276 `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
277 be either `1`, or the same as the corresponding `losses` dimension).
278 scope: The scope for the operations performed in computing the loss.
279 loss_collection: collection to which this loss will be added.
280 reduction: Type of reduction to apply to loss.
281 dim: The old (deprecated) name for `axis`.
282
283 Returns:
284 Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
285 shape as `labels`; otherwise, it is scalar.
286
287 Raises:
288 ValueError: If `predictions` shape doesn't match `labels` shape, or
289 `axis`, `labels`, `predictions` or `weights` is `None`.
290
291 @compatibility(eager)
292 The `loss_collection` argument is ignored when executing eagerly. Consider
293 holding on to the return value or collecting losses via a `tf.keras.Model`.
294 @end_compatibility
295 """
296 axis = deprecated_argument_lookup("axis", axis, "dim", dim)
297 if axis is None:
298 raise ValueError("You must specify 'axis'.")
299 if labels is None:
300 raise ValueError("labels must not be None.")
301 if predictions is None:
302 raise ValueError("predictions must not be None.")
303 with ops.name_scope(scope, "cosine_distance_loss",
304 (predictions, labels, weights)) as scope:
305 predictions = math_ops.cast(predictions, dtype=dtypes.float32)
306 labels = math_ops.cast(labels, dtype=dtypes.float32)
307 predictions.get_shape().assert_is_compatible_with(labels.get_shape())
308
309 radial_diffs = math_ops.multiply(predictions, labels)
310 losses = 1 - math_ops.reduce_sum(radial_diffs, axis=(axis,), keepdims=True)
311 return compute_weighted_loss(
312 losses, weights, scope, loss_collection, reduction=reduction)
313
314
315@tf_export(v1=["losses.hinge_loss"])

Callers

nothing calls this directly

Calls 8

multiplyMethod · 0.80
reduce_sumMethod · 0.80
compute_weighted_lossFunction · 0.70
name_scopeMethod · 0.45
castMethod · 0.45
get_shapeMethod · 0.45

Tested by

no test coverage detected