Pytorch获取中间变量的梯度grad

      为了节约显存,pytorch在反向传播的过程中只保留了计算图中的叶子结点的梯度值,而未保留中间节点的梯度

import torch
 
x = torch.tensor(3., requires_grad=True)
y = x ** 2
z = 4 * y
 
z.backward()
print(x.grad)   # tensor(24.)
print(y.grad)   # None

可以看到当进行反向传播后,只保留了x的梯度tensor(24.),而y的梯度没有保留所以为None。

但有时我们需要得到模型中间变量的梯度(如绘制Grad-CAM图时),怎么办呢。

有2种方法

①torch.autograd.grad(outputs, inputs)

import torch
import torch.autograd as autograd
 
x = torch.tensor(3., requires_grad=True)
y = x ** 2
z = 4 * y
 
x_grad = autograd.grad(z, x, retain_graph=True)[0]
y_grad = autograd.grad(z, y, retain_graph=True)[0]
print(x_grad)   # tensor(24.)
print(y_grad)   # tensor(4.)

可以看到此时x和y的梯度都可以获得,使用此方法时不用执行.backward()。

②torch.Tensor.register_hook()

import torch
 
x = torch.tensor(3., requires_grad=True)
y = x ** 2
z = 4 * y
features_grad = 0.
 
 
# 为了读取模型中间参数变量的梯度而定义的辅助函数
def extract(g):
    global features_grad
    features_grad = g
 
 
y.register_hook(extract)
z.backward()
y_grad = features_grad
 
print(x.grad)   # tensor(24.)
print(y_grad)   # tensor(4.)

在执行反向传播之前,对需要求梯度的中间变量执行.register_hook(),便可获得该中间变量的梯度值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值