课堂代码
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w = torch.tensor([1.0])
w.requires_grad = True
def forward(x):
return w * x
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y)
l.backward()
print("x:", x, "y:", y, "grad:", w.grad.item())
w.data -= 0.01 * w.grad.data
w.grad.data.zero_() # after update, remember set the grad to zero
print("epoch:", epoch, "loss:", l.item())
print("when x = 4, y = ", forward(4).item())
课后作业
实现该函数的反向传播过程,并求梯度
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w1 = torch.tensor([1.0])
w1.requires_grad = True
w2 = torch.tensor([1.0])
w2.requires_grad = True
b = torch.tensor([1.0])
b.requires_grad = True
def forward(x):
return w1 * x**2 + w2 * x + b
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y)
l.backward()
print("x:", x, "y:", y, "grad:", w1.grad.item(), w2.grad.item(), b.grad.item())
w1.data -= 0.01 * w1.grad.data
w2.data -= 0.01 * w2.grad.data
b.data -= 0.01 * b.grad.data
w1.grad.data.zero_()
w2.grad.data.zero_()
b.grad.data.zero_()
print("epoch:", epoch, "loss:", l.item())
print("when x = 4, y = ", forward(4).item())
注:结果差距较大应该是模型与数据不匹配导致的。