import torch
# 建立張量並設定requires_grad=true來追蹤其計算歷史
x = torch.ones(2,
2, requires_grad=
true
)print
(x)
tensor([[1., 1.],
[1., 1.]], requires_grad=true)
y = x +
2print
(y)
tensor([[3., 3.],
[3., 3.]], grad_fn=)
print
(y.grad_fn)
z = y * y *
3out = z.mean(
)print
(z, out)
tensor([[27., 27.],
[27., 27.]], grad_fn=) tensor(27., grad_fn=)
pytorch會自動追蹤和記錄對於張量的所有操作,當計算完成後呼叫.backward()方法自動計算梯度並且將計算結果儲存到grad屬性中。在張量進行操作後,grad_fn已經被賦予了乙個新的函式,這個函式引用了乙個建立了這個tensor類的function物件。
# .requires_grad_(...)可以改變現有張量的requires_grad屬性,預設flag為false
a = torch.randn(2,
2)a =(
(a *3)
/(a -1)
)print
(a.requires_grad)
a.requires_grad_(
true
)print
(a.requires_grad)
b =(a * a)
.sum()
print
(b.grad_fn)
false
true
x1 = torch.ones(2,
2,requires_grad=
true
)z1 =3*
(x1 +2)
*(x1 +2)
out = z1.mean(
)# z1 = 3(x1+2)^2, out = mean(z1)
print
("x1 = "
, x1)
print
("z1 = "
, z1)
# out是乙個標量(scalar),out.backward()相當於out.backward(torch,tensor(1))
out.backward(
)# x.grad 梯度d(out)/dx
print
(x1.grad)
# 清零
x1.grad.data.zero_(
)
x1 = tensor([[1., 1.],
[1., 1.]], requires_grad=true)
z1 = tensor([[27., 27.],
[27., 27.]], grad_fn=)
tensor([[4.5000, 4.5000],
[4.5000, 4.5000]])
tensor([[0., 0.],
[0., 0.]])
x = torch.eye(2,
2, requires_grad=
true
)y = torch.ones(2,
2, requires_grad=
true
)z = x **
2+ y **
3print
("z ="
, z)
# 由於返回值不是乙個標量,因此需要輸入乙個大小相同的張量作為引數,
# 這裡我們用ones_like函式根據x生成乙個張量
gradient = torch.ones_like(x)
z.backward(gradient)
print
("gradient ="
, gradient)
print
("x.grad ="
, x.grad)
# x.grad = (dz/dx) .* gradient
print
("y.grad ="
, y.grad)
# y.grad = (dz/dy) .* gradient
z = tensor([[2., 1.],
[1., 2.]], grad_fn=)
gradient = tensor([[1., 1.],
[1., 1.]])
x.grad = tensor([[2., 0.],
[0., 2.]])
y.grad = tensor([[3., 3.],
[3., 3.]])
x = torch.randn(
3, requires_grad=
true
)y = x *
2while y.data.norm(
)<
1000
:# .data.norm() 張量l2範數
y = y *
2print
("x ="
, x)
print
("y ="
, y)
gradient = torch.tensor([1
,0.1
,0.01])
y.backward(gradient)
print
("gradient ="
, gradient)
print
("x.grad ="
, x.grad)
x = tensor([0.6344, 0.8409, 2.2294], requires_grad=true)
y = tensor([ 324.8004, 430.5210, 1141.4760], grad_fn=)
gradient = tensor([1.0000, 0.1000, 0.0100])
x.grad = tensor([512.0000, 51.2000, 5.1200])
print
(x.requires_grad)
print
((x **2)
.requires_grad)
# 將變數包裹在with torch.no_grad()中,可以暫時遮蔽autograd計算
with torch.no_grad():
print
((x **2)
.requires_grad)
print
((x **2)
.requires_grad)
true
true
false
true
總結一下整個自動求導過程:
1.當我們執行z.backward()的時候。這個操作將呼叫z裡面的grad_fn這個屬性,執行求導的操作。
2.這個操作將遍歷grad_fn的next_functions,然後分別取出裡面的function(accumulategrad),執行求導操作。這部分是乙個遞迴的過程直到最後型別為葉子節點。
3.計算出結果以後,將結果儲存到他們對應的variable 這個變數所引用的物件(x和y)的 grad這個屬性裡面。
4.求導結束。所有的葉節點的grad變數都得到了相應的更新
最終當我們執行完c.backward()之後,a和b裡面的grad值就得到了更新。 Autograd 自動求導
import torch 建立張量並設定requires grad true來追蹤其計算歷史 x torch.ones 2,2,requires grad true print x tensor 1.1.1.1.requires grad true y x 2print y tensor 3.3.3...
pytoch總結autograd自動求導02
1,pytorch中的自動求導機制 autograd模組 1 神經網路求導的核心包是autograd,使用時匯入import torch.autograd 2 定義tensor時格式如下 torch.tensor data,dtype none,device none,requires grad n...
Autograd 自動微分
1 深度學習的演算法本質上是通過反向傳播求導數,pytorch的autograd模組實現了此功能 在tensor上的所有操作,autograd都能為他們自動提供微分,避免手動計算導數的複雜過程。2 autograd.variable是autograd中的核心類,它簡單的封裝了tensor,並支援幾乎...