Skip to content

Commit de1ca9f

Browse files
leomilanosvekars
andauthored
Switch from sin(x) to exp(x) as the target function in polynomial_autograd (#3597)
Fixes #3596 This PR branch uses 'exp(x)' instead of 'sin(x)' as the 'y' (target) variable. The reason for this is faster and clearer convergence, since exp(x) is well approximated by a Taylor expansion around the origin. Also, all the derivatives are exp(0)=1, so the polynomial coefficients are trivially '1/n! '(for the 'n-th' term). I am also improving the information output to the user inside the optimization loop. Finally, I am making the top doc string a raw string, to avoid a python3 warning about it. cc @albanD @jbschlosser --------- Co-authored-by: Svetlana Karslioglu <[email protected]>
1 parent d7a5681 commit de1ca9f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

beginner_source/examples_autograd/polynomial_autograd.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""
1+
r"""
22
PyTorch: Tensors and autograd
33
-------------------------------
44
@@ -27,8 +27,8 @@
2727
# Create Tensors to hold input and outputs.
2828
# By default, requires_grad=False, which indicates that we do not need to
2929
# compute gradients with respect to these Tensors during the backward pass.
30-
x = torch.linspace(-math.pi, math.pi, 2000, dtype=dtype)
31-
y = torch.sin(x)
30+
x = torch.linspace(-1, 1, 2000, dtype=dtype)
31+
y = torch.exp(x) # A Taylor expansion would be 1 + x + (1/2) x**2 + (1/3!) x**3 + ...
3232

3333
# Create random Tensors for weights. For a third order polynomial, we need
3434
# 4 weights: y = a + b x + c x^2 + d x^3
@@ -39,17 +39,23 @@
3939
c = torch.randn((), dtype=dtype, requires_grad=True)
4040
d = torch.randn((), dtype=dtype, requires_grad=True)
4141

42-
learning_rate = 1e-6
43-
for t in range(2000):
42+
initial_loss = 1.
43+
learning_rate = 1e-5
44+
for t in range(5000):
4445
# Forward pass: compute predicted y using operations on Tensors.
4546
y_pred = a + b * x + c * x ** 2 + d * x ** 3
4647

4748
# Compute and print loss using operations on Tensors.
4849
# Now loss is a Tensor of shape (1,)
4950
# loss.item() gets the scalar value held in the loss.
5051
loss = (y_pred - y).pow(2).sum()
52+
53+
# Calculare initial loss, so we can report loss relative to it
54+
if t==0:
55+
initial_loss=loss.item()
56+
5157
if t % 100 == 99:
52-
print(t, loss.item())
58+
print(f'Iteration t = {t:4d} loss(t)/loss(0) = {round(loss.item()/initial_loss, 6):10.6f} a = {a.item():10.6f} b = {b.item():10.6f} c = {c.item():10.6f} d = {d.item():10.6f}')
5359

5460
# Use autograd to compute the backward pass. This call will compute the
5561
# gradient of loss with respect to all Tensors with requires_grad=True.

0 commit comments

Comments
 (0)