pytorch中的detach和detach_

pytorch 的 Variable 对象中有两个方法,detach和 detach_ :

detach

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

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

detach的源码:

1 # detach 的源码
2 def detach(self):
3     result = NoGrad()(self)  # this is needed, because it merges version counters
4     result._grad_fn = None
5     return result

detach_

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

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

  • 将 Variable 的grad_fn 设置为 None,这样,BP 的时候,到这个 Variable 就找不到 它的 grad_fn,所以就不会再往后BP了。
  • 将 requires_grad 设置为 False。这个感觉大可不必,但是既然源码中这么写了,如果有需要梯度的话可以再手动 将 requires_grad 设置为 true
1 # detach_ 的源码
2 def detach_(self):
3     """Detaches the Variable from the graph that created it, making it a
4     leaf.
5     """
6     self._grad_fn = None
7     self.requires_grad = False

能用来干啥

可以对部分网络求梯度。

如果我们有两个网络 , 两个关系是这样的 现在我们想用 来为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_ 都可以用。但是如果 你也想用 模型的输出给 detach(分离)了。

转自: https://blog.csdn.net/u012436149/article/details/76714349