🔑 requires_grad属性
import torch
x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
print(x.requires_grad)
y = x * 2
z = y.mean()
print(z.requires_grad)
with torch.no_grad():
w = x * 3
print(w.requires_grad)
detached = x.detach()
print(detached.requires_grad)
⚡ backward()方法详解
x = torch.randn(3, requires_grad=True)
y = x.sum()
y.backward()
print(x.grad)
x = torch.randn(3, requires_grad=True)
y = x * 2
y.backward(torch.tensor([1.0, 1.0, 1.0]))
print(x.grad)
x = torch.tensor([1.0], requires_grad=True)
y = x * 2
y.backward()
print(x.grad)
z = x * 3
z.backward()
print(x.grad)
x.grad.zero_()
🧮 实战示例:线性回归
import torch
x_train = torch.tensor([[1.0], [2.0], [3.0], [4.0]])
y_train = torch.tensor([[2.0], [4.0], [6.0], [8.0]])
W = torch.randn(1, 1, requires_grad=True)
b = torch.randn(1, requires_grad=True)
learning_rate = 0.01
epochs = 1000
for epoch in range(epochs):
y_pred = x_train @ W + b
loss = ((y_pred - y_train) ** 2).mean()
loss.backward()
with torch.no_grad():
W -= learning_rate * W.grad
b -= learning_rate * b.grad
W.grad.zero_()
b.grad.zero_()
print(f'W = {W.item():.4f}, b = {b.item():.4f}')