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

Function absolute_difference

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

Adds an Absolute Difference loss to the training procedure. `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 shape `[batch_size]`, then the total loss for each sample of the batch is resca

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

Source from the content-addressed store, hash-verified

205
206@tf_export(v1=["losses.absolute_difference"])
207def absolute_difference(
208 labels, predictions, weights=1.0, scope=None,
209 loss_collection=ops.GraphKeys.LOSSES,
210 reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
211 """Adds an Absolute Difference loss to the training procedure.
212
213 `weights` acts as a coefficient for the loss. If a scalar is provided, then
214 the loss is simply scaled by the given value. If `weights` is a `Tensor` of
215 shape `[batch_size]`, then the total loss for each sample of the batch is
216 rescaled by the corresponding element in the `weights` vector. If the shape of
217 `weights` matches the shape of `predictions`, then the loss of each
218 measurable element of `predictions` is scaled by the corresponding value of
219 `weights`.
220
221 Args:
222 labels: The ground truth output tensor, same dimensions as 'predictions'.
223 predictions: The predicted outputs.
224 weights: Optional `Tensor` whose rank is either 0, or the same rank as
225 `labels`, and must be broadcastable to `labels` (i.e., all dimensions must
226 be either `1`, or the same as the corresponding `losses` dimension).
227 scope: The scope for the operations performed in computing the loss.
228 loss_collection: collection to which this loss will be added.
229 reduction: Type of reduction to apply to loss.
230
231 Returns:
232 Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
233 shape as `labels`; otherwise, it is scalar.
234
235 Raises:
236 ValueError: If the shape of `predictions` doesn't match that of
237 `labels` or if the shape of `weights` is invalid or if `labels`
238 or `predictions` is None.
239
240 @compatibility(eager)
241 The `loss_collection` argument is ignored when executing eagerly. Consider
242 holding on to the return value or collecting losses via a `tf.keras.Model`.
243 @end_compatibility
244 """
245 if labels is None:
246 raise ValueError("labels must not be None.")
247 if predictions is None:
248 raise ValueError("predictions must not be None.")
249 with ops.name_scope(scope, "absolute_difference",
250 (predictions, labels, weights)) as scope:
251 predictions = math_ops.cast(predictions, dtype=dtypes.float32)
252 labels = math_ops.cast(labels, dtype=dtypes.float32)
253 predictions.get_shape().assert_is_compatible_with(labels.get_shape())
254 losses = math_ops.abs(math_ops.subtract(predictions, labels))
255 return compute_weighted_loss(
256 losses, weights, scope, loss_collection, reduction=reduction)
257
258
259@tf_export(v1=["losses.cosine_distance"])

Callers

nothing calls this directly

Calls 5

compute_weighted_lossFunction · 0.70
name_scopeMethod · 0.45
castMethod · 0.45
get_shapeMethod · 0.45

Tested by

no test coverage detected