自动求导是深度学习中的核心概念之一。在 PyTorch 中,自动求导通过 Autograd 模块来实现,它允许我们自动计算导数。
基本概念
自动求导的核心思想是链式法则。它允许我们追踪计算过程中的每一步,并自动计算导数。
实例:计算函数的导数
假设我们有一个简单的函数 ( f(x) = x^2 ),我们可以使用 PyTorch 来计算它的导数。
import torch
# 定义变量
x = torch.tensor(2.0, requires_grad=True)
# 定义函数
f = x ** 2
# 计算导数
f.backward(torch.tensor(1.0))
print(x.grad) # 输出: tensor(4.0)
在上面的代码中,我们首先创建了一个需要梯度的变量 x
,然后定义了函数 f
。调用 f.backward()
会自动计算 f
对 x
的导数。
高级用法
自动求导不仅限于简单的函数,它还可以用于复杂的神经网络。
# 定义一个简单的神经网络
class SimpleNet(torch.nn.Module):
def __init__(self):
super(SimpleNet, self).__init__()
self.fc1 = torch.nn.Linear(10, 20)
self.relu = torch.nn.ReLU()
self.fc2 = torch.nn.Linear(20, 1)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
# 实例化网络
net = SimpleNet()
# 假设输入和目标
inputs = torch.randn(5, 10)
targets = torch.randn(5, 1)
# 计算损失
outputs = net(inputs)
loss = torch.nn.MSELoss()(outputs, targets)
# 反向传播
loss.backward()
# 输出梯度
for param in net.parameters():
print(param.grad)
在上面的代码中,我们定义了一个简单的神经网络,并使用它来计算一个损失函数的导数。
更多资源
想要了解更多关于 PyTorch 的自动求导,可以访问PyTorch 官方文档。
<center><img src="https://cloud-image.ullrai.com/q/Autograd_Condition/" alt="Autograd_Condition"/></center>