KEMBAR78
DOC Adds code comment for _ConvNd.reset_parameters by thomasjpfan · Pull Request #58931 · pytorch/pytorch · GitHub
Skip to content

Conversation

@thomasjpfan
Copy link
Contributor

Fixes #55741 by adding a comment regarding the behavior of kaiming_uniform_

The docstring is correct in this case. For example:

import math
import matplotlib.pyplot as plt

import torch
import torch.nn as nn

in_channels = 120
groups = 2
kernel = (3, 8)
m = nn.Conv2d(in_channels=in_channels, groups=groups,
              out_channels=100, kernel_size=kernel)

k = math.sqrt(groups / (in_channels * math.prod(kernel)))
print(f"k: {k:0.6f}")

print(f"min weight: {m.weight.min().item():0.6f}")
print(f"max weight: {m.weight.max().item():0.6f}")

outputs:

k: 0.026352
min weight: -0.026352
max weight: 0.026352

And when we plot the distribution, it is uniform with the correct bounds:

_ = plt.hist(m.weight.detach().numpy().ravel())

Unknown

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented May 25, 2021

💊 CI failures summary and remediations

As of commit 0e77bc5 (more details on the Dr. CI page):


  • 5/5 failures possibly* introduced in this PR
    • 1/5 non-scanned failure(s)

🕵️ 3 new failures recognized by patterns

The following CI failures do not appear to be due to upstream breakages:

See CircleCI build pytorch_linux_xenial_cuda11_1_cudnn8_py3_gcc7_test1 (1/3)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

May 25 23:38:57 AssertionError: RuntimeError not raised
May 25 23:38:57   File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 292, in instantiated_test
May 25 23:38:57     result = test_fn(self, *args)
May 25 23:38:57   File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 266, in test_wrapper
May 25 23:38:57     return test(*args, **kwargs)
May 25 23:38:57   File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 617, in dep_fn
May 25 23:38:57     return fn(slf, device, *args, **kwargs)
May 25 23:38:57   File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py", line 765, in only_fn
May 25 23:38:57     return fn(self, device, *args, **kwargs)
May 25 23:38:57   File "test_ops.py", line 46, in test_unsupported_dtypes
May 25 23:38:57     op(sample.input, *sample.args, **sample.kwargs)
May 25 23:38:57 AssertionError: RuntimeError not raised
May 25 23:38:57 
May 25 23:38:58 ----------------------------------------------------------------------
May 25 23:38:58 Ran 7751 tests in 1305.698s
May 25 23:38:58 
May 25 23:38:58 FAILED (failures=1, skipped=1800)
May 25 23:38:58 
May 25 23:38:58 Generating XML reports...
May 25 23:38:58 Generated XML report: test-reports/python-unittest/test_ops/TEST-TestCommonCUDA-20210525231711.xml
May 25 23:38:58 Generated XML report: test-reports/python-unittest/test_ops/TEST-TestGradientsCUDA-20210525231711.xml
May 25 23:38:58 Generated XML report: test-reports/python-unittest/test_ops/TEST-TestOpInfoCUDA-20210525231711.xml

See CircleCI build pytorch_linux_xenial_py3_6_gcc5_4_build (2/3)

Step: "(Optional) Merge target branch" (full log | diagnosis details | 🔁 rerun)

Automatic merge failed; fix conflicts and then commit the result.
CONFLICT (add/add): Merge conflict in .azure_pipelines/pytorch-tests-pipeline.yml
Auto-merging .azure_pipelines/pytorch-tests-pipeline.yml
CONFLICT (add/add): Merge conflict in .azure_pipelines/job_templates/wheel-wait-template.yml
Auto-merging .azure_pipelines/job_templates/wheel-wait-template.yml
CONFLICT (add/add): Merge conflict in .azure_pipelines/job_templates/wheel-wait-job-template.yml
Auto-merging .azure_pipelines/job_templates/wheel-wait-job-template.yml
CONFLICT (add/add): Merge conflict in .azure_pipelines/job_templates/pytorch-template-win.yml
Auto-merging .azure_pipelines/job_templates/pytorch-template-win.yml
CONFLICT (add/add): Merge conflict in .azure_pipelines/job_templates/pytorch-template-unix.yml
Auto-merging .azure_pipelines/job_templates/pytorch-template-unix.yml
Automatic merge failed; fix conflicts and then commit the result.


