pytorch.data属性和.detach属性相同与不同之处

说一下.data和.detach吧
a = torch.tensor([1.,2,3], requires_grad = True)
print(a)
b = a.data
print(b)
c = a.detach()
print(c)

b *= 5
print(a)
print(b)

'''
tensor([1., 2., 3.], requires_grad=True)
tensor([1., 2., 3.])
tensor([1., 2., 3.])
tensor([ 5., 10., 15.], requires_grad=True)
tensor([ 5., 10., 15.])'''

.data 和.detach只取出本体tensor数据,舍弃了grad,grad_fn等额外反向图计算过程需保存的额外信息。

在版本0.4.0后加入.detach,并推荐使用.detach,

  1. .detach

    >>> a = torch.tensor([1,2,3.], requires_grad = True)
    >>> out = a.sigmoid()
    >>> c = out.detach()
    >>> c.zero_()  
    tensor([ 0.,  0.,  0.])
    
    >>> out  # modified by c.zero_() !!
    tensor([ 0.,  0.,  0.])
    
    >>> out.sum().backward()  # Requires the original value of out, but that was overwritten by c.zero_()
    RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
    
  2. .deta

    >>> a = torch.tensor([1,2,3.], requires_grad = True)
    >>> out = a.sigmoid()
    >>> c = out.data
    >>> c.zero_()
    tensor([ 0.,  0.,  0.])
    
    >>> out  # out  was modified by c.zero_()
    tensor([ 0.,  0.,  0.])
    
    >>> out.sum().backward()
    >>> a.grad  # The result is very, very wrong because `out` changed!
    tensor([ 0.,  0.,  0.])
    

简单的说就是,.data取出本体tensor后仍与原数据共享内存(从第一个代码段中可以看出),在使用in-place操作后,会修改原数据的值,而如果在反向传播过程中使用到原数据会导致计算错误,而使用.detach后,如果在反向传播过程中发现原数据被修改过会报错。更加安全

你可能感兴趣的:(pytorch,记录)