大部分nn中的層class都有nn.function對應,其區別是:
- nn.Module實現的layer是由class Layer(nn.Module)定義的特殊類,會自動提取可學習參數nn.Parameter
- nn.functional中的函數更像是純函數,由def function(input)定義。
由於兩者性能差異不大,所以具體使用取決於個人喜好。對於激活函數和池化層,由於沒有可學習參數,一般使用nn.functional完成,其他的有學習參數的部分則使用類。但是Droupout由於在訓練和測試時操作不同,所以建議使用nn.Module實現,它能夠通過model.eval加以區分。
一、nn.functional函數基本使用
import torch as t import torch.nn as nn from torch.autograd import Variable as V input_ = V(t.randn(2, 3)) model = nn.Linear(3, 4) output1 = model(input_) output2 = nn.functional.linear(input_, model.weight, model.bias) print(output1 == output2) b1 = nn.functional.relu(input_) b2 = nn.ReLU()(input_) print(b1 == b2)
二、搭配使用nn.Module和nn.functional
並不是什么難事,之前有接觸過,nn.functional不需要放入__init__進行構造,所以不具有可學習參數的部分可以使用nn.functional進行代替。
『PyTorch』第四彈_通過LeNet初識pytorch神經網絡_下
# Author : Hellcat # Time : 2018/2/11 import torch as t import torch.nn as nn import torch.nn.functional as F class LeNet(nn.Module): def __init__(self): super(LeNet,self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.conv2 = nn.Conv2d(6,16,5) self.fc1 = nn.Linear(16*5*5,120) self.fc2 = nn.Linear(120,84) self.fc3 = nn.Linear(84,10) def forward(self,x): x = F.max_pool2d(F.relu(self.conv1(x)),(2,2)) x = F.max_pool2d(F.relu(self.conv2(x)),2) x = x.view(x.size()[0], -1) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x
三、nn.functional函數構造nn.Module類
兩者主要的區別就是對於可學習參數nn.Parameter的識別能力,所以構造時添加了識別能力即可。
class Linear(nn.Module): def __init__(self, in_features, out_features): # nn.Module.__init__(self) super(Linear, self).__init__() self.w = nn.Parameter(t.randn(out_features, in_features)) # nn.Parameter是特殊Variable self.b = nn.Parameter(t.randn(out_features)) def forward(self, x): # wx+b return F.linear(x, self.w, self.b) layer = Linear(4, 3) input = V(t.randn(2, 4)) output = layer(input) print(output)
Variable containing:
1.7498 -0.8839 0.5314
-2.4863 -0.6442 1.1036
[torch.FloatTensor of size 2x3]