detach detach_ pytorch
pytorch中的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 的值也會改變
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
# detach_ 的源碼
def detach_(self):"""Detaches the Variable from the graph that created it, making it aleaf."""self._grad_fn = Noneself.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_ 都可以用。但是如果 你也想用 y
總結
以上是生活随笔為你收集整理的detach detach_ pytorch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 获取当前脚本目录路径问题汇总
- 下一篇: NLLLoss CrossEntropy