Exited with code exit status 1

See CircleCI build pytorch_xla_linux_bionic_py3_6_clang9_build (3/3)

Step: "(Optional) Merge target branch" (full log | diagnosis details | 🔁 rerun)

Automatic merge failed; fix conflicts and then commit the result.
CONFLICT (add/add): Merge conflict in .azure_pipelines/pytorch-tests-pipeline.yml
Auto-merging .azure_pipelines/pytorch-tests-pipeline.yml
CONFLICT (add/add): Merge conflict in .azure_pipelines/job_templates/wheel-wait-template.yml
Auto-merging .azure_pipelines/job_templates/wheel-wait-template.yml
CONFLICT (add/add): Merge conflict in .azure_pipelines/job_templates/wheel-wait-job-template.yml
Auto-merging .azure_pipelines/job_templates/wheel-wait-job-template.yml
CONFLICT (add/add): Merge conflict in .azure_pipelines/job_templates/pytorch-template-win.yml
Auto-merging .azure_pipelines/job_templates/pytorch-template-win.yml
CONFLICT (add/add): Merge conflict in .azure_pipelines/job_templates/pytorch-template-unix.yml
Auto-merging .azure_pipelines/job_templates/pytorch-template-unix.yml
Automatic merge failed; fix conflicts and then commit the result.


Exited with code exit status 1


1 failure not recognized by patterns:

Job Step Action
GitHub Actions Windows CI (pytorch-win-vs2019-cpu-py3) / render_test_results Unknown 🔁 rerun

This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

Copy link
Contributor

@jbschlosser jbschlosser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the clarification!


def reset_parameters(self) -> None:
# Setting a=sqrt(5) in kaiming_uniform is the same as initializing with
# uniform(-1/sqrt(k), 1/sqrt(k)), where k = weight.size(1) * prod(*kernel_size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a line to the effect of: "See for more details." where link points to Soumith's comment explaining the calculation (#15314 (comment))

@facebook-github-bot
Copy link
Contributor

@jbschlosser has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@jbschlosser merged this pull request in 8130f2f.

deniskokarev pushed a commit to deniskokarev/pytorch that referenced this pull request Jun 9, 2021
Summary:
Fixes pytorch#55741 by adding a comment regarding the behavior of `kaiming_uniform_`

The docstring is correct in this case. For example:

```python
import math
import matplotlib.pyplot as plt

import torch
import torch.nn as nn

in_channels = 120
groups = 2
kernel = (3, 8)
m = nn.Conv2d(in_channels=in_channels, groups=groups,
              out_channels=100, kernel_size=kernel)

k = math.sqrt(groups / (in_channels * math.prod(kernel)))
print(f"k: {k:0.6f}")

print(f"min weight: {m.weight.min().item():0.6f}")
print(f"max weight: {m.weight.max().item():0.6f}")
```

outputs:
```
k: 0.026352
min weight: -0.026352
max weight: 0.026352
```

And when we plot the distribution, it is uniform with the correct bounds:

```python
_ = plt.hist(m.weight.detach().numpy().ravel())
```

![Unknown](https://user-images.githubusercontent.com/5402633/119552979-21ba3800-bd69-11eb-8e10-e067c943abe3.png)

Pull Request resolved: pytorch#58931

Reviewed By: anjali411

Differential Revision: D28689863

Pulled By: jbschlosser

fbshipit-source-id: 98eebf265dfdaceed91f1991fc4b1592c0b3cf37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_ConvNd weight initialization does not match docs

4 participants