• pytorch: Variable detach 与 detach_


    pytorch 的 Variable 对象中有两个方法,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 的值也会改变
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # detach 的源码
    def detach(self):
    result = NoGrad()(self) # this is needed, because it merges version counters
    result._grad_fn = None
    return result
    1
    2
    3
    4
    5
    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
    1
    2
    3
    4
    5
    6
    7
    能用来干啥

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

    # 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()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    在这种情况下,detach 和 detach_ 都可以用。但是如果 你也想用 yy 来对 AA 进行 BP 呢?那就只能用第一种方法了。因为 第二种方法 已经将 AA 模型的输出 给 detach(分离)了。
    ---------------------
    作者:ke1th
    来源:CSDN
    原文:https://blog.csdn.net/u012436149/article/details/76714349
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    #4702. gcd
    独特的树叶

    搞笑的代码 ( funny )
    越野赛车问题
    删边(cip)
    最长公共子序列
    美食节
    线段树
    新年快乐
  • 原文地址:https://www.cnblogs.com/jfdwd/p/11069718.html
Copyright © 2020-2023  润新知