欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch .detach() .detach_() 和 .data用于切斷反向傳播的實現(xiàn)

 更新時間:2019年12月27日 14:36:44   作者:慢行厚積  
這篇文章主要介紹了pytorch .detach() .detach_() 和 .data用于切斷反向傳播的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

當我們再訓練網(wǎng)絡的時候可能希望保持一部分的網(wǎng)絡參數(shù)不變,只對其中一部分的參數(shù)進行調整;或者值訓練部分分支網(wǎng)絡,并不讓其梯度對主網(wǎng)絡的梯度造成影響,這時候我們就需要使用detach()函數(shù)來切斷一些分支的反向傳播

1   detach()[source]

返回一個新的Variable,從當前計算圖中分離下來的,但是仍指向原變量的存放位置,不同之處只是requires_grad為false,得到的這個Variable永遠不需要計算其梯度,不具有grad。

即使之后重新將它的requires_grad置為true,它也不會具有梯度grad

這樣我們就會繼續(xù)使用這個新的Variable進行計算,后面當我們進行反向傳播時,到該調用detach()的Variable就會停止,不能再繼續(xù)向前進行傳播

源碼為:

def detach(self):
    """Returns a new Variable, detached from the current graph.
    Result will never require gradient. If the input is volatile, the output
    will be volatile too.
    .. note::
     Returned Variable uses the same data tensor, as the original one, and
     in-place modifications on either of them will be seen, and may trigger
     errors in correctness checks.
    """
    result = NoGrad()(self) # this is needed, because it merges version counters
    result._grad_fn = None
     return result

可見函數(shù)進行的操作有:

  • 將grad_fn設置為None
  • 將Variable的requires_grad設置為False

如果輸入 volatile=True(即不需要保存記錄,當只需要結果而不需要更新參數(shù)時這么設置來加快運算速度),那么返回的Variable volatile=True。(volatile已經棄用)

注意:

返回的Variable和原始的Variable公用同一個data tensor。in-place函數(shù)修改會在兩個Variable上同時體現(xiàn)(因為它們共享data tensor),當要對其調用backward()時可能會導致錯誤。

舉例:

比如正常的例子是:

import torch

a = torch.tensor([1, 2, 3.], requires_grad=True)
print(a.grad)
out = a.sigmoid()

out.sum().backward()
print(a.grad)

返回:

(deeplearning) userdeMBP:pytorch user$ python test.py
None
tensor([0.1966, 0.1050, 0.0452])

當使用detach()但是沒有進行更改時,并不會影響backward():

import torch

a = torch.tensor([1, 2, 3.], requires_grad=True)
print(a.grad)
out = a.sigmoid()
print(out)

#添加detach(),c的requires_grad為False
c = out.detach()
print(c)

#這時候沒有對c進行更改,所以并不會影響backward()
out.sum().backward()
print(a.grad)

返回:

(deeplearning) userdeMBP:pytorch user$ python test.py
None
tensor([0.7311, 0.8808, 0.9526], grad_fn=<SigmoidBackward>)
tensor([0.7311, 0.8808, 0.9526])
tensor([0.1966, 0.1050, 0.0452])

可見c,out之間的區(qū)別是c是沒有梯度的,out是有梯度的

如果這里使用的是c進行sum()操作并進行backward(),則會報錯:

import torch

a = torch.tensor([1, 2, 3.], requires_grad=True)
print(a.grad)
out = a.sigmoid()
print(out)

#添加detach(),c的requires_grad為False
c = out.detach()
print(c)

#使用新生成的Variable進行反向傳播
c.sum().backward()
print(a.grad)

返回:

(deeplearning) userdeMBP:pytorch user$ python test.py
None
tensor([0.7311, 0.8808, 0.9526], grad_fn=<SigmoidBackward>)
tensor([0.7311, 0.8808, 0.9526])
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    c.sum().backward()
  File "/anaconda3/envs/deeplearning/lib/python3.6/site-packages/torch/tensor.py", line 102, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/anaconda3/envs/deeplearning/lib/python3.6/site-packages/torch/autograd/__init__.py", line 90, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

