pytorch-detach与detach_

本文详细解析了PyTorch中的detach与detach_方法,解释了这两个方法如何从计算图中分离变量,避免梯度计算,以及在神经网络训练中如何使用它们来控制反向传播的流程。

detach


官方文档中,对这个方法是这么介绍的。

  • 返回一个新的 从当前图中分离的 Variable。
  • 返回的 Variable 永远不会需要梯度
  • 如果 被 detach 的Variable volatile=True, 那么 detach 出来的 volatile 也为 True
  • 还有一个注意事项,即:返回的 Variable 和 被 detach 的Variable 指向同一个 tensor
import torch
from torch.nn import init
from torch.autograd import Variable
t1 = torch.FloatTensor([1., 2.])
v1 = Variable(t1)
t2 = torch.FloatTensor([2., 3.])
v2 = Variable(t2)
v3 = v1 + v2
v3_detached = v3.detach()
v3_detached.data.add_(t1) # 修改了 v3_detached Variable中 tensor 的值
print(v3, v3_detached)    # v3 中tensor 的值也会改变

# detach 的源码:
 

def detach(self):
    result = NoGrad()(self)  # this is needed, because it merges version counters
    result._grad_fn = None
    return result


detach_


官网给的解释是:将 Variable 从创建它的 graph 中分离,把它作为叶子节点。

从源码中也可以看出这一点:

将 Variable 的grad_fn 设置为 None,这样,BP 的时候,到这个 Variable 就找不到 它的 grad_fn,所以就不会再往后BP了。
将 requires_grad 设置为 False。这个感觉大可不必,但是既然源码中这么写了,如果有需要梯度的话可以再手动 将 requires_grad 设置为 true。


# detach_ 的源码:

def detach_(self):
    """Detaches the Variable from the graph that created it, making it a
    leaf.
    """
    self._grad_fn = None
    self.requires_grad = False

能用来干啥


如果我们有两个网络 A,B, 两个关系是这样的 y=A(x),z=B(y)现在我们想用 z.backward() 来为 B网络的参数来求梯度,但是又不想求 A 网络参数的梯度。我们可以这样:

# y=A(x), z=B(y) 求B中参数的梯度,不求A中参数的梯度
# 第一种方法
y = A(x)
z = B(y.detach())
z.backward()

# 第二种方法
y = A(x)
y.detach_()
z = B(y)
z.backward()

在这种情况下,detach detach_ 都可以用。但是如果 你也想用 y 来对 A 进行 BP 呢?那就只能用第一种方法了。因为 第二种方法 已经将 A 模型的输出 给 detach(分离)了。
 

转载地址:https://blog.youkuaiyun.com/u012436149/article/details/76714349

### 实现GradCAM算法 为了在PyTorch中实现GradCAM,可以遵循特定的方法来提取卷积层的特征映射以及对应的梯度。这有助于生成热力图以可视化模型关注的区域。 首先定义一个辅助函数用于获取目标类别的得分相对于指定卷积层输出的梯度: ```python from torch.nn import functional as F def get_gradients(model, images, target_class, conv_layer_name): gradients = [] def save_gradient(grad): gradients.append(grad) handle = dict(model.named_modules())[conv_layer_name].register_backward_hook( lambda module, grad_input, grad_output: save_gradient(grad_output[0]) ) model.zero_grad() output = model(images) one_hot = F.one_hot(torch.tensor(target_class), num_classes=output.size()[-1]).float().to(output.device) score_for_target = (one_hot * output).sum() score_for_target.backward(retain_graph=True) handle.remove() return gradients[-1] ``` 接着创建另一个函数用来计算全局平均池化后的权重并将其应用于相应的特征映射上从而得到最终的热力图: ```python import numpy as np import cv2 def generate_heatmap(gradients, feature_maps): pooled_gradients = torch.mean(gradients, dim=[0, 2, 3]) # Global Average Pooling over spatial dimensions and batch for i in range(feature_maps.shape[1]): feature_maps[:, i, :, :] *= pooled_gradients[i] heatmap = torch.mean(feature_maps, dim=1).squeeze().detach().cpu().numpy() heatmap = np.maximum(heatmap, 0) # ReLU equivalent on the heatmap heatmap /= np.max(heatmap) # Normalize between 0 and 1 return heatmap ``` 最后编写一段代码片段调用上述两个函数完成整个过程,并将结果叠加到原始图片之上以便观察效果[^1]。 ```python image_path = 'path_to_image' target_conv_layer = 'layer4' # Example layer name from ResNet architecture; change accordingly. model.eval() original_img = load_and_preprocess(image_path) output = model(original_img.unsqueeze(0)) predicted_class = output.argmax(dim=-1).item() grads = get_gradients(model, original_img.unsqueeze(0), predicted_class, target_conv_layer) feature_map = extract_feature_map(model, original_img.unsqueeze(0), target_conv_layer) cam = generate_heatmap(grads, feature_map) # Resize CAM to match input image size cam_resized = cv2.resize(cam, (original_img.shape[-1], original_img.shape[-2])) # Overlay CAM on top of the original image overlayed_image = overlay_cam_on_image(load_original_image(image_path), cam_resized) display(overlayed_image) ``` 请注意,在实际应用时还需要根据具体的网络结构调整`target_conv_layer`参数值,并确保所有预处理步骤训练期间保持一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值