fix description of equation in polynomial_autograd.py example - #3897
fix description of equation in polynomial_autograd.py example#3897telamonian wants to merge 3 commits into
Conversation
The equation that a + b*x + c*x^2 + d*x^3 converges to changed in a recent commit, from y=sin(x) to y=e^x. However, they didn't change the text description of the equation. This commit fixes that
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/3897
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @telamonian! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
After I saw #3899 I have now also removed the same unused lines of code that @gtsitsik removed in his version of this same PR. Also, I made up some code for plotting the fit/visualizing the "goodness" of fit: import matplotlib.pyplot as plt
def fit(x):
return a.item() + b.item() * x + c.item() * x**2 + d.item() * x**3
y_pred = [fit(_x) for _x in x]
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(x, y_pred, label="predicted")
ax.plot(x, y, label="real")
ax.set_xlabel("x")
ax.set_ylabel("$e^x$")
ax.legend()
plt.show()
Should I add this plotting code to the tutorial, or do you guys want to keep |
martinptm
left a comment
There was a problem hiding this comment.
Hello @telamonian, I have just stumbled across the mismatch in the tutorial that you address here. It does break the flow of the tutorial and I appreciate that you already prepared a fix.
However, in the context of the previous tutorial sections, I believe it would be more consistent to keep fitting a sin-curve in this section as well, i.e. fix the implementation to match the description instead of the other way around.
If this is done, it is important for the stability of the fit to keep the lower learning_rate too.
| d = torch.randn((), dtype=dtype, requires_grad=True) | ||
|
|
||
| initial_loss = 1. | ||
| learning_rate = 1e-5 |
There was a problem hiding this comment.
| learning_rate = 1e-5 | |
| learning_rate = 1e-6 |
| # By default, requires_grad=False, which indicates that we do not need to | ||
| # compute gradients with respect to these Tensors during the backward pass. | ||
| x = torch.linspace(-1, 1, 2000, dtype=dtype) | ||
| y = torch.exp(x) # A Taylor expansion would be 1 + x + (1/2) x**2 + (1/3!) x**3 + ... |
| holding the gradient of ``x`` with respect to some scalar value. | ||
| """ | ||
| import torch | ||
| import math |
| # Create Tensors to hold input and outputs. | ||
| # By default, requires_grad=False, which indicates that we do not need to | ||
| # compute gradients with respect to these Tensors during the backward pass. | ||
| x = torch.linspace(-1, 1, 2000, dtype=dtype) |
There was a problem hiding this comment.
| x = torch.linspace(-math.pi, math.pi, 2000, dtype=dtype) |
| PyTorch: Tensors and autograd | ||
| ------------------------------- | ||
|
|
||
| A third order polynomial, trained to predict :math:`y=\sin(x)` from :math:`-\pi` |
|
@martinptm you may want to check the discussion in #3899 as well. |

fixes #3632
Description
In the polynomial_autograd.py example, the equation that a + bx + cx^2 + d*x^3 converges to changed in a recent commit, from y=sin(x) to y=e^x. However, they didn't change the text description of the equation. This commit fixes that.
Checklist