python中backward_【PyTorch】聊聊 backward 背后的代码

本文详细探讨了PyTorch中`backward`函数的工作原理,从Tensor的`backward`方法到`torch.autograd.backward`,再到C++实现的`_ImperativeEngine.run_backward`。文章解释了反向传播过程中涉及的参数、内部机制,包括梯度计算、链式法则的运用,以及如何处理高阶微分和图的保留。此外,还介绍了`try_get_grad_accumulator`函数和`Engine::execute`在计算图执行中的作用,以及`pre_hooks`和`post_hooks`功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说起backward大家肯定不陌生,用过PyTorch的肯定都知道,这个函数的作用是反向传播计算梯度的。比如下边这个例子,要反向传播计算梯度之后,才能调用优化器的step函数更新网络模型参数。

Example:

>>> optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)

>>> optimizer.zero_grad()

>>> loss_fn(model(input), target).backward()

>>> optimizer.step()

[1] torch.Tensor.backward

在 torch/tensor.py 文件中可以看到,class Tensor(torch._C._TensorBase)中有函数def backward。所以我们可以用tensor.backward()来进行反向传播。

def backward(self, gradient=None, retain_graph=None, create_graph=False):

r"""Computes the gradient of current tensor w.r.t. graph leaves.The graph is differentiated using the chain rule. If the tensor isnon-scalar (i.e. its data has more than one element) and requiresgradient, the function additionally requires specifying ``gradient``.It should be a tensor of matching type and location, that containsthe gradient of the differentiated function w.r.t. ``self``.This function accumulates gradients in the leaves - you might need tozero them before calling it.Arguments:gradient (Tensor or None): Gradient w.r.t. thetensor. If it is a tensor, it will be automatically convertedto a Tensor that does not require grad unless ``create_graph`` is True.None values can be specified for scalar Tensors or ones thatdon't require grad. If a None value would be acceptable thenthis argument is optional.retain_graph (bool, optional): If ``False``, the graph used to computethe grads will be freed. Note that in nearly all cases settingthis option to True is not needed and often can be worked aroundin a much more efficient way. Defaults to the value of``create_graph``.create_graph (bool, optional): If ``True``, graph of the derivative willbe constructed, allowing to compute higher order derivativeproducts. Defaults to ``False``."""

torch.autograd.backward(self, gradient, retain_graph, create_graph)

其中,create_graph参数的作用是,如果为True,那么就创建一个专门的graph of the derivative,这可以方便计算高阶微分。参数retain_graph可以忽略,因为绝大多数情况根本不需要,它的作用是要不要保留Graph。该函数实现代码也很简单,就是调用torch.autograd.backward。所以接下来看一下torch.autograd.backward中的实现。

[2] torch.autograd.backward

函数torch.autograd.backward的定义在文件 torch/autograd/__init__.py 中。借助于链式法则the chain rule和Jacobian-vector product可以很方便的计算梯度。下边就是具体的代码。

# ...

from .variable import Variable

# ...

def _make_grads(outputs, grads):

new_grads = []

for out, grad in zip(outputs, grads):

if isinstance(grad, torch.Tensor):

if not out.shape == grad.shape:

# raise RuntimeError ...

new_grads.append(grad)

elif grad is None:

if out.requires_grad:

if out.numel() != 1:

# raise RuntimeError ...

else:

new_grads.append(None)

else:

# raise TypeError ...

return tuple(new_grads)

def backward(tensors, grad_tensors=None, retain_graph=None, create_graph=False, grad_variables=None):

r"""Computes the sum of gradients of given tensors w.r.t. graph leaves.The graph is differentiated using the chain rule. If any of ``tensors``are non-scalar (i.e. their data has more than one element) and requiregradient, then the Jacobian-vector product would be computed, in thiscase the function additionally requires specifying ``grad_tensors``.It should be a sequence of matching length, that contains the "vector"in the Jacobian-vector product, usually the gradient of the differentiatedfunction w.r.t. corresponding tensors (``None`` is an acceptable value forall tensors that don't need gradient tensors).This function accumulates gradients in the leaves - you might need to zerothem before calling it."""

