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

Method _prepare_total_loss

tensorflow/python/keras/engine/training.py:1671–1762  ·  view source on GitHub ↗

Computes total loss from loss functions. Arguments: masks: List of mask values corresponding to each model output. Returns: A list of loss weights of python floats. Raises: TypeError: If model run_eagerly is True.

(self, masks)

Source from the content-addressed store, hash-verified

1669 return [getattr(x, '_keras_mask', None) for x in self.outputs]
1670
1671 def _prepare_total_loss(self, masks):
1672 """Computes total loss from loss functions.
1673
1674 Arguments:
1675 masks: List of mask values corresponding to each model output.
1676
1677 Returns:
1678 A list of loss weights of python floats.
1679
1680 Raises:
1681 TypeError: If model run_eagerly is True.
1682 """
1683 if self.run_eagerly:
1684 raise TypeError('total loss can not be computed when compiled with '
1685 'run_eagerly = True.')
1686 total_loss = None
1687 with K.name_scope('loss'):
1688 for endpoint, mask in zip(self._training_endpoints, masks):
1689 if endpoint.should_skip_target():
1690 continue
1691 y_true = endpoint.training_target.target
1692 y_pred = endpoint.output
1693 loss_fn = endpoint.loss_fn
1694 loss_weight = endpoint.loss_weight
1695 loss_name = endpoint.loss_name()
1696 sample_weight = endpoint.sample_weight
1697
1698 with K.name_scope(loss_name):
1699 if mask is not None:
1700 mask = math_ops.cast(mask, y_pred.dtype)
1701 # Update weights with mask.
1702 if sample_weight is None:
1703 sample_weight = mask
1704 else:
1705 # Update dimensions of weights to match with mask if possible.
1706 mask, _, sample_weight = (
1707 tf_losses_utils.squeeze_or_expand_dimensions(
1708 mask, sample_weight=sample_weight))
1709 sample_weight *= mask
1710
1711 if hasattr(loss_fn, 'reduction'):
1712 per_sample_losses = loss_fn.call(y_true, y_pred)
1713 weighted_losses = losses_utils.compute_weighted_loss(
1714 per_sample_losses,
1715 sample_weight=sample_weight,
1716 reduction=losses_utils.ReductionV2.NONE)
1717 loss_reduction = loss_fn.reduction
1718
1719 # `AUTO` loss reduction defaults to `SUM_OVER_BATCH_SIZE` for all
1720 # compile use cases.
1721 if loss_reduction == losses_utils.ReductionV2.AUTO:
1722 loss_reduction = losses_utils.ReductionV2.SUM_OVER_BATCH_SIZE
1723
1724 # Compute the stateless loss value.
1725 output_loss = losses_utils.reduce_weighted_loss(
1726 weighted_losses, reduction=loss_reduction)
1727 else:
1728 # Compute the stateless loss value for a custom loss class.

Calls 8

should_skip_targetMethod · 0.80
loss_nameMethod · 0.80
output_loss_metricMethod · 0.80
loss_fnFunction · 0.50
name_scopeMethod · 0.45
castMethod · 0.45
callMethod · 0.45
get_losses_forMethod · 0.45

Tested by

no test coverage detected