最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PyTorch 如何檢查模型梯度是否可導(dǎo)

 更新時(shí)間:2021年06月05日 11:44:43   作者:煙雨風(fēng)渡  
這篇文章主要介紹了PyTorch 檢查模型梯度是否可導(dǎo)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、PyTorch 檢查模型梯度是否可導(dǎo)

當(dāng)我們構(gòu)建復(fù)雜網(wǎng)絡(luò)模型或在模型中加入復(fù)雜操作時(shí),可能會(huì)需要驗(yàn)證該模型或操作是否可導(dǎo),即模型是否能夠優(yōu)化,在PyTorch框架下,我們可以使用torch.autograd.gradcheck函數(shù)來實(shí)現(xiàn)這一功能。

首先看一下官方文檔中關(guān)于該函數(shù)的介紹:

可以看到官方文檔中介紹了該函數(shù)基于何種方法,以及其參數(shù)列表,下面給出幾個(gè)例子介紹其使用方法,注意:

Tensor需要是雙精度浮點(diǎn)型且設(shè)置requires_grad = True

第一個(gè)例子:檢查某一操作是否可導(dǎo)

from torch.autograd import gradcheck
import torch
import torch.nn as nn
 
inputs = torch.randn((10, 5), requires_grad=True, dtype=torch.double)
linear = nn.Linear(5, 3)
linear = linear.double()
test = gradcheck(lambda x: linear(x), inputs)
print("Are the gradients correct: ", test)

輸出為:

Are the gradients correct: True

第二個(gè)例子:檢查某一網(wǎng)絡(luò)模型是否可導(dǎo)

from torch.autograd import gradcheck
import torch
import torch.nn as nn 
# 定義神經(jīng)網(wǎng)絡(luò)模型
class Net(nn.Module):
 
    def __init__(self):
        super(Net, self).__init__()
        self.net = nn.Sequential(
            nn.Linear(15, 30),
            nn.ReLU(),
            nn.Linear(30, 15),
            nn.ReLU(),
            nn.Linear(15, 1),
            nn.Sigmoid()
        )
 
    def forward(self, x):
        y = self.net(x)
        return y
 
net = Net()
net = net.double()
inputs = torch.randn((10, 15), requires_grad=True, dtype=torch.double)
test = gradcheck(net, inputs)
print("Are the gradients correct: ", test)

輸出為:

Are the gradients correct: True

二、Pytorch求導(dǎo)

1.標(biāo)量對(duì)矩陣求導(dǎo)

在這里插入圖片描述

驗(yàn)證:

>>>import torch
>>>a = torch.tensor([[1],[2],[3.],[4]])    # 4*1列向量
>>>X = torch.tensor([[1,2,3],[5,6,7],[8,9,10],[5,4,3.]],requires_grad=True)  #4*3矩陣,注意,值必須要是float類型
>>>b = torch.tensor([[2],[3],[4.]]) #3*1列向量
>>>f = a.view(1,-1).mm(X).mm(b)  # f = a^T.dot(X).dot(b)
>>>f.backward()
>>>X.grad   #df/dX = a.dot(b^T)
tensor([[ 2.,  3.,  4.],
    [ 4.,  6.,  8.],
    [ 6.,  9., 12.],
    [ 8., 12., 16.]])
>>>a.grad b.grad   # a和b的requires_grad都為默認(rèn)(默認(rèn)為False),所以求導(dǎo)時(shí),沒有梯度
(None, None)
>>>a.mm(b.view(1,-1))  # a.dot(b^T)
    tensor([[ 2.,  3.,  4.],
    [ 4.,  6.,  8.],
    [ 6.,  9., 12.],
    [ 8., 12., 16.]])

2.矩陣對(duì)矩陣求導(dǎo)

在這里插入圖片描述 在這里插入圖片描述

驗(yàn)證:

>>>A = torch.tensor([[1,2],[3,4.]])  #2*2矩陣
>>>X =  torch.tensor([[1,2,3],[4,5.,6]],requires_grad=True)  # 2*3矩陣
>>>F = A.mm(X)
>>>F
tensor([[ 9., 12., 15.],
    [19., 26., 33.]], grad_fn=<MmBackward>)
>>>F.backgrad(torch.ones_like(F)) # 注意括號(hào)里要加上這句
>>>X.grad
tensor([[4., 4., 4.],
    [6., 6., 6.]])

注意:

requires_grad為True的數(shù)組必須是float類型

進(jìn)行backgrad的必須是標(biāo)量,如果是向量,必須在后面括號(hào)里加上torch.ones_like(X)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

河曲县| 宾阳县| 彩票| 诏安县| 华蓥市| 遵义市| 井冈山市| 平原县| 克山县| 龙陵县| 神农架林区| 桦南县| 白银市| 新宁县| 盈江县| 彭泽县| 兴安盟| 黄骅市| 晋州市| 巴林左旗| 聊城市| 东辽县| 公主岭市| 崇左市| 灵川县| 淮北市| 永年县| 八宿县| 永修县| 三原县| 海口市| 徐州市| 芷江| 聊城市| 鄱阳县| 鄂伦春自治旗| 乌兰察布市| 台南县| 波密县| 万安县| 读书|