一、交叉熵和softmax
交叉熵已經包括了softmax

二、理解
1、兩者的相同之處:
nn.Xxx和nn.functional.xxx的實際功能是相同的,即nn.Conv2d和nn.functional.conv2d都是進行卷積,nn.Dropout和nn.functional.dropout都是進行dropout,。。。。。;- 運行效率也是近乎相同。
nn.functional.xxx是函數接口,而nn.Xxx是nn.functional.xxx的類封裝,並且nn.Xxx都繼承於一個共同祖先nn.Module。
這一點導致nn.Xxx除了具有nn.functional.xxx功能之外,內部附帶了nn.Module相關的屬性和方法,例如train(), eval(),load_state_dict, state_dict 等。
2、兩者的差別之處:
- 兩者的調用方式不同。
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同時傳入輸入數據和weight, bias等其他參數 。
weight = torch.rand(64,3,3,3) bias = torch.rand(64) out = nn.functional.conv2d(inputs, weight, bias, padding=1)
nn.Xxx繼承於nn.Module, 能夠很好的與nn.Sequential結合使用, 而nn.functional.xxx無法與nn.Sequential結合使用。
fm_layer = nn.Sequential( nn.Conv2d(3, 64, kernel_size=3, padding=1), nn.BatchNorm2d(num_features=64), nn.ReLU(), nn.MaxPool2d(kernel_size=2), nn.Dropout(0.2) )
nn.Xxx不需要你自己定義和管理weight;而nn.functional.xxx需要你自己定義weight,每次調用的時候都需要手動傳入weight, 不利於代碼復用。
使用nn.Xxx定義一個CNN 。
class CNN(nn.Module): 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.function.xxx定義一個與上面相同的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.relu(out) out = F.max_pool2d(out) out = F.conv2d(x, self.cnn2_weight, self.bias2_weight) out = F.relu(out) out = F.max_pool2d(out) out = F.linear(x, self.linear1_weight, self.bias3_weight) return out
上面兩種定義方式得到CNN功能都是相同的,至於喜歡哪一種方式,是個人口味問題,但PyTorch官方推薦:具有學習參數的(例如,conv2d, linear, batch_norm)采用nn.Xxx方式,沒有學習參數的(例如,maxpool, loss func, activation func)等根據個人選擇使用nn.functional.xxx或者nn.Xxx方式。但關於dropout,個人強烈推薦使用nn.Xxx方式,因為一般情況下只有訓練階段才進行dropout,在eval階段都不會進行dropout。使用nn.Xxx方式定義dropout,在調用model.eval()之后,model中所有的dropout layer都關閉,但以nn.function.dropout方式定義dropout,在調用model.eval()之后並不能關閉dropout。
class Model1(nn.Module): def __init__(self): super(Model1, self).__init__() self.dropout = nn.Dropout(0.5) def forward(self, x): return self.dropout(x) class Model2(nn.Module): def __init__(self): super(Model2, self).__init__() def forward(self, x): return F.dropout(x) m1 = Model1() m2 = Model2() inputs = torch.rand(10) print(m1(inputs)) print(m2(inputs)) print(20 * '-' + "eval model:" + 20 * '-' + '\r\n') m1.eval() m2.eval() print(m1(inputs)) print(m2(inputs))
輸出:
從上面輸出可以看出m2調用了eval之后,dropout照樣還在正常工作。當然如果你有強烈願望堅持使用nn.functional.dropout,也可以采用下面方式來補救。
class Model3(nn.Module): def __init__(self): super(Model3, self).__init__() def forward(self, x): return F.dropout(x, training=self.training)
3、什么時候使用nn.functional.xxx,什么時候使用nn.Xxx?
這個問題依賴於你要解決你問題的復雜度和個人風格喜好。在nn.Xxx不能滿足你的功能需求時,nn.functional.xxx是更佳的選擇,因為nn.functional.xxx更加的靈活(更加接近底層),你可以在其基礎上定義出自己想要的功能。
個人偏向於在能使用nn.Xxx情況下盡量使用,不行再換nn.functional.xxx ,感覺這樣更能顯示出網絡的層次關系,也更加的純粹(所有layer和model本身都是Module,一種和諧統一的感覺)。