if grad_variables is not None:

warnings.warn("'grad_variables' is deprecated. Use 'grad_tensors' instead.")

if grad_tensors is None:

grad_tensors = grad_variables

else:

raise RuntimeError("'grad_tensors' and 'grad_variables' (deprecated) "

"arguments both passed to backward(). Please only "

"use 'grad_tensors'.")

tensors = (tensors,) if isinstance(tensors, torch.Tensor) else tuple(tensors)

if grad_tensors is None:

grad_tensors = [None] * len(tensors)

elif isinstance(grad_tensors, torch.Tensor):

grad_tensors = [grad_tensors]

else:

grad_tensors = list(grad_tensors)

grad_tensors = _make_grads(tensors, grad_tensors)

if retain_graph is None:

retain_graph = create_graph

Variable._execution_engine.run_backward(

tensors, grad_tensors, retain_graph, create_graph,

allow_unreachable=True) # allow_unreachable flag

# ...

if not torch._C._autograd_init():

raise RuntimeError("autograd initialization failed")

参数grad_variables是老版本的,已经被deprecated,现在使用的是grad_tensors。即便你使用了也没关系,代码会把参数grad_variables的

GPLinker_pytorch是一个基于PyTorch实现的图神经网络模型,用于链接预测任务。以下是GPLinker_pytorch代码复现的步骤: ### 1. 环境准备 首先,确保你已经安装了必要的库和工具,包括PyTorch和相关的依赖项。 ```bash pip install torch torchvision ``` ### 2. 数据准备 准备你的数据集。假设你的数据集包含节点特征、边信息和标签。你需要将数据转换为PyTorch张量。 ```python import torch # 示例数据 node_features = torch.randn(num_nodes, feature_dim) edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]], dtype=torch.long) edge_attr = torch.randn(num_edges, edge_dim) labels = torch.tensor([0, 1, 1, 0], dtype=torch.long) ``` ### 3. 模型定义 定义GPLinker模型。GPLinker通常包含多个图卷积层和一个链接预测层。 ```python import torch.nn as nn import torch.nn.functional as F class GPLinker(nn.Module): def __init__(self, input_dim, hidden_dim, output_dim): super(GPLinker, self).__init__() self.conv1 = nn.Linear(input_dim, hidden_dim) self.conv2 = nn.Linear(hidden_dim, hidden_dim) self.link_pred = nn.Linear(hidden_dim, output_dim) def forward(self, x, edge_index): x = F.relu(self.conv1(x)) x = F.relu(self.conv2(x)) return self.link_pred(x) model = GPLinker(input_dim=feature_dim, hidden_dim=hidden_dim, output_dim=num_classes) ``` ### 4. 训练和评估 定义损失函数和优化器,并编写训练和评估循环。 ```python import torch.optim as optim criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=learning_rate) for epoch in range(num_epochs): model.train() optimizer.zero_grad() out = model(node_features, edge_index) loss = criterion(out, labels) loss.backward() optimizer.step() if epoch % 10 == 0: model.eval() with torch.no_grad(): out = model(node_features, edge_index) _, predicted = torch.max(out, dim=1) accuracy = (predicted == labels).sum().item() / labels.size(0) print(f'Epoch {epoch}, Loss: {loss.item()}, Accuracy: {accuracy}') ``` ### 5. 保存和加载模型 训练完成后,可以保存模型参数,并在需要时加载。 ```python # 保存模型 torch.save(model.state_dict(), 'gplinker_pytorch.pth') # 加载模型 model = GPLinker(input_dim=feature_dim, hidden_dim=hidden_dim, output_dim=num_classes) model.load_state_dict(torch.load('gplinker_pytorch.pth')) model.eval() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值