Commit Graph

224 Commits

Author SHA1 Message Date
isdanni
6c7013a3dc [Doc] Add weight dtype in torch.nn.CrossEntropyLoss (#110998)
Fixes #101213

Pull Request resolved: https://github.com/pytorch/pytorch/pull/110998
Approved by: https://github.com/albanD
2023-10-11 19:52:13 +00:00
Minh-Long Luu (刘明龙)
95f268e426 Add examples for nn.CosineEmbeddingLoss (#108215)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/108215
Approved by: https://github.com/mikaylagawarecki
2023-08-31 20:01:24 +00:00
dvorst
91a674ccd4 Fix docstring for shape of target for MultiLabelSoftMarginLoss (#107817)
Fixes #92000

The documentation at https://pytorch.org/docs/stable/generated/torch.nn.MultiLabelSoftMarginLoss.html#multilabelsoftmarginloss states:
> label targets padded by -1 ensuring same shape as the input.

However, the shape of input and target tensor are compared, and an exception is raised if they differ in either dimension 0 or 1. Meaning the label targets are never padded. See the code snippet below and the resulting output. The documentation is therefore adjusted to:
> label targets must have the same shape as the input.

```
import torch
import torch.nn as nn

# Create some example data
input = torch.tensor(
    [
        [0.8, 0.2, -0.5],
        [0.1, 0.9, 0.3],
    ]
)
target1 = torch.tensor(
    [
        [1, 0, 1],
        [0, 1, 1],
        [0, 1, 1],
    ]
)
target2 = torch.tensor(
    [
        [1, 0],
        [0, 1],
    ]
)
target3 = torch.tensor(
    [
        [1, 0, 1],
        [0, 1, 1],
    ]
)
loss_func = nn.MultiLabelSoftMarginLoss()
try:
    loss = loss_func(input, target1).item()
except RuntimeError as e:
    print('target1 ', e)
try:
    loss = loss_func(input, target2).item()
except RuntimeError as e:
    print('target2 ', e)
loss = loss_func(input, target3).item()
print('target3 ', loss)
```

output:
```
target1  The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 0
target2  The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 1
target3  0.6305370926856995
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/107817
Approved by: https://github.com/mikaylagawarecki
2023-08-24 15:13:46 +00:00
GwiHwan
2d41fa9d38 Revise err msgs for weight param of Multimarginloss (#106047)
Summary: fix lint issue of #106019

Fix: https://github.com/pytorch/pytorch/issues/106020
Pull Request resolved: https://github.com/pytorch/pytorch/pull/106047
Approved by: https://github.com/Skylion007
2023-07-27 01:44:13 +00:00
Bosheng Zhang (Daniel)
1518d5eec4 Update Documentation for TripletMarginLoss (#105115)
This PR updates the documentation for `TripletMarginLoss` in `torch.nn`. The previous version of the documentation didn't mention the parameter `eps` used for numerical stability.

This PR does the following:
1. Describes the purpose and use of the `eps` parameter in the `TripletMarginLoss` class documentation.
2. Includes `eps` in the example usage of `TripletMarginLoss`.

Please review this update for the completeness with respect to the `TripletMarginLoss` functionality. If there are any issues or further changes needed, please let me know.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/105115
Approved by: https://github.com/mikaylagawarecki
2023-07-14 20:04:25 +00:00
Nikita Karetnikov
c03558fa8d [doc] apply weight after p in MultiMarginLoss (#104844)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/104844
Approved by: https://github.com/lezcano
2023-07-12 03:42:14 +00:00
Danni Li
35a8242226 [Doc] Add sum reduction for CTCLoss (#100235)
Summary:

Fix: #99141

Reference:
39b885cbbf/aten/src/ATen/native/LossCTC.cpp (L366-L371)

Test Plan: See GitHub Tests.

Differential Revision: D45387774

Pull Request resolved: https://github.com/pytorch/pytorch/pull/100235
Approved by: https://github.com/albanD, https://github.com/mikaylagawarecki
2023-06-28 16:08:22 +00:00
cviviers
81c181dc01 Update BCEWithLogitsLoss pos_weight description in documentation (#101567)
Fixes #82496 and #65702

Pull Request resolved: https://github.com/pytorch/pytorch/pull/101567
Approved by: https://github.com/mikaylagawarecki
2023-05-19 21:23:21 +00:00
Joel Schlosser
bd9d50a3fc Remove future deprecation warning from kl_div docs (#96541)
Fixes #95687
Pull Request resolved: https://github.com/pytorch/pytorch/pull/96541
Approved by: https://github.com/albanD
2023-05-05 23:01:21 +00:00
Carl Lemaire
9a5fed1bd0 Harmonize BCELoss example to F.binary_cross_entropy (#95178)
About that line:

```
torch.empty(3).random_(2)
```
* Since BCE supports targets in the interval [0, 1], a better example is to sample from uniform(0, 1), using `rand`
* BCE supports multiple dimensions, and the example in `F.binary_cross_entropy` highlights it
* `rand` is more well known than `random_`, which is a bit obscure (`rand` is in the [Random Sampling section in the docs](https://pytorch.org/docs/stable/torch.html#random-sampling))
* Chaining `empty` and `random_` gives binary values as floats, which is a weird way to get that result
* Why do it in two steps when we have sampling functions that do it in a single step?
Pull Request resolved: https://github.com/pytorch/pytorch/pull/95178
Approved by: https://github.com/albanD, https://github.com/kit1980
2023-03-20 23:45:01 +00:00
itmorn
ea6113ea20 Update loss.py (#95367)
Fix the dimension bug in the document

Fixes #ISSUE_NUMBER

Pull Request resolved: https://github.com/pytorch/pytorch/pull/95367
Approved by: https://github.com/albanD, https://github.com/kit1980
2023-03-20 23:24:49 +00:00
James Braza
b390e7037e [docs] passing LogSoftmax into NLLLoss (#97001)
Fixes https://github.com/pytorch/pytorch/issues/96795

Pull Request resolved: https://github.com/pytorch/pytorch/pull/97001
Approved by: https://github.com/soulitzer
2023-03-17 23:22:13 +00:00
ZiadAmerr
3cdf18cb4f Corrected HingeEmbeddingLoss documentation (#95140)
Minor correction. `HingeEmbeddingLoss`'s documentation had this piecewise function; but there is no $\Delta$ in the function definition, it was used to denote `margin`.

$$l_n = \begin{cases}
            x_n, & \text{if}\; y_n = 1,\\
            \max \{0, \Delta - x_n\}, & \text{if}\; y_n = -1,
        \end{cases}$$

Following other documentation guidelines, `HuberLoss` has a parameter `delta`, and its piecewise function is defined as follows; using $delta$ as a reference to the `delta` parameter and not $\Delta$.

$$l_n = \begin{cases}
        0.5 (x_n - y_n)^2, & \text{if } |x_n - y_n| < delta \\
        delta * (|x_n - y_n| - 0.5 * delta), & \text{otherwise }
        \end{cases}$$

So by analogy, `HingeEmbeddingLoss` should also be the same, thus, the right piecewise function for it should be like the following instead.

$$l_n = \begin{cases}
            x_n, & \text{if}\; y_n = 1,\\
            \max \{0, margin- x_n\}, & \text{if}\; y_n = -1,
        \end{cases}$$
Pull Request resolved: https://github.com/pytorch/pytorch/pull/95140
Approved by: https://github.com/albanD
2023-03-13 14:32:04 +00:00
Xuehai Pan
5b1cedacde [BE] [2/3] Rewrite super() calls in functorch and torch (#94588)
Rewrite Python built-in class `super()` calls. Only non-semantic changes should be applied.

- #94587
- #94588
- #94592

Also, methods with only a `super()` call are removed:

```diff
class MyModule(nn.Module):
-   def __init__(self):
-       super().__init__()
-
    def forward(self, ...):
        ...
```

Some cases that change the semantics should be kept unchanged. E.g.:

f152a79be9/caffe2/python/net_printer.py (L184-L190)

f152a79be9/test/test_jit_fuser_te.py (L2628-L2635)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/94588
Approved by: https://github.com/ezyang, https://github.com/albanD
2023-02-10 21:16:33 +00:00
joncrall
ad782ff7df Enable xdoctest runner in CI for real this time (#83816)
Builds on #83317 and enables running the doctests. Just need to figure out what is causing the failures.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/83816
Approved by: https://github.com/ezyang, https://github.com/malfet
2022-12-29 05:32:42 +00:00
WEN Hao
fc4acd4425 Fix error in the index range math expression in the docstring of MultiMarginLoss (#84513)
Fixes #84512

Pull Request resolved: https://github.com/pytorch/pytorch/pull/84513
Approved by: https://github.com/Lezcano, https://github.com/cpuhrsch
2022-09-07 19:12:33 +00:00
joncrall
b136f3f310 More doctest refinements. (#83317)
Follow up to #82797

Now that the doctests themselves are in a better state, we should be able to enable xdoctest on the CI so they stay that way.

@ezyang @vadimkantorov
Pull Request resolved: https://github.com/pytorch/pytorch/pull/83317
Approved by: https://github.com/ezyang
2022-08-22 20:07:26 +00:00
joncrall
4618371da5 Integrate xdoctest - Rebased (#82797)
This is a new version of #15648 based on the latest master branch.

Unlike the previous PR where I fixed a lot of the doctests in addition to integrating xdoctest, I'm going to reduce the scope here. I'm simply going to integrate xdoctest, and then I'm going to mark all of the failing tests as "SKIP". This will let xdoctest run on the dashboards, provide some value, and still let the dashboards pass. I'll leave fixing the doctests themselves to another PR.

In my initial commit, I do the bare minimum to get something running with failing dashboards. The few tests that I marked as skip are causing segfaults. Running xdoctest results in 293 failed, 201 passed tests. The next commits will be to disable those tests. (unfortunately I don't have a tool that will insert the `#xdoctest: +SKIP` directive over every failing test, so I'm going to do this mostly manually.)

Fixes https://github.com/pytorch/pytorch/issues/71105

@ezyang
Pull Request resolved: https://github.com/pytorch/pytorch/pull/82797
Approved by: https://github.com/ezyang
2022-08-12 02:08:01 +00:00
Alex Li
1fedd40424 Update cross entropy documentation to metion logits clearly (#82538)
### Description
Improved the documentation for cross entropy as it is a common point of confusion.

### Issue
#82081

### Testing
I did not test this change as it is tiny and documentation-only
Pull Request resolved: https://github.com/pytorch/pytorch/pull/82538
Approved by: https://github.com/jbschlosser
2022-08-08 22:24:28 +00:00
ProGamerGov
71d50f4f89 Change docstring type callable to Callable for consistency (#82487)
### Description

Across PyTorch's docstrings, both `callable` and `Callable` for variable types. The Callable should be capitalized as we are referring to the `Callable` type, and not the Python `callable()` function.

### Testing

There shouldn't be any testing required.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/82487
Approved by: https://github.com/albanD
2022-08-01 17:26:09 +00:00
ProGamerGov
357b7d589c Fix docstring inconsistencies: string -> str, boolean -> bool (#82410)
### Description

Throughout the PyTorch docs and codebase, the `string` type in docstrings is referred to by two separate names. This leads to inconsistent docs, like you can see here: https://pytorch.org/docs/stable/generated/torch.nn.Conv3d.html#torch.nn.Conv3d

This PR fixes this issue by ensuring that all mentions of the string type in docstrings, are using the same format that Sphinx generates hyperlinks for.

### Testing
No testing should be required for this change

Pull Request resolved: https://github.com/pytorch/pytorch/pull/82410
Approved by: https://github.com/jbschlosser
2022-07-28 21:29:57 +00:00
PyTorch MergeBot
9db3c517de Add __all__ for torch.nn.modules, torch.distributed.elastic, torch.nn.utils submodules (#80240)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/80240
Approved by: https://github.com/rohan-varma
2022-06-27 17:11:12 +00:00
Alex Zhuang
081ff9602a Correct torch.nn.CrossEntropyLoss output shape specification (#79568)
Fixes #79531

Pull Request resolved: https://github.com/pytorch/pytorch/pull/79568
Approved by: https://github.com/jbschlosser
2022-06-15 14:28:02 +00:00
Aditya Kane
4e4c80d539 Nit in TripletMarginLoss
`:math` -> `:math:`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/76629
Approved by: https://github.com/albanD
2022-05-02 14:31:24 +00:00
Jake Tae
c02bdfae5d Docs: Fix log_target example in kl divergence
Fixes #74453.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/74945
Approved by: https://github.com/albanD
2022-04-08 20:27:01 +00:00
Hannes Hansen
6a58651256 changed documentation from cosine distance to cosine similarity; fixe…
This PR updates the documentation for the CosineEmbeddingLoss.
The loss function uses the `cosine similarity` but in the documentation the term `cosine distance` is used. Therefor the term is changed to `cosine similarity`

Fixes #75104

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75188
Approved by: https://github.com/cpuhrsch
2022-04-04 18:45:16 +00:00
Joel Schlosser
f670179c0a Fix doc regressions for various modules and functional forms (#73014)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73014

Fixes #72501
Fixes #72502
Fixes #72503
Fixes #72504
Fixes #72505
Fixes #72506
Fixes #72507
Fixes #72509
Fixes #72510

Test Plan: Imported from OSS

Reviewed By: albanD

Differential Revision: D34305640

Pulled By: jbschlosser

fbshipit-source-id: 62f341633fdb0316eaa346cf7247865290eb830a
(cherry picked from commit 8362d264e7)
2022-02-17 22:40:18 +00:00
kshitij12345
2981534f54 [nn] cross_entropy: no batch dim support (#71055)
Summary:
Reference: https://github.com/pytorch/pytorch/issues/60585

cc albanD mruberry jbschlosser walterddr kshitij12345

Pull Request resolved: https://github.com/pytorch/pytorch/pull/71055

Reviewed By: anjali411

Differential Revision: D33567403

Pulled By: jbschlosser

fbshipit-source-id: 4d0a311ad7419387c4547e43e533840c8b6d09d8
2022-01-13 14:48:51 -08:00
George Qi
d7db5fb462 ctc loss no batch dim support (#70092)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/70092

Test Plan: Imported from OSS

Reviewed By: jbschlosser

Differential Revision: D33280068

Pulled By: george-qi

fbshipit-source-id: 3278fb2d745a396fe27c00fb5f40df0e7f584f81
2022-01-07 14:33:22 -08:00
Jake Tae
ce409d8f50 docs: clarify smooth l1 == l1 when beta == 0 (#70673)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/68558.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/70673

Reviewed By: albanD

Differential Revision: D33430267

Pulled By: jbschlosser

fbshipit-source-id: db92187ff4f2799b19a6c4a5a6b653e9211c3aca
2022-01-05 14:35:35 -08:00
George Qi
4d49af863f GaussianNLLLoss no_batch_dim docs and testing (#69783)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/69783

Test Plan: Imported from OSS

Reviewed By: jbschlosser

Differential Revision: D33200486

Pulled By: george-qi

fbshipit-source-id: a2bc2b366772682825f879dae4ac29c1f4d6a5f1
2021-12-23 09:27:53 -08:00
PepeLotudo
da63f3f92b Corrected typo in Cross entropy formula (#70220)
Summary:
Changes made to line 1073: The denominator of the formula was the EXP(SUM(x)) and changed it to SUM(EXP(x))

Fixes #ISSUE_NUMBER

Pull Request resolved: https://github.com/pytorch/pytorch/pull/70220

Reviewed By: davidberard98

Differential Revision: D33279050

Pulled By: jbschlosser

fbshipit-source-id: 3e13aff5879240770e0cf2e047e7ef077784eb9c
2021-12-22 11:06:21 -08:00
Aditya Tewary
284758b585 correct NLLLoss parameters default value (#68426)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/17577

Previous
`size_average by default:  True`
`reduce by default: True`
Present
`size_average by default:  None`
`reduce by default: None`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/68426

Reviewed By: VitalyFedyunin

Differential Revision: D32463324

Pulled By: jbschlosser

fbshipit-source-id: 7ba9cd03c9fb6b2f19301e7e39c3c490de17202b
2021-11-16 13:45:52 -08:00
lezcano
8e1ead8e4d Fix the kl_div docs (#67443)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/67443

Fixes https://github.com/pytorch/pytorch/issues/57459

After discussing the linked issue, we resolved that `F.kl_div` computes
the right thing as to be consistent with the rest of the losses in
PyTorch.

To avoid any confusion, these docs add a note discussing how the PyTorch
implementation differs from the mathematical definition and the reasons
for doing so.

These docs also add an example that may further help understanding the
intended use of this loss.

cc brianjo mruberry

Test Plan: Imported from OSS

Reviewed By: bdhirsh

Differential Revision: D32136888

Pulled By: jbschlosser

fbshipit-source-id: 1ad0a606948656b44ff7d2a701d995c75875e671
2021-11-04 07:09:08 -07:00
kshitij12345
828a9dcc04 [nn] MarginRankingLoss : no batch dim (#64975)
Summary:
Reference: https://github.com/pytorch/pytorch/issues/60585

cc albanD mruberry jbschlosser walterddr

Pull Request resolved: https://github.com/pytorch/pytorch/pull/64975

Reviewed By: albanD

Differential Revision: D31906528

Pulled By: jbschlosser

fbshipit-source-id: 1127242a859085b1e06a4b71be19ad55049b38ba
2021-10-26 09:03:31 -07:00
Christopher Yeh
3097755e7a [DOC] Fix typo in KLDivLoss (#66583)
Summary:
Fix simple typo.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/66583

Reviewed By: soulitzer

Differential Revision: D31653998

Pulled By: jbschlosser

fbshipit-source-id: e4fc91be297cc9a85099d7883b42436b5e3392d3
2021-10-14 13:21:37 -07:00
kshitij12345
9c23f6eb7d [nn] TripletMarginLoss and PairwiseDistance : no batch dim (#64882)
Summary:
Reference: https://github.com/pytorch/pytorch/issues/60585

Pull Request resolved: https://github.com/pytorch/pytorch/pull/64882

Reviewed By: malfet

Differential Revision: D31055577

Pulled By: jbschlosser

fbshipit-source-id: 2f0a5a08619b672026b48a78bc7d83a6dccba0bf
2021-09-21 07:29:48 -07:00
kshitij12345
01e92f2a56 [nn] no batch dim support: CosineEmbeddingLoss (#64590)
Summary:
Reference: https://github.com/pytorch/pytorch/issues/60585

TODO
* [x] Add tests

Pull Request resolved: https://github.com/pytorch/pytorch/pull/64590

Reviewed By: H-Huang

Differential Revision: D30900775

Pulled By: jbschlosser

fbshipit-source-id: d24e72787017e79afbf8f04a94901a290485b81a
2021-09-13 10:45:33 -07:00
Thomas J. Fan
d3bcba5f85 ENH Adds label_smoothing to cross entropy loss (#63122)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/7455

Partially resolves pytorch/vision#4281

Pull Request resolved: https://github.com/pytorch/pytorch/pull/63122

Reviewed By: iramazanli

Differential Revision: D30586076

Pulled By: jbschlosser

fbshipit-source-id: 06afc3aa1f8b9edb07fe9ed68c58968ad1926924
2021-08-29 23:33:04 -07:00
Jonathan Chang
3abb606091 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
2021-08-27 09:51:05 -07:00
Thomas J. Fan
2ca2761f3c ENH Adds no_batch_dim for NLLLoss (#62651)
Summary:
Towards https://github.com/pytorch/pytorch/issues/60585

Pull Request resolved: https://github.com/pytorch/pytorch/pull/62651

Reviewed By: VitalyFedyunin

Differential Revision: D30303340

Pulled By: jbschlosser

fbshipit-source-id: 7ab478cf63bf6cd1f850cad5fd101e74a2cfe3f5
2021-08-24 08:27:27 -07:00
Joel Schlosser
a42345adee Support for target with class probs in CrossEntropyLoss (#61044)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/11959

Alternative approach to creating a new `CrossEntropyLossWithSoftLabels` class. This PR simply adds support for "soft targets" AKA class probabilities to the existing `CrossEntropyLoss` and `NLLLoss` classes.

Implementation is dumb and simple right now, but future work can add higher performance kernels for this case.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/61044

Reviewed By: zou3519

Differential Revision: D29876894

Pulled By: jbschlosser

fbshipit-source-id: 75629abd432284e10d4640173bc1b9be3c52af00
2021-07-29 10:04:41 -07:00
Thomas J. Fan
80a662e773 ENH Updates docs and tests for classification modules that already support no batch dims (#61874)
Summary:
Towards https://github.com/pytorch/pytorch/issues/60585

Pull Request resolved: https://github.com/pytorch/pytorch/pull/61874

Reviewed By: heitorschueroff

Differential Revision: D29979977

Pulled By: jbschlosser

fbshipit-source-id: 82c19151aa7220e564337b05d7677d52981e0aa2
2021-07-29 09:14:52 -07:00
Thomas J. Fan
f03e7170f0 ENH Updates docs and tests for regression modules that already support no-batch-dims (#61461)
Summary:
Towards https://github.com/pytorch/pytorch/issues/60585

This PR does not use `check_sum_reduction` because I wanted to test every reduction option.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/61461

Reviewed By: suo

Differential Revision: D29883744

Pulled By: jbschlosser

fbshipit-source-id: cdad0effb41f0484938caad0d4c9d6d83e2aec07
2021-07-23 16:40:17 -07:00
Thomas J. Fan
4e51503b1f DOC Improves input and target docstring for loss functions (#60553)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/56581

Pull Request resolved: https://github.com/pytorch/pytorch/pull/60553

Reviewed By: VitalyFedyunin

Differential Revision: D29343797

Pulled By: jbschlosser

fbshipit-source-id: cafc29d60a204a21deff56dd4900157d2adbd91e
2021-06-23 20:20:29 -07:00
aflah02
fd2a36369a Fixed torch.nn.MultiMarginLoss equation format error (#59188)
Summary:
Removed the extra parenthesis from the right side
Fixes https://github.com/pytorch/pytorch/issues/58634

Pull Request resolved: https://github.com/pytorch/pytorch/pull/59188

Reviewed By: ngimel

Differential Revision: D28797720

Pulled By: jbschlosser

fbshipit-source-id: 47e3084526389e7d1cc17c1a01b253e666c58784
2021-06-01 12:04:34 -07:00
Joel Schlosser
af463d2235 Add shape documentation for CosineEmbeddingLoss (#58403)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/52732

Pull Request resolved: https://github.com/pytorch/pytorch/pull/58403

Reviewed By: HDCharles

Differential Revision: D28480076

Pulled By: jbschlosser

fbshipit-source-id: c2c51e9da86e274e80126bbcabebb27270f2d2d0
2021-05-17 18:14:16 -07:00
Hameer Abbasi
46e4b2dbda Convert assert -> cast. (#57458)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/55868.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/57458

Reviewed By: mruberry

Differential Revision: D28365745

Pulled By: walterddr

fbshipit-source-id: 35cc3fa85f87b0ef98cf970f620ab909d240c7be
2021-05-12 13:54:16 -07:00
M.L. Croci
1f0223d6bb Fix bug in gaussian_nll_loss (#56469)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/53964. cc albanD almson

## Major changes:
- Overhauled the actual loss calculation so that the shapes are now correct (in functional.py)
- added the missing doc in nn.functional.rst

## Minor changes (in functional.py):
- I removed the previous check on whether input and target were the same shape. This is to allow for broadcasting, say when you have 10 predictions that all have the same target.
- I added some comments to explain each shape check in detail. Let me know if these should be shortened/cut.

Screenshots of updated docs attached.
Let me know what you think, thanks!

## Edit: Description of change of behaviour (affecting BC):
The backwards-compatibility is only affected for the `reduction='none'` mode. This was the source of the bug. For tensors with size (N, D), the old returned loss had size (N), as incorrect summation was happening. It will now have size (N, D) as expected.

### Example
Define input tensors, all with size (2, 3).
`input = torch.tensor([[0., 1., 3.], [2., 4., 0.]], requires_grad=True)`
`target = torch.tensor([[1., 4., 2.], [-1., 2., 3.]])`
`var = 2*torch.ones(size=(2, 3), requires_grad=True)`

Initialise loss with reduction mode 'none'. We expect the returned loss to have the same size as the input tensors, (2, 3).
`loss = torch.nn.GaussianNLLLoss(reduction='none')`

Old behaviour:
`print(loss(input, target, var)) `
`# Gives tensor([3.7897, 6.5397], grad_fn=<MulBackward0>. This has size (2).`

New behaviour:
`print(loss(input, target, var)) `
`# Gives tensor([[0.5966, 2.5966, 0.5966], [2.5966, 1.3466, 2.5966]], grad_fn=<MulBackward0>)`
`# This has the expected size, (2, 3).`

To recover the old behaviour, sum along all dimensions except for the 0th:
`print(loss(input, target, var).sum(dim=1))`
`# Gives tensor([3.7897, 6.5397], grad_fn=<SumBackward1>.`

![doc1](https://user-images.githubusercontent.com/26558092/115391089-f7f47b00-a1d6-11eb-8726-e4da9057aee0.png)
![doc2](https://user-images.githubusercontent.com/26558092/115391094-f925a800-a1d6-11eb-954b-afd187f42bc7.png)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/56469

Reviewed By: jbschlosser, agolynski

Differential Revision: D27894170

Pulled By: albanD

fbshipit-source-id: 197890189c97c22109491c47f469336b5b03a23f
2021-04-22 07:43:48 -07:00
Joel Schlosser
e86476f736 Huber loss (#50553)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/48595.

## Background

This PR implements HuberLoss, which differs from SmoothL1Loss by a factor of beta. The current implementation does not share logic between the two. Feedback is welcome for the optimal way to minimize code duplication while remaining performant.

I've done some early [benchmarking](https://pytorch.org/tutorials/recipes/recipes/benchmark.html#collecting-instruction-counts-with-callgrind) with Huber calling in to the Smooth L1 kernel and scaling afterwards; for the simple test case I used, instruction counts are as follows:
```
Huber loss calls dedicated Huber kernel: 2,795,300
Huber loss calls Smooth L1 kernel and scales afterwards: 4,523,612
```
With these numbers, instruction counts are ~62% higher when using the pre-existing Smooth L1 kernel.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/50553

Test Plan:
```
python test/test_nn.py TestNN.test_HuberLoss
python test/test_nn.py TestNN.test_HuberLoss_delta
python test/test_nn.py TestNN.test_huber_loss_invalid_delta
python test/test_nn.py TestNNDeviceTypeCPU.test_smooth_l1_loss_vs_huber_loss_cpu
python test/test_nn.py TestNNDeviceTypeCUDA.test_smooth_l1_loss_vs_huber_loss_cuda
python test/test_nn.py TestNNDeviceTypeCPU.test_invalid_reduction_strings_cpu
python test/test_nn.py TestNNDeviceTypeCUDA.test_invalid_reduction_strings_cuda
python test/test_nn.py TestNN.test_loss_equal_input_target_shape
python test/test_nn.py TestNN.test_pointwise_loss_broadcast
python test/test_overrides.py
python test/test_jit.py TestJitGeneratedFunctional.test_nn_huber_loss
python test/test_type_hints.py
python test/test_cpp_api_parity.py
build/bin/test_api
```

## Documentation
<img width="677" alt="Screen Shot 2021-01-14 at 4 25 08 PM" src="https://user-images.githubusercontent.com/75754324/104651224-5a445980-5685-11eb-884b-14ea517958c2.png">
<img width="677" alt="Screen Shot 2021-01-14 at 4 24 35 PM" src="https://user-images.githubusercontent.com/75754324/104651190-4e589780-5685-11eb-974d-8c63a89c050e.png">
<img width="661" alt="Screen Shot 2021-01-14 at 4 24 45 PM" src="https://user-images.githubusercontent.com/75754324/104651198-50225b00-5685-11eb-958e-136b36f6f8a8.png">
<img width="869" alt="Screen Shot 2021-01-14 at 4 25 27 PM" src="https://user-images.githubusercontent.com/75754324/104651208-53b5e200-5685-11eb-9fe4-5ff433aa13c5.png">
<img width="862" alt="Screen Shot 2021-01-14 at 4 25 48 PM" src="https://user-images.githubusercontent.com/75754324/104651209-53b5e200-5685-11eb-8051-b0cfddcb07d3.png">

Reviewed By: H-Huang

Differential Revision: D26734071

Pulled By: jbschlosser

fbshipit-source-id: c98c1b5f32a16f7a2a4e04bdce678080eceed5d5
2021-03-02 17:30:45 -08:00