pytorch:nn與nn.functional的區(qū)別

1.兩者的調(diào)用方式不同
調(diào)用nn.xxx時(shí)要先在里面?zhèn)魅氤瑓?shù),然后再將數(shù)據(jù)以函數(shù)調(diào)用的方式輸進(jìn)nn.xxx里,例如:

inputs = torch.rand(64, 3, 244, 244)
conv = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1)
out = conv(inputs)

而nn.functional.xxx則要同時(shí)輸入數(shù)據(jù)和weight,bias等參數(shù),例如:

weight = torch.rand(64,3,3,3)
bias = torch.rand(64) 
out = nn.functional.conv2d(inputs, weight, bias, padding=1)

2.nn.xxx能夠放在nn.Sequential里,而nn.functional.xxx就不行
3.nn.functional.xxx需要自己定義weight,每次調(diào)用時(shí)都需要手動(dòng)傳入weight,而nn.xxx則不用,例如:
使用nn.xxx定義一個(gè)cnn:

class CNN(nn.Moudle)
    def __init__(self):
        super(CNN, self).__init__()
        
        self.cnn1 = nn.Conv2d(in_channels=1,  out_channels=16, kernel_size=5,padding=0)
        self.relu1 = nn.ReLU()
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)
        
        self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5,  padding=0)
        self.relu2 = nn.ReLU()
        self.maxpool2 = nn.MaxPool2d(kernel_size=2)
        
        self.linear1 = nn.Linear(4 * 4 * 32, 10)
        
    def forward(self, x):
        x = x.view(x.size(0), -1)
        out = self.maxpool1(self.relu1(self.cnn1(x)))
        out = self.maxpool2(self.relu2(self.cnn2(out)))
        out = self.linear1(out.view(x.size(0), -1))
        return out

使用nn.functional.xxx定義一個(gè)與上面相同的cnn:

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        
        self.cnn1_weight = nn.Parameter(torch.rand(16, 1, 5, 5))
        self.bias1_weight = nn.Parameter(torch.rand(16))
        
        self.cnn2_weight = nn.Parameter(torch.rand(32, 16, 5, 5))
        self.bias2_weight = nn.Parameter(torch.rand(32))
        
        self.linear1_weight = nn.Parameter(torch.rand(4 * 4 * 32, 10))
        self.bias3_weight = nn.Parameter(torch.rand(10))
        
    def forward(self, x):
        x = x.view(x.size(0), -1)
        out = F.conv2d(x, self.cnn1_weight, self.bias1_weight)
        out = F.conv2d(x, self.cnn2_weight, self.bias2_weight)
        out = F.linear(x, self.linear1_weight, self.bias3_weight)
        return out

4.關(guān)于dropout,推薦使用nn.xxx。因?yàn)橐话闱闆r下只有訓(xùn)練時(shí)才用dropout,在eval不需要dropout。使用nn.Dropout,在調(diào)用model.eval()后,模型的dropout層都關(guān)閉,但用nn.functional.dropout,在調(diào)用model.eval()后不會(huì)關(guān)閉dropout.

5.有一種情況用nn.functional.xxx會(huì)更好,即如果行為相同,但參數(shù)矩陣相同的兩個(gè)layer共享參數(shù),可直接多次調(diào)用nn的Module。但若行為不同,比如想讓兩個(gè)dilation不同但kernel相同的卷積層共享參數(shù),就可用到nn.functional,例如:

import torch
import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

        self.weight = nn.Parameter(torch.Tensor(10,10,3,3))
    
    def forward(self, x):
        x_1 = F.conv2d(x, self.weight,dilation=1, padding=1)
        x_2 = F.conv2d(x, self.weight,dilation=2, padding=2)
        return x_1 + x_2

建議:在構(gòu)建模型框架時(shí)(nn.Module)可用nn.xxx,在訓(xùn)練模型時(shí)可用nn.functional.xxx。另外,如果涉及到參數(shù)計(jì)算的,那用nn.;若不需要涉及更新參數(shù),只是一次性計(jì)算,那用nn.functional。

?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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