From 3abb6060910186f38fc1eef2a2169639533f0c54 Mon Sep 17 00:00:00 2001 From: Jonathan Chang Date: Fri, 27 Aug 2021 09:49:39 -0700 Subject: [PATCH] Add doc for nn.MultiMarginLoss (shape, example) (#63760) Summary: Fixes https://github.com/pytorch/pytorch/issues/63747 Pull Request resolved: https://github.com/pytorch/pytorch/pull/63760 Reviewed By: malfet Differential Revision: D30541581 Pulled By: jbschlosser fbshipit-source-id: 99560641e614296645eb0e51999513f57dfcfa98 --- torch/nn/modules/loss.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/torch/nn/modules/loss.py b/torch/nn/modules/loss.py index 03732b6..af1da83 100644 --- a/torch/nn/modules/loss.py +++ b/torch/nn/modules/loss.py @@ -1323,7 +1323,7 @@ class MultiMarginLoss(_WeightedLoss): The loss function then becomes: .. math:: - \text{loss}(x, y) = \frac{\sum_i \max(0, w[y] * (\text{margin} - x[y] + x[i]))^p)}{\text{x.size}(0)} + \text{loss}(x, y) = \frac{\sum_i \max(0, w[y] * (\text{margin} - x[y] + x[i]))^p}{\text{x.size}(0)} Args: p (int, optional): Has a default value of :math:`1`. :math:`1` and :math:`2` @@ -1347,6 +1347,20 @@ class MultiMarginLoss(_WeightedLoss): elements in the output, ``'sum'``: the output will be summed. Note: :attr:`size_average` and :attr:`reduce` are in the process of being deprecated, and in the meantime, specifying either of those two args will override :attr:`reduction`. Default: ``'mean'`` + + Shape: + - Input: :math:`(N, C)` or :math:`(C)`, where :math:`N` is the batch size and :math:`C` is the number of classes. + - Target: :math:`(N)` or :math:`()`, where each value is :math:`0 \leq \text{targets}[i] \leq C-1`. + - Output: scalar. If :attr:`reduction` is ``'none'``, then same shape as the target. + + Examples:: + + >>> loss = nn.MultiMarginLoss() + >>> x = torch.tensor([[0.1, 0.2, 0.4, 0.8]]) + >>> y = torch.tensor([3]) + >>> loss(x, y) + >>> # 0.25 * ((1-(0.8-0.1)) + (1-(0.8-0.2)) + (1-(0.8-0.4))) + tensor(0.3250) """ __constants__ = ['p', 'margin', 'reduction'] margin: float -- 2.7.4