如果此時對c進行了更改,這個更改會被autograd追蹤,在對out.sum()進行backward()時也會報錯,因為此時的值進行backward()得到的梯度是錯誤的:

import torch

a = torch.tensor([1, 2, 3.], requires_grad=True)
print(a.grad)
out = a.sigmoid()
print(out)

#添加detach(),c的requires_grad為False
c = out.detach()
print(c)
c.zero_() #使用in place函數(shù)對其進行修改

#會發(fā)現(xiàn)c的修改同時會影響out的值
print(c)
print(out)

#這時候對c進行更改,所以會影響backward(),這時候就不能進行backward(),會報錯
out.sum().backward()
print(a.grad)

返回:

(deeplearning) userdeMBP:pytorch user$ python test.py
None
tensor([0.7311, 0.8808, 0.9526], grad_fn=<SigmoidBackward>)
tensor([0.7311, 0.8808, 0.9526])
tensor([0., 0., 0.])
tensor([0., 0., 0.], grad_fn=<SigmoidBackward>)
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    out.sum().backward()
  File "/anaconda3/envs/deeplearning/lib/python3.6/site-packages/torch/tensor.py", line 102, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/anaconda3/envs/deeplearning/lib/python3.6/site-packages/torch/autograd/__init__.py", line 90, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

2   .data

如果上面的操作使用的是.data,效果會不同:

這里的不同在于.data的修改不會被autograd追蹤,這樣當進行backward()時它不會報錯,回得到一個錯誤的backward值

import torch

a = torch.tensor([1, 2, 3.], requires_grad=True)
print(a.grad)
out = a.sigmoid()
print(out)


c = out.data
print(c)
c.zero_() #使用in place函數(shù)對其進行修改

#會發(fā)現(xiàn)c的修改同時也會影響out的值
print(c)
print(out)

#這里的不同在于.data的修改不會被autograd追蹤,這樣當進行backward()時它不會報錯,回得到一個錯誤的backward值
out.sum().backward()
print(a.grad)

返回:

(deeplearning) userdeMBP:pytorch user$ python test.py
None
tensor([0.7311, 0.8808, 0.9526], grad_fn=<SigmoidBackward>)
tensor([0.7311, 0.8808, 0.9526])
tensor([0., 0., 0.])
tensor([0., 0., 0.], grad_fn=<SigmoidBackward>)
tensor([0., 0., 0.])

上面的內容實現(xiàn)的原理是:

In-place 正確性檢查

所有的Variable都會記錄用在他們身上的 in-place operations。如果pytorch檢測到variable在一個Function中已經被保存用來backward,但是之后它又被in-place operations修改。當這種情況發(fā)生時,在backward的時候,pytorch就會報錯。這種機制保證了,如果你用了in-place operations,但是在backward過程中沒有報錯,那么梯度的計算就是正確的。

⚠️下面結果正確是因為改變的是sum()的結果,中間值a.sigmoid()并沒有被影響,所以其對求梯度并沒有影響:

import torch

a = torch.tensor([1, 2, 3.], requires_grad=True)
print(a.grad)
out = a.sigmoid().sum() #但是如果sum寫在這里,而不是寫在backward()前,得到的結果是正確的
print(out)


c = out.data
print(c)
c.zero_() #使用in place函數(shù)對其進行修改

#會發(fā)現(xiàn)c的修改同時也會影響out的值
print(c)
print(out)

#沒有寫在這里
out.backward()
print(a.grad)

返回:

(deeplearning) userdeMBP:pytorch user$ python test.py
None
tensor(2.5644, grad_fn=<SumBackward0>)
tensor(2.5644)
tensor(0.)
tensor(0., grad_fn=<SumBackward0>)
tensor([0.1966, 0.1050, 0.0452])

3   detach_()[source]

