PyTorch搭建神經網絡模型的4種方法


PyTorch有多種方法搭建神經網絡,下面識別手寫數字為例,介紹4種搭建神經網絡的方法。

方法一:torch.nn.Sequential()

torch.nn.Sequential類是torch.nn中的一種序列容器,參數會按照我們定義好的序列自動傳遞下去。
import torch.nn as nn
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Sequential(           # input shape (1, 28, 28)
            nn.Conv2d(1, 16, 5, 1, 2),        # output shape (16, 28, 28)
            nn.ReLU(),
            nn.MaxPool2d(2),                  # output shape (16, 14, 14)
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(16, 32, 5, 1, 2),       # output shape (32, 14, 14)
            nn.ReLU(),
            nn.MaxPool2d(2),                  # output shape (32, 7, 7)
        )
        self.linear = nn.Linear(32*7*7, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)
        output = self.linear(x)
        return output

net = Net()
print(net)

運行結果:

注意:這樣做有一個問題,每一個層是沒有名稱,默認的是以0、1、2、3來命名,從上面的運行結果也可以看出。

方法二:torch.nn.Sequential() 搭配 collections.OrderDict()

import torch.nn as nn
from collections import OrderedDict   # OrderedDict是字典的子類,可以記住元素的添加順序
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Sequential(OrderedDict([
            ('conv1', nn.Conv2d(1, 16, 5, 1, 2)),
            ('ReLU1', nn.ReLU()),
            ('pool1', nn.MaxPool2d(2)),
        ]))
        self.conv2 = nn.Sequential(OrderedDict([
            ('conv2', nn.Conv2d(16, 32, 5, 1, 2)),
            ('ReLU2', nn.ReLU()),
            ('pool2', nn.MaxPool2d(2)),
        ]))
        self.linear = nn.Linear(32*7*7, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)
        output = self.linear(x)
        return output

net = Net()
print(net)

運行結果:

從上面的結果中可以看出,這個時候每一個層都有了自己的名稱,但是此時需要注意,我們並不能夠通過名稱直接獲取層,依然只能通過索引index,即net.conv1[1] 是正確的,net.conv1['ReLU1']是錯誤的。這是因為torch.nn.Sequential()只支持index訪問。

方法三:torch.nn.Sequential() 搭配 add_module()

import torch.nn as nn
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Sequential()
        self.conv1.add_module('conv1', nn.Conv2d(1, 16, 5, 1, 2))
        self.conv1.add_module('ReLU1', nn.ReLU())
        self.conv1.add_module('pool1', nn.MaxPool2d(2))

        self.conv2 = nn.Sequential()
        self.conv2.add_module('conv2', nn.Conv2d(16, 32, 5, 1, 2))
        self.conv2.add_module('ReLU2', nn.ReLU())
        self.conv2.add_module('pool2', nn.MaxPool2d(2))

        self.linear = nn.Linear(32*7*7, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)
        output = self.linear(x)
        return output

net = Net()
print(net)

運行結果:

方法四

import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 16, 5, 1, 2)
        self.conv2 = nn.Conv2d(16, 32, 5, 1, 2)
        self.linear = nn.Linear(32*7*7, 10)

    def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), 2)
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        output = self.linear(x)
        return output

net = Net()
print(net)

運行結果:

參考資料

[1] pytorch教程之nn.Sequential類詳解——使用Sequential類來自定義順序連接模型

[2] pytorch構建網絡模型的4種方法

[3] 《深度學習之PyTorch實戰計算機視覺》

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM