Pytorch(筆記6)--nn.Module功能詳解

    在接觸了之前所說的Conv,pool,Batchnorm,ReLU等方法都是神經網絡中常見的操作,我們可以根據這些方法來自定義網絡模型,也可以根據需求對經典模型進行調整,他們都繼承共同的抽象類nn.Module,其中包含好了很多函數。

1.nn.Sequential

   這個方法中可以封裝多個子類,注意,一定繼承nn.Module的類,在調用的時候,可以使用下面的方法

net = nn.Sequential(
nn.Conv2d(3,32,3,1,1),
nn.MaxPool2d(2,2),
nn.ReLU(inplace=True),
nn.BatchNorm2d(32)
)
x = t.rand(1,3,32,32)
x = net(x)
x.shape

2.nn.parameters

  這個是一個很重要的方法,用於存儲模型訓練中所有的參數信息,我們可以自定義需要學習的內容,

  • 添加自定義參數到net.parameters中,
self.gamma = Parameter(torch.zeros(1))

就可以將我們自定義的參數加入到優化參數列表中了

  • 查看參數shape
net=nn.Sequential(nn.Linear(4,2),nn.Linear(2,2))
list(net.parameters())[0].shape #查詢網絡參數結構
  • 查看參數列表

   

in:dict(net.named_parameters()).items()

out:[('1.weight', Parameter containing:
  tensor([[ 0.2523,  0.7062],
          [-0.6563, -0.6892]], requires_grad=True)),
 ('0.bias', Parameter containing:
  tensor([0.3603, 0.4283], requires_grad=True)),
 ('1.bias', Parameter containing:
  tensor([ 0.2454, -0.0655], requires_grad=True)),
 ('0.weight', Parameter containing:
  tensor([[-0.1277, -0.3428, -0.1208,  0.2463],
          [ 0.0940,  0.0312,  0.3777, -0.2666]], requires_grad=True))]
  • 定義優化器
optim = optim.SGD(net.parameters(),lr = 0.1)

3.nn.to(device)

通常我們在訓練的過程中,都會指定cuda,或者使用CPU進行訓練,可以使用這種方法來實現,使網絡在GPU上進行訓練

device = t.device("cuda" if t.cuda.is_available() else "cpu") #如果GPU允許就使用GPU
net = net.to(device)

4.save and load 

在訓練過程中達到一定的acc或者一定的epoch,我們就會使網絡結果持久化到本地,避免一些服務器異常的情況,在新增數據進行訓練的時候也可以使用預訓練的模型上進行

  self.load_state_dict(torch.load(‘model.pth’))

  t.save(net.state_dict(), name)

5.train and test

     在訓練過程中,沒訓練幾個epoch或者一些batch,就要進行val操作,至於val和test的區別,就是我們高三時候的每週一小考和高考一樣,“把周測當高考,把高考當週測”,這是我的母校綏化一中的標語,哈哈,會不會有學弟學妹在看這篇博客。

net.to(device)#加載網絡模型到GPU
...
net.train() #調整到訓練狀態
...
with torch.no_grad(): #標記爲驗證狀態 
net.eval()
...

6.impoement own layer 

 只要知道了規則,繼承nn.Module,就可以實現我們自己的網絡結構,下面我分享一個,我經常使用的base_module

class BasicModule(t.nn.Module):
    """
    封裝了nn.Module,主要是提供了save和load兩個方法
    """
    def __init__(self):
        super(BasicModule,self).__init__()
        self.model_name=str(type(self))# 默認名字

    def load(self, path):
        """
        可加載指定路徑的模型
        """
        self.load_state_dict(t.load(path))

    def save(self, name=None):
        """
        保存模型,默認使用“模型名字+時間”作爲文件名
        """
        if name is None:
            prefix = 'checkpoints/' + self.model_name + '_'
            name = time.strftime(prefix + '%m%d_%H:%M:%S.pth')
        t.save(self.state_dict(), name)
        return name
        """
        獲取參數優化器信息
        """
    def get_optimizer(self, lr, weight_decay):
        return t.optim.Adam(self.parameters(), lr=lr, weight_decay=weight_decay)


class Flat(t.nn.Module):
    """
    把輸入reshape成(batch_size,dim_length)
    """
    def __init__(self):
        super(Flat, self).__init__()
        #self.size = size

    def forward(self, x):
        return x.view(x.size(0), -1)

    這幾個方法都是訓練中很常用的,只要按下面的語法就可以實現我們自己的神經網絡了。

class Modulename(t.nn.Module,args=):
    def __init__(self):
        super(Modulename,self).__init__()

    希望這些常見的方法對你有一點幫助,歡迎指正,溝通! 堅持一件事或許很難,但堅持下來一定很酷!^_^

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章