Pytorch學(xué)習(xí)筆記(二)


autograde

  • requires_grad 屬性:設(shè)置屬性為true則跟蹤該張量上的運(yùn)算
  • requires_grad_() 函數(shù):設(shè)置requires_grad屬性
  • grad 屬性:梯度值
  • backward() 函數(shù) : 計(jì)算梯度
  • with torch.no_grad() 函數(shù): 在該代碼塊中不跟蹤張量
  • detach() 函數(shù): 撤回跟蹤張量設(shè)置
  • grad_fn 屬性:創(chuàng)建張量的函數(shù)

例1

x=torch.ones(2,2)
y=x+2
print(x)
print(y)

x=torch.ones(2,2,requires_grad=True)
y=x+2
print(x)
print(y)
print(y.grad_fn)

output:

tensor([[1., 1.],
        [1., 1.]])
tensor([[3., 3.],
        [3., 3.]])
tensor([[1., 1.],
        [1., 1.]], requires_grad=True)
tensor([[3., 3.],
        [3., 3.]], grad_fn=<AddBackward0>)
<AddBackward0 object at 0x0000027286D72B38>

例2

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)

output:

False
True
<SumBackward0 object at 0x0000023D90762B00>

例3

x=torch.ones(2,2,requires_grad=True)
y=x+2
z=y*y*3
out=z.mean()
print(out)
out.backward()
print(x.grad)

output:

tensor(27., grad_fn=<MeanBackward0>)
tensor([[4.5000, 4.5000],
        [4.5000, 4.5000]])

例4

x=torch.randn(3,requires_grad=True)
y=x*2
v=torch.tensor([0.1,1.0,0.0001],dtype=torch.float) #尺度與y一致
y.backward(v)
print(x.grad)

output:

tensor([2.0000e-01, 2.0000e+00, 2.0000e-04])

例5

x=torch.randn(2,2,requires_grad=True)
print(x.requires_grad)
print((x**2).requires_grad)
with torch.no_grad():
    print((x**2).requires_grad)

output:

True
True
False

參考資料

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容