Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions beginner_source/examples_tensor/polynomial_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
just cast the Tensor to a cuda datatype.
"""

import torch
import math
import torch


dtype = torch.float
Expand All @@ -39,19 +39,19 @@
learning_rate = 1e-6
for t in range(2000):
# Forward pass: compute predicted y
y_pred = a + b * x + c * x ** 2 + d * x ** 3
y_pred = a + (b * x) + (c * x.pow(2)) + (d * x.pow(3))

# Compute and print loss
loss = (y_pred - y).pow(2).sum().item()
if t % 100 == 99:
print(t, loss)

# Backprop to compute gradients of a, b, c, d with respect to loss
grad_y_pred = 2.0 * (y_pred - y)
grad_y_pred = -2.0 * (y - y_pred)
grad_a = grad_y_pred.sum()
grad_b = (grad_y_pred * x).sum()
grad_c = (grad_y_pred * x ** 2).sum()
grad_d = (grad_y_pred * x ** 3).sum()
grad_b = torch.matmul(input=grad_y_pred, other=x).sum()
grad_c = torch.matmul(input=grad_y_pred, other=x.pow(2)).sum()
grad_d = torch.matmul(input=grad_y_pred, other=x.pow(3)).sum()

# Update weights using gradient descent
a -= learning_rate * grad_a
Expand Down