pytorch程式碼示例筆記 -- Autograd

風中塵埃發表於2020-12-30

這篇博文是我記錄學習pytorch的一些程式碼例子,我會時不時來看這些程式碼加深學習

例子來自https://pytorch.org/tutorials/beginner/pytorch_with_examples.html

import torch
import math

dtype  = torch.float
device = torch.device("cpu")

x = torch.linspace(-math.pi, math.pi, 2000,
                   dtype=dtype, device=device)
y = torch.sin(x)

a = torch.randn((), device=device, dtype=dtype, requires_grad=True)
b = torch.randn((), device=device, dtype=dtype, requires_grad=True)
c = torch.randn((), device=device, dtype=dtype, requires_grad=True)
d = torch.randn((), device=device, dtype=dtype, requires_grad=True)

learning_rate = 1e-6
for t in range(2000):
    y_pred = a + b * x + c * x ** 2 + d * x ** 3   #y預測
    loss = (y_pred - y).pow(2).sum()               #平方誤差
    if t % 100 == 99:
        print(t, loss.item())
        
    loss.backward()
    
    with torch.no_grad():
        a -= learning_rate * a.grad
        b -= learning_rate * b.grad
        c -= learning_rate * c.grad
        d -= learning_rate * d.grad
        
        a.grad = None
        b.grad = None
        c.grad = None
        d.grad = None

print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x ^ 3')
    

 

相關文章