Creates a cross-entropy loss using tf.nn.softmax_cross_entropy_with_logits_v2. `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 loss weights apply to each corr
(
onehot_labels, logits, weights=1.0, label_smoothing=0, scope=None,
loss_collection=ops.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS)
| 712 | |
| 713 | @tf_export(v1=["losses.softmax_cross_entropy"]) |
| 714 | def softmax_cross_entropy( |
| 715 | onehot_labels, logits, weights=1.0, label_smoothing=0, scope=None, |
| 716 | loss_collection=ops.GraphKeys.LOSSES, |
| 717 | reduction=Reduction.SUM_BY_NONZERO_WEIGHTS): |
| 718 | """Creates a cross-entropy loss using tf.nn.softmax_cross_entropy_with_logits_v2. |
| 719 | |
| 720 | `weights` acts as a coefficient for the loss. If a scalar is provided, |
| 721 | then the loss is simply scaled by the given value. If `weights` is a |
| 722 | tensor of shape `[batch_size]`, then the loss weights apply to each |
| 723 | corresponding sample. |
| 724 | |
| 725 | If `label_smoothing` is nonzero, smooth the labels towards 1/num_classes: |
| 726 | new_onehot_labels = onehot_labels * (1 - label_smoothing) |
| 727 | + label_smoothing / num_classes |
| 728 | |
| 729 | Note that `onehot_labels` and `logits` must have the same shape, |
| 730 | e.g. `[batch_size, num_classes]`. The shape of `weights` must be |
| 731 | broadcastable to loss, whose shape is decided by the shape of `logits`. |
| 732 | In case the shape of `logits` is `[batch_size, num_classes]`, loss is |
| 733 | a `Tensor` of shape `[batch_size]`. |
| 734 | |
| 735 | Args: |
| 736 | onehot_labels: One-hot-encoded labels. |
| 737 | logits: Logits outputs of the network. |
| 738 | weights: Optional `Tensor` that is broadcastable to loss. |
| 739 | label_smoothing: If greater than 0 then smooth the labels. |
| 740 | scope: the scope for the operations performed in computing the loss. |
| 741 | loss_collection: collection to which the loss will be added. |
| 742 | reduction: Type of reduction to apply to loss. |
| 743 | |
| 744 | Returns: |
| 745 | Weighted loss `Tensor` of the same type as `logits`. If `reduction` is |
| 746 | `NONE`, this has shape `[batch_size]`; otherwise, it is scalar. |
| 747 | |
| 748 | Raises: |
| 749 | ValueError: If the shape of `logits` doesn't match that of `onehot_labels` |
| 750 | or if the shape of `weights` is invalid or if `weights` is None. Also if |
| 751 | `onehot_labels` or `logits` is None. |
| 752 | |
| 753 | @compatibility(eager) |
| 754 | The `loss_collection` argument is ignored when executing eagerly. Consider |
| 755 | holding on to the return value or collecting losses via a `tf.keras.Model`. |
| 756 | @end_compatibility |
| 757 | """ |
| 758 | if onehot_labels is None: |
| 759 | raise ValueError("onehot_labels must not be None.") |
| 760 | if logits is None: |
| 761 | raise ValueError("logits must not be None.") |
| 762 | with ops.name_scope(scope, "softmax_cross_entropy_loss", |
| 763 | (logits, onehot_labels, weights)) as scope: |
| 764 | logits = ops.convert_to_tensor(logits) |
| 765 | onehot_labels = math_ops.cast(onehot_labels, logits.dtype) |
| 766 | logits.get_shape().assert_is_compatible_with(onehot_labels.get_shape()) |
| 767 | |
| 768 | if label_smoothing > 0: |
| 769 | num_classes = math_ops.cast( |
| 770 | array_ops.shape(onehot_labels)[-1], logits.dtype) |
| 771 | smooth_positives = 1.0 - label_smoothing |
nothing calls this directly
no test coverage detected