Skip to content

Commit c5c4d86

Browse files
committed
Automated tutorials push
1 parent a2f57c1 commit c5c4d86

File tree

255 files changed

+17268
-23275
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+17268
-23275
lines changed
0 Bytes
Binary file not shown.

_downloads/0184bded18578d24a48fdfad2c701b09/polynomial_autograd.ipynb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
"# Create Tensors to hold input and outputs.\n",
5454
"# By default, requires_grad=False, which indicates that we do not need to\n",
5555
"# compute gradients with respect to these Tensors during the backward pass.\n",
56-
"x = torch.linspace(-math.pi, math.pi, 2000, dtype=dtype)\n",
57-
"y = torch.sin(x)\n",
56+
"x = torch.linspace(-1, 1, 2000, dtype=dtype)\n",
57+
"y = torch.exp(x) # A Taylor expansion would be 1 + x + (1/2) x**2 + (1/3!) x**3 + ...\n",
5858
"\n",
5959
"# Create random Tensors for weights. For a third order polynomial, we need\n",
6060
"# 4 weights: y = a + b x + c x^2 + d x^3\n",
@@ -65,17 +65,23 @@
6565
"c = torch.randn((), dtype=dtype, requires_grad=True)\n",
6666
"d = torch.randn((), dtype=dtype, requires_grad=True)\n",
6767
"\n",
68-
"learning_rate = 1e-6\n",
69-
"for t in range(2000):\n",
68+
"initial_loss = 1.\n",
69+
"learning_rate = 1e-5\n",
70+
"for t in range(5000):\n",
7071
" # Forward pass: compute predicted y using operations on Tensors.\n",
7172
" y_pred = a + b * x + c * x ** 2 + d * x ** 3\n",
7273
"\n",
7374
" # Compute and print loss using operations on Tensors.\n",
7475
" # Now loss is a Tensor of shape (1,)\n",
7576
" # loss.item() gets the scalar value held in the loss.\n",
7677
" loss = (y_pred - y).pow(2).sum()\n",
78+
"\n",
79+
" # Calculare initial loss, so we can report loss relative to it\n",
80+
" if t==0:\n",
81+
" initial_loss=loss.item()\n",
82+
"\n",
7783
" if t % 100 == 99:\n",
78-
" print(t, loss.item())\n",
84+
" 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}')\n",
7985
"\n",
8086
" # Use autograd to compute the backward pass. This call will compute the\n",
8187
" # gradient of loss with respect to all Tensors with requires_grad=True.\n",
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)