淺談對(duì)pytroch中torch.autograd.backward的思考
反向傳遞法則是深度學(xué)習(xí)中最為重要的一部分,torch中的backward可以對(duì)計(jì)算圖中的梯度進(jìn)行計(jì)算和累積
這里通過(guò)一段程序來(lái)演示基本的backward操作以及需要注意的地方
>>> import torch
>>> from torch.autograd import Variable
>>> x = Variable(torch.ones(2,2), requires_grad=True)
>>> y = x + 2
>>> y.grad_fn
Out[6]: <torch.autograd.function.AddConstantBackward at 0x229e7068138>
>>> y.grad
>>> z = y*y*3
>>> z.grad_fn
Out[9]: <torch.autograd.function.MulConstantBackward at 0x229e86cc5e8>
>>> z
Out[10]:
Variable containing:
27 27
27 27
[torch.FloatTensor of size 2x2]
>>> out = z.mean()
>>> out.grad_fn
Out[12]: <torch.autograd.function.MeanBackward at 0x229e86cc408>
>>> out.backward() # 這里因?yàn)閛ut為scalar標(biāo)量,所以參數(shù)不需要填寫
>>> x.grad
Out[19]:
Variable containing:
4.5000 4.5000
4.5000 4.5000
[torch.FloatTensor of size 2x2]
>>> out # out為標(biāo)量
Out[20]:
Variable containing:
27
[torch.FloatTensor of size 1]
>>> x = Variable(torch.Tensor([2,2,2]), requires_grad=True)
>>> y = x*2
>>> y
Out[52]:
Variable containing:
4
4
4
[torch.FloatTensor of size 3]
>>> y.backward() # 因?yàn)閥輸出為非標(biāo)量,求向量間元素的梯度需要對(duì)所求的元素進(jìn)行標(biāo)注,用相同長(zhǎng)度的序列進(jìn)行標(biāo)注
Traceback (most recent call last):
File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-53-95acac9c3254>", line 1, in <module>
y.backward()
File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\torch\autograd\variable.py", line 156, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph, retain_variables)
File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\torch\autograd\__init__.py", line 86, in backward
grad_variables, create_graph = _make_grads(variables, grad_variables, create_graph)
File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\torch\autograd\__init__.py", line 34, in _make_grads
raise RuntimeError("grad can be implicitly created only for scalar outputs")
RuntimeError: grad can be implicitly created only for scalar outputs
>>> y.backward(torch.FloatTensor([0.1, 1, 10]))
>>> x.grad #注意這里的0.1,1.10為梯度求值比例
Out[55]:
Variable containing:
0.2000
2.0000
20.0000
[torch.FloatTensor of size 3]
>>> y.backward(torch.FloatTensor([0.1, 1, 10]))
>>> x.grad # 梯度累積
Out[57]:
Variable containing:
0.4000
4.0000
40.0000
[torch.FloatTensor of size 3]
>>> x.grad.data.zero_() # 梯度累積進(jìn)行清零
Out[60]:
0
0
0
[torch.FloatTensor of size 3]
>>> x.grad # 累積為空
Out[61]:
Variable containing:
0
0
0
[torch.FloatTensor of size 3]
>>> y.backward(torch.FloatTensor([0.1, 1, 10]))
>>> x.grad
Out[63]:
Variable containing:
0.2000
2.0000
20.0000
[torch.FloatTensor of size 3]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
查看django執(zhí)行的sql語(yǔ)句及消耗時(shí)間的兩種方法
今天小編就為大家分享一篇查看django執(zhí)行的sql語(yǔ)句及消耗時(shí)間的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
python3學(xué)習(xí)筆記之多進(jìn)程分布式小例子
本篇文章主要介紹了python3學(xué)習(xí)筆記之多進(jìn)程分布式小例子,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
解決pycharm工程啟動(dòng)卡住沒(méi)反應(yīng)的問(wèn)題
今天小編就為大家分享一篇解決pycharm工程啟動(dòng)卡住沒(méi)反應(yīng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python3實(shí)現(xiàn)的回文數(shù)判斷及羅馬數(shù)字轉(zhuǎn)整數(shù)算法示例
這篇文章主要介紹了Python3實(shí)現(xiàn)的回文數(shù)判斷及羅馬數(shù)字轉(zhuǎn)整數(shù)算法,涉及Python數(shù)值運(yùn)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Python 實(shí)現(xiàn)簡(jiǎn)單的客戶端認(rèn)證
這篇文章主要介紹了Python 如何實(shí)現(xiàn)簡(jiǎn)單的客戶端認(rèn)證,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
Python實(shí)現(xiàn)動(dòng)態(tài)柱狀圖的繪制
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)動(dòng)態(tài)柱狀圖的繪制的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)與借鑒價(jià)值,需要的可以參考一下2022-12-12

