Pytorch 深度学习实践 第3讲
# 小试牛刀
# import torch
# a = torch.tensor([1.0])
# a.requires_grad = True # 或者 a.requires_grad_()
# print(a) # tensor([1.], requires_grad=True)
# print(a.data) # tensor([1.])
# print(a.type()) # torch.FloatTensor
# print(a.data.type()) # torch.FloatTensor
# print(a.grad) # None
# print(type(a.grad)) # <class 'NoneType'>
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_()
def forward(x):
return x*w
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
print("predict (before training)", 4, forward(4).item())
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y)
l.backward()
print("\tgrad", x, y, w.grad.item())
w.data = w.data - 0.01 * w.grad.data
w.grad.data.zero_()
print('epoch:', epoch, l.item())
print('predict (after training)', 4, 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_()
w2 = torch.tensor([1.0])
w2.requires_grad_()
b = torch.tensor([1.0])
b.requires_grad_()
def forward(x):
return w1 * (x ** 2) + w2 * x + b
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
print("predict (before training)", 4, forward(4).item())
for epoch in range(10000):
for x, y in zip(x_data, y_data):
l = loss(x, y)
l.backward()
print('\tgrad:', x, y, w1.grad.item(), w2.grad.item(), b.grad.item())
w1.data = w1.data - 0.01 * w1.grad.data
w2.data = w2.data - 0.01 * w2.grad.data
b.data = b.data - 0.01 * b.grad.data
w1.grad.data.zero_()
w2.grad.data.zero_()
b.grad.data.zero_()
print('Epoch', epoch, l.item())
print('predict (after training)', 4, forward(4).item())