將一個Variable從創(chuàng)建它的圖中分離,并把它設置成葉子variable

其實就相當于變量之間的關系本來是x -> m -> y,這里的葉子variable是x,但是這個時候對m進行了.detach_()操作,其實就是進行了兩個操作:

  • 將m的grad_fn的值設置為None,這樣m就不會再與前一個節(jié)點x關聯(lián),這里的關系就會變成x, m -> y,此時的m就變成了葉子結點
  • 然后會將m的requires_grad設置為False,這樣對y進行backward()時就不會求m的梯度

這么一看其實detach()和detach_()很像,兩個的區(qū)別就是detach_()是對本身的更改,detach()則是生成了一個新的variable

比如x -> m -> y中如果對m進行detach(),后面如果反悔想還是對原來的計算圖進行操作還是可以的

但是如果是進行了detach_(),那么原來的計算圖也發(fā)生了變化,就不能反悔了

參考:https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-autograd/#detachsource

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解Python中四種關系圖數(shù)據(jù)可視化的效果對比

    詳解Python中四種關系圖數(shù)據(jù)可視化的效果對比

    python關系圖的可視化主要就是用來分析一堆數(shù)據(jù)中,每一條數(shù)據(jù)的節(jié)點之間的連接關系從而更好的分析出人物或其他場景中存在的關聯(lián)關系。本文將制作四個不同的關系圖的可視化效果,感興趣的可以了解一下
    2022-11-11
  • 創(chuàng)建虛擬環(huán)境打包py文件的實現(xiàn)步驟

    創(chuàng)建虛擬環(huán)境打包py文件的實現(xiàn)步驟

    使用虛擬環(huán)境,可以為每個項目創(chuàng)建一個獨立的Python環(huán)境,每個環(huán)境都有自己的庫和版本,從而避免了依賴沖突,本文主要介紹了創(chuàng)建虛擬環(huán)境打包py文件的實現(xiàn)步驟,感興趣的可以了解一下
    2024-04-04
  • 基于python二叉樹的構造和打印例子

    基于python二叉樹的構造和打印例子

    今天小編就為大家分享一篇基于python二叉樹的構造和打印例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Linux系統(tǒng)(CentOS)下python2.7.10安裝

    Linux系統(tǒng)(CentOS)下python2.7.10安裝

    這篇文章主要為大家詳細介紹了Linux系統(tǒng)(CentOS)下python2.7.10安裝圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • python3 中使用urllib問題以及urllib詳解

    python3 中使用urllib問題以及urllib詳解

    這篇文章主要介紹了python3 中使用urllib問題以及urllib詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • Python matplotlib繪制實時數(shù)據(jù)動畫

    Python matplotlib繪制實時數(shù)據(jù)動畫

    Matplotlib作為Python的2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環(huán)境生成出版質量級別的圖形。本文將利用Matplotlib庫繪制實時數(shù)據(jù)動畫,感興趣的可以了解一下
    2022-03-03
  • python安裝cx_Oracle和wxPython的方法

    python安裝cx_Oracle和wxPython的方法

    這篇文章主要介紹了python安裝cx_Oracle和wxPython的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • python網(wǎng)絡爬蟲 CrawlSpider使用詳解

    python網(wǎng)絡爬蟲 CrawlSpider使用詳解

    這篇文章主要介紹了python網(wǎng)絡爬蟲 CrawlSpider使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • Tensorflow 實現(xiàn)將圖像與標簽數(shù)據(jù)轉化為tfRecord文件

    Tensorflow 實現(xiàn)將圖像與標簽數(shù)據(jù)轉化為tfRecord文件

    今天小編就為大家分享一篇Tensorflow 實現(xiàn)將圖像與標簽數(shù)據(jù)轉化為tfRecord文件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python將文本去空格并保存到txt文件中的實例

    Python將文本去空格并保存到txt文件中的實例

    今天小編就為大家分享一篇Python將文本去空格并保存到txt文件中的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評論