Creates a cross-entropy loss using tf.nn.sigmoid_cross_entropy_with_logits. `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 corresp
(
multi_class_labels, logits, weights=1.0, label_smoothing=0, scope=None,
loss_collection=ops.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS)
| 648 | |
| 649 | @tf_export(v1=["losses.sigmoid_cross_entropy"]) |
| 650 | def sigmoid_cross_entropy( |
| 651 | multi_class_labels, logits, weights=1.0, label_smoothing=0, scope=None, |
| 652 | loss_collection=ops.GraphKeys.LOSSES, |
| 653 | reduction=Reduction.SUM_BY_NONZERO_WEIGHTS): |
| 654 | """Creates a cross-entropy loss using tf.nn.sigmoid_cross_entropy_with_logits. |
| 655 | |
| 656 | `weights` acts as a coefficient for the loss. If a scalar is provided, |
| 657 | then the loss is simply scaled by the given value. If `weights` is a |
| 658 | tensor of shape `[batch_size]`, then the loss weights apply to each |
| 659 | corresponding sample. |
| 660 | |
| 661 | If `label_smoothing` is nonzero, smooth the labels towards 1/2: |
| 662 | |
| 663 | new_multiclass_labels = multiclass_labels * (1 - label_smoothing) |
| 664 | + 0.5 * label_smoothing |
| 665 | |
| 666 | Args: |
| 667 | multi_class_labels: `[batch_size, num_classes]` target integer labels in |
| 668 | `{0, 1}`. |
| 669 | logits: Float `[batch_size, num_classes]` logits outputs of the network. |
| 670 | weights: Optional `Tensor` whose rank is either 0, or the same rank as |
| 671 | `labels`, and must be broadcastable to `labels` (i.e., all dimensions must |
| 672 | be either `1`, or the same as the corresponding `losses` dimension). |
| 673 | label_smoothing: If greater than `0` then smooth the labels. |
| 674 | scope: The scope for the operations performed in computing the loss. |
| 675 | loss_collection: collection to which the loss will be added. |
| 676 | reduction: Type of reduction to apply to loss. |
| 677 | |
| 678 | Returns: |
| 679 | Weighted loss `Tensor` of the same type as `logits`. If `reduction` is |
| 680 | `NONE`, this has the same shape as `logits`; otherwise, it is scalar. |
| 681 | |
| 682 | Raises: |
| 683 | ValueError: If the shape of `logits` doesn't match that of |
| 684 | `multi_class_labels` or if the shape of `weights` is invalid, or if |
| 685 | `weights` is None. Also if `multi_class_labels` or `logits` is None. |
| 686 | |
| 687 | @compatibility(eager) |
| 688 | The `loss_collection` argument is ignored when executing eagerly. Consider |
| 689 | holding on to the return value or collecting losses via a `tf.keras.Model`. |
| 690 | @end_compatibility |
| 691 | """ |
| 692 | if multi_class_labels is None: |
| 693 | raise ValueError("multi_class_labels must not be None.") |
| 694 | if logits is None: |
| 695 | raise ValueError("logits must not be None.") |
| 696 | with ops.name_scope(scope, "sigmoid_cross_entropy_loss", |
| 697 | (logits, multi_class_labels, weights)) as scope: |
| 698 | logits = ops.convert_to_tensor(logits) |
| 699 | multi_class_labels = math_ops.cast(multi_class_labels, logits.dtype) |
| 700 | logits.get_shape().assert_is_compatible_with(multi_class_labels.get_shape()) |
| 701 | |
| 702 | if label_smoothing > 0: |
| 703 | multi_class_labels = (multi_class_labels * (1 - label_smoothing) + |
| 704 | 0.5 * label_smoothing) |
| 705 | |
| 706 | losses = nn.sigmoid_cross_entropy_with_logits(labels=multi_class_labels, |
| 707 | logits=logits, |
nothing calls this directly
no test coverage detected