摘要
two_layer_net_autograd
"""
PyTorch:张量和autograd
-------------------------------
这是一个全连接的ReLU网络,只有一个隐层,没有任何偏置值,通过最小化欧氏距离的平方,训练它从x预测y。
该实现使用PyTorch张量上的操作计算正向传递,并使用PyTorch autograd计算梯度。
PyTorch张量表示计算图中的一个节点。如果x是一个张量,有requires_grad = True,那么x.grad是另一个张量,它包含“x”对某个标量值的梯度。
"""
import torch
dtype = torch.float
device = torch.device("cpu")
# device = torch.device("cuda:0") # Uncomment this to run on GPU
# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10
# Create random Tensors to hold input and outputs.
# Setting requires_grad=False indicates that we do not need to compute gradients
# with respect to these Tensors during the backward pass.
x = torch.randn(N, D_in, device=device, dtype=dtype)
y = torch.randn(N, D_out, device=device, dtype=dtype)
# Create random Tensors for weights.
# Setting requires_grad=True indicates that we want to compute gradients with
# respect to these Tensors during the backward pass.
w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True)
learning_rate = 1e-6
for t in range(500):
#前向传递:使用张量运算计算预测y;这些操作与我们使用张量计算前向传递时使用的操作完全相同,
#但是我们不需要保持对中间值的引用,因为我们没有手工实现后向传递。
y_pred = x.mm(w1).clamp(min=0).mm(w2)
# Compute and print loss using operations on Tensors.
# Now loss is a Tensor of shape (1,)
# loss.item() gets the a scalar value held in the loss.
loss = (y_pred - y).pow(2).sum()
print(t, loss.item())
#使用autograd计算向后传递。这个调用将用requires_grad=True计算所有张量的损失梯度。
#在这个call之后w1.grad和w2.grad将是张量,分别对w1和w2保持损失梯度。
loss.backward()
#使用梯度下降手动更新权重。包装在 torch.no_grad(),因为权重有requires_grad=True,
#但是我们不需要在autograd中跟踪它。另一种方法是对weight.data和weight.grad.data进行操作。
#回想一下tensor.data给出一个张量,它与张量共享存储空间,但不跟踪历史。
#你也可以使用torch.optim.SGD实现这一目标。
with torch.no_grad():
w1 -= learning_rate * w1.grad
w2 -= learning_rate * w2.grad
# Manually zero the gradients after updating weights
w1.grad.zero_()
w2.grad.zero_()