【pytorch參數初始化】 pytorch默認參數初始化以及自定義參數初始化

本文用兩個問題來引入

1.pytorch自定義網絡結構不進行參數初始化會怎樣,參數值是隨機的嗎?

2.如何自定義參數初始化?

先回答第一個問題

在pytorch中,有自己默認初始化參數方式,所以在你定義好網絡結構以後,不進行參數初始化也是可以的。

1.Conv2d繼承自_ConvNd,在_ConvNd中,可以看到默認參數就是進行初始化的,如下圖所示

2.torch.nn.BatchNorm2d也一樣有默認初始化的方式

3.torch.nn.Linear也如此

現在來回答第二個問題。

pytorch中對神經網絡模型中的參數進行初始化方法如下:

from torch.nn import init
#define the initial function to init the layer's parameters for the network
def weigth_init(m):
    if isinstance(m, nn.Conv2d):
        init.xavier_uniform_(m.weight.data)
        init.constant_(m.bias.data,0.1)
    elif isinstance(m, nn.BatchNorm2d):
        m.weight.data.fill_(1)
        m.bias.data.zero_()
    elif isinstance(m, nn.Linear):
        m.weight.data.normal_(0,0.01)
        m.bias.data.zero_()

  首先定義了一個初始化函數,接着進行調用就ok了,不過要先把網絡模型實例化:

  #Define Network
    model = Net(args.input_channel,args.output_channel)
    model.apply(weigth_init)

 此上就完成了對模型中訓練參數的初始化。

 在知乎上也有看到一個類似的版本,也相應的貼上來作爲參考了:


def initNetParams(net):
    '''Init net parameters.'''
    for m in net.modules():
        if isinstance(m, nn.Conv2d):
            init.xavier_uniform(m.weight)
            if m.bias:
                init.constant(m.bias, 0)
        elif isinstance(m, nn.BatchNorm2d):
            init.constant(m.weight, 1)
            init.constant(m.bias, 0)
        elif isinstance(m, nn.Linear):
            init.normal(m.weight, std=1e-3)
            if m.bias:
                init.constant(m.bias, 0)
 
initNetParams(net)

再說一下關於模型的保存及加載

1.保存有兩種方式,第一種是保存模型的整個結構信息和參數,第二種是隻保存模型的參數

 #保存整個網絡模型及參數
 torch.save(net, 'net.pkl') 

 #僅保存模型參數
 torch.save(net.state_dict(), 'net_params.pkl')

2.加載對應保存的兩種網絡

# 保存和加載整個模型  
torch.save(model_object, 'model.pth')  
model = torch.load('model.pth')  
 
# 僅保存和加載模型參數  
torch.save(model_object.state_dict(), 'params.pth')  
model_object.load_state_dict(torch.load('params.pth'))

 

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