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

Function hinge_loss

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

Adds a hinge loss to the training procedure. Args: 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. Internally the {0,1} labels are converted to {-1,1} when calculating the hinge loss. l

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

Source from the content-addressed store, hash-verified

314
315@tf_export(v1=["losses.hinge_loss"])
316def hinge_loss(labels, logits, weights=1.0, scope=None,
317 loss_collection=ops.GraphKeys.LOSSES,
318 reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
319 """Adds a hinge loss to the training procedure.
320
321 Args:
322 labels: The ground truth output tensor. Its shape should match the shape of
323 logits. The values of the tensor are expected to be 0.0 or 1.0. Internally
324 the {0,1} labels are converted to {-1,1} when calculating the hinge loss.
325 logits: The logits, a float tensor. Note that logits are assumed to be
326 unbounded and 0-centered. A value > 0 (resp. < 0) is considered a positive
327 (resp. negative) binary prediction.
328 weights: Optional `Tensor` whose rank is either 0, or the same rank as
329 `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
330 be either `1`, or the same as the corresponding `losses` dimension).
331 scope: The scope for the operations performed in computing the loss.
332 loss_collection: collection to which the loss will be added.
333 reduction: Type of reduction to apply to loss.
334
335 Returns:
336 Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
337 shape as `labels`; otherwise, it is scalar.
338
339 Raises:
340 ValueError: If the shapes of `logits` and `labels` don&#x27;t match or
341 if `labels` or `logits` is None.
342
343 @compatibility(eager)
344 The `loss_collection` argument is ignored when executing eagerly. Consider
345 holding on to the return value or collecting losses via a `tf.keras.Model`.
346 @end_compatibility
347 """
348 if labels is None:
349 raise ValueError("labels must not be None.")
350 if logits is None:
351 raise ValueError("logits must not be None.")
352 with ops.name_scope(scope, "hinge_loss", (logits, labels, weights)) as scope:
353 logits = math_ops.cast(logits, dtype=dtypes.float32)
354 labels = math_ops.cast(labels, dtype=dtypes.float32)
355 logits.get_shape().assert_is_compatible_with(labels.get_shape())
356 # We first need to convert binary labels to -1/1 labels (as floats).
357 all_ones = array_ops.ones_like(labels)
358 labels = math_ops.subtract(2 * labels, all_ones)
359 losses = nn_ops.relu(
360 math_ops.subtract(all_ones, math_ops.multiply(labels, logits)))
361 return compute_weighted_loss(
362 losses, weights, scope, loss_collection, reduction=reduction)
363
364
365@tf_export(v1=["losses.huber_loss"])

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected