PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx
PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx
在寫 PyTorch 代碼時(shí),我們會(huì)發(fā)現(xiàn)在 torch.nn.xxx 和 torch.nn.functional.xxx 中有一些功能重復(fù)的操作,比如卷積、激活、池化。這些操作有什么不同?各有什么用處?
首先可以觀察源碼:
eg:torch.nn.Conv2d
CLASS torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros')
eg:torch.nn.functional
torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) → Tensor
從中,我們可以發(fā)現(xiàn),nn.Conv2d 是一個(gè)類,而 nn.functional.conv2d是一個(gè)函數(shù)。
換言之:
- nn.Module 實(shí)現(xiàn)的 layer 是由 class Layer(nn.Module) 定義的特殊類
- nn.functional 中的函數(shù)更像是純函數(shù),由 def function(input) 定義
此外:
兩者的調(diào)用方式不同:調(diào)用 nn.xxx 時(shí)要先在里面?zhèn)魅氤瑓?shù),然后再將數(shù)據(jù)以函數(shù)調(diào)用的方式傳入 nn.xxx
# torch.nn inputs = torch.randn(64, 3, 244, 244) self.conv = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1) outputs = self.conv(inputs)# torch.nn.functional 需要同時(shí)傳入數(shù)據(jù)和 weight,bias等參數(shù) inputs = torch.randn(64, 3, 244, 244) weight = torch.randn(64, 3, 3, 3) bias = torch.randn(64) outputs = nn.functinoal.conv2d(inputs, weight, bias, padding=1)nn.xxx 能夠放在 nn.Sequential里,而 nn.functional.xxx 就不行
nn.functional.xxx 需要自己定義 weight,每次調(diào)用時(shí)都需要手動(dòng)傳入 weight,而 nn.xxx 則不用
import torch import torch.nn as nn import torch.nn.functional as F# torch.nn 定義的CNN class CNN(nn.Module):def __init__(self):super(CNN, self).__init__()self.conv_1 = nn.Conv2d(1, 16, krenel_size=5, padding=0)self.relu_1 = nn.ReLU(inplace=True)self.maxpool_1 = nn.MaxPool2d(kernel_size=2)self.conv_2 = nn.Conv2d(16, 32, krenel_size=5, padding=0)self.relu_2 = nn.ReLU(inplace=True)self.maxpool_2 = nn.MaxPool2d(kernel_size=2) self.linear = nn.Linear(4*4*32, 10)def forward(self, x):x = x.view(x.size(0), -1)out = self.maxpool_1(self.relu_1(self.conv_1(x)))out = self.maxpool_2(self.relu_2(self.conv_2(out)))out = self.linear(out.view(x.size(0), -1))return out# torch.nn.functional 定義一個(gè)相同的CNN class CNN(nn.Module):def __init__(self):super(CNN, self).__init__()self.conv_1_weight = nn.Parameter(torch.randn(16, 1, 5, 5))self.bias_1_weight = nn.Parameter(torch.randn(16))self.conv_2_weight = nn.Parameter(torch.randn(32, 16, 5, 5))self.bias_2_weight = nn.Parameter(torch.randn(32))self.linear_weight = nn.Parameter(torch.randn(4 * 4 * 32, 10))self.bias_weight = nn.Parameter(torch.randn(10))def forward(self, x):x = x.view(x.size(0), -1)out = F.conv2d(x, self.conv_1_weight, self.bias_1_weight)out = F.conv2d(out, self.conv_2_weight, self.bias_2_weight)out = F.linear(out.view(x.size(0), -1), self.linear_weight, self.bias_weight)在使用Dropout時(shí),推薦使用 nn.xxx。因?yàn)橐话阒挥杏?xùn)練時(shí)才使用 Dropout,在驗(yàn)證或測(cè)試時(shí)不需要使用 Dropout。使用 nn.Dropout時(shí),如果調(diào)用 model.eval() ,模型的 Dropout 層都會(huì)關(guān)閉;但如果使用 nn.functional.dropout,在調(diào)用 model.eval() 時(shí),不會(huì)關(guān)閉 Dropout。
當(dāng)我們想要自定義卷積核時(shí),是不能使用torch.nn.ConvNd 的,因?yàn)樗锩娴臋?quán)重都是需要學(xué)習(xí)的參數(shù),沒(méi)有辦法自行定義。但是,我們可以使用 torch.nn.functional.conv2d() 。
References:
轉(zhuǎn)載于:https://www.cnblogs.com/xxxxxxxxx/p/11475419.html
總結(jié)
以上是生活随笔為你收集整理的PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: VS Code 配置 Python 开发
- 下一篇: AttributeError: 'str