Pytorch 自动求导
深度学习的算法本质上是通过反向传播求导数,而Pytorch的autograd模块实现了此功能。在Tensor上的所有操作,autograd均能为它们提供自动微分
# 在创建张量的时候,可以通过指定requires_grad=True标识,进行自动求导,Pytorch会记录该张量的每一步操作历史,并自动计算
import torch
x = torch.rand(3, 3, requires_grad=True)
x
tensor([[0.0803, 0.9218, 0.3219],
[0.8003, 0.1912, 0.9332],
[0.6010, 0.2762, 0.0237]], requires_grad=True)
y = torch.rand(3, 3, requires_grad=True)
y
tensor([[0.1794, 0.3274, 0.1144],
[0.5815, 0.3099, 0.3854],
[0.0383, 0.7856, 0.2387]], requires_grad=True)
z = torch.sum(x + y)
z
tensor(7.1100, grad_fn=<SumBackward0>)
# 简单的自动求导
z.backward()
print(x.grad, y.grad)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]) tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
# 复杂的自动求导
x = torch.rand(3, 3, requires_grad=True)
y = torch.rand(3, 3, requires_grad=True)
z = y**2 + x**3
z
tensor([[0.2509, 1.5016, 0.7266],
[0.1246, 0.9339, 0.3272],
[1.0595, 0.4782, 0.0501]], grad_fn=<AddBackward0>)
z.backward(torch.ones_like(x))
x.grad
tensor([[0.8078, 2.2859, 2.1076],
[0.4714, 2.6892, 0.8068],
[2.2977, 0.2319, 0.0336]])
使用with torch.no_grad()禁止对已设置requires_grad=True的张量进行自动求导,一般应用在计算测试集准确率时
with torch.no_grad():
print((x + y * 2).requires_grad)
False
本文深入探讨了Pytorch的autograd模块,演示了如何利用它进行简单和复杂的自动求导过程,包括张量操作的历史记录和梯度计算,以及如何在测试集评估中禁用自动求导。
1万+

被折叠的 条评论
为什么被折叠?



