最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pytorch - TORCH.NN.INIT 參數(shù)初始化的操作

 更新時(shí)間:2021年02月27日 12:11:28   作者:Peanut_范  
這篇文章主要介紹了Pytorch - TORCH.NN.INIT 參數(shù)初始化的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

路徑:

https://pytorch.org/docs/master/nn.init.html#nn-init-doc

初始化函數(shù):torch.nn.init

# -*- coding: utf-8 -*-
"""
Created on 2019
@author: fancp
"""
import torch 
import torch.nn as nn
w = torch.empty(3,5)
#1.均勻分布 - u(a,b)
#torch.nn.init.uniform_(tensor, a=0.0, b=1.0)
print(nn.init.uniform_(w))
# =============================================================================
# tensor([[0.9160, 0.1832, 0.5278, 0.5480, 0.6754],
#     [0.9509, 0.8325, 0.9149, 0.8192, 0.9950],
#     [0.4847, 0.4148, 0.8161, 0.0948, 0.3787]])
# =============================================================================
#2.正態(tài)分布 - N(mean, std)
#torch.nn.init.normal_(tensor, mean=0.0, std=1.0)
print(nn.init.normal_(w))
# =============================================================================
# tensor([[ 0.4388, 0.3083, -0.6803, -1.1476, -0.6084],
#     [ 0.5148, -0.2876, -1.2222, 0.6990, -0.1595],
#     [-2.0834, -1.6288, 0.5057, -0.5754, 0.3052]])
# =============================================================================
#3.常數(shù) - 固定值 val
#torch.nn.init.constant_(tensor, val)
print(nn.init.constant_(w, 0.3))
# =============================================================================
# tensor([[0.3000, 0.3000, 0.3000, 0.3000, 0.3000],
#     [0.3000, 0.3000, 0.3000, 0.3000, 0.3000],
#     [0.3000, 0.3000, 0.3000, 0.3000, 0.3000]])
# =============================================================================
#4.全1分布
#torch.nn.init.ones_(tensor)
print(nn.init.ones_(w))
# =============================================================================
# tensor([[1., 1., 1., 1., 1.],
#     [1., 1., 1., 1., 1.],
#     [1., 1., 1., 1., 1.]])
# =============================================================================
#5.全0分布
#torch.nn.init.zeros_(tensor)
print(nn.init.zeros_(w))
# =============================================================================
# tensor([[0., 0., 0., 0., 0.],
#     [0., 0., 0., 0., 0.],
#     [0., 0., 0., 0., 0.]])
# =============================================================================
#6.對(duì)角線為 1,其它為 0
#torch.nn.init.eye_(tensor)
print(nn.init.eye_(w))
# =============================================================================
# tensor([[1., 0., 0., 0., 0.],
#     [0., 1., 0., 0., 0.],
#     [0., 0., 1., 0., 0.]])
# =============================================================================
#7.xavier_uniform 初始化
#torch.nn.init.xavier_uniform_(tensor, gain=1.0)
#From - Understanding the difficulty of training deep feedforward neural networks - Bengio 2010
print(nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu')))
# =============================================================================
# tensor([[-0.1270, 0.3963, 0.9531, -0.2949, 0.8294],
#     [-0.9759, -0.6335, 0.9299, -1.0988, -0.1496],
#     [-0.7224, 0.2181, -1.1219, 0.8629, -0.8825]])
# =============================================================================
#8.xavier_normal 初始化
#torch.nn.init.xavier_normal_(tensor, gain=1.0)
print(nn.init.xavier_normal_(w))
# =============================================================================
# tensor([[ 1.0463, 0.1275, -0.3752, 0.1858, 1.1008],
#     [-0.5560, 0.2837, 0.1000, -0.5835, 0.7886],
#     [-0.2417, 0.1763, -0.7495, 0.4677, -0.1185]])
# =============================================================================
#9.kaiming_uniform 初始化
#torch.nn.init.kaiming_uniform_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')
#From - Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - HeKaiming 2015
print(nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu'))
# =============================================================================
# tensor([[-0.7712, 0.9344, 0.8304, 0.2367, 0.0478],
#     [-0.6139, -0.3916, -0.0835, 0.5975, 0.1717],
#     [ 0.3197, -0.9825, -0.5380, -1.0033, -0.3701]])
# =============================================================================
#10.kaiming_normal 初始化
#torch.nn.init.kaiming_normal_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')
print(nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu'))
# =============================================================================
# tensor([[-0.0210, 0.5532, -0.8647, 0.9813, 0.0466],
#     [ 0.7713, -1.0418, 0.7264, 0.5547, 0.7403],
#     [-0.8471, -1.7371, 1.3333, 0.0395, 1.0787]])
# =============================================================================
#11.正交矩陣 - (semi)orthogonal matrix
#torch.nn.init.orthogonal_(tensor, gain=1)
#From - Exact solutions to the nonlinear dynamics of learning in deep linear neural networks - Saxe 2013
print(nn.init.orthogonal_(w))
# =============================================================================
# tensor([[-0.0346, -0.7607, -0.0428, 0.4771, 0.4366],
#     [-0.0412, -0.0836, 0.9847, 0.0703, -0.1293],
#     [-0.6639, 0.4551, 0.0731, 0.1674, 0.5646]])
# =============================================================================
#12.稀疏矩陣 - sparse matrix 
#torch.nn.init.sparse_(tensor, sparsity, std=0.01)
#From - Deep learning via Hessian-free optimization - Martens 2010
print(nn.init.sparse_(w, sparsity=0.1))
# =============================================================================
# tensor([[ 0.0000, 0.0000, -0.0077, 0.0000, -0.0046],
#     [ 0.0152, 0.0030, 0.0000, -0.0029, 0.0005],
#     [ 0.0199, 0.0132, -0.0088, 0.0060, 0.0000]])
# =============================================================================

補(bǔ)充:【pytorch參數(shù)初始化】 pytorch默認(rèn)參數(shù)初始化以及自定義參數(shù)初始化

本文用兩個(gè)問題來引入

1.pytorch自定義網(wǎng)絡(luò)結(jié)構(gòu)不進(jìn)行參數(shù)初始化會(huì)怎樣,參數(shù)值是隨機(jī)的嗎?

2.如何自定義參數(shù)初始化?

先回答第一個(gè)問題

在pytorch中,有自己默認(rèn)初始化參數(shù)方式,所以在你定義好網(wǎng)絡(luò)結(jié)構(gòu)以后,不進(jìn)行參數(shù)初始化也是可以的。

1.Conv2d繼承自_ConvNd,在_ConvNd中,可以看到默認(rèn)參數(shù)就是進(jìn)行初始化的,如下圖所示

2.torch.nn.BatchNorm2d也一樣有默認(rèn)初始化的方式

3.torch.nn.Linear也如此

現(xiàn)在來回答第二個(gè)問題。

pytorch中對(duì)神經(jīng)網(wǎng)絡(luò)模型中的參數(shù)進(jìn)行初始化方法如下:

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_()

首先定義了一個(gè)初始化函數(shù),接著進(jìn)行調(diào)用就ok了,不過要先把網(wǎng)絡(luò)模型實(shí)例化:

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

此上就完成了對(duì)模型中訓(xùn)練參數(shù)的初始化。

在知乎上也有看到一個(gè)類似的版本,也相應(yīng)的貼上來作為參考了:

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)

再說一下關(guān)于模型的保存及加載

1.保存有兩種方式,第一種是保存模型的整個(gè)結(jié)構(gòu)信息和參數(shù),第二種是只保存模型的參數(shù)

 #保存整個(gè)網(wǎng)絡(luò)模型及參數(shù)
 torch.save(net, 'net.pkl') 
 
 #僅保存模型參數(shù)
 torch.save(net.state_dict(), 'net_params.pkl')

2.加載對(duì)應(yīng)保存的兩種網(wǎng)絡(luò)

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • Python切片用法實(shí)例教程

    Python切片用法實(shí)例教程

    這篇文章主要介紹了Python切片用法,可以有效的提高Python程序設(shè)計(jì)的效率,更加靈活的進(jìn)行Python循環(huán)操作,需要的朋友可以參考下
    2014-09-09
  • 詳解Appium+Python之生成html測(cè)試報(bào)告

    詳解Appium+Python之生成html測(cè)試報(bào)告

    這篇文章主要介紹了詳解Appium+Python之生成html測(cè)試報(bào)告,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Django nginx配置實(shí)現(xiàn)過程詳解

    Django nginx配置實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了Django nginx配置實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • centos安裝python3.10的教程

    centos安裝python3.10的教程

    文章主要介紹了在CentOS系統(tǒng)上安裝Python 3.10.1的步驟,包括升級(jí)OpenSSL到1.1.1,以及詳細(xì)的操作過程,如切換目錄、下載安裝包、解壓、配置、編譯安裝、創(chuàng)建軟連接和驗(yàn)證等
    2025-03-03
  • Django分頁器的用法詳解

    Django分頁器的用法詳解

    本文主要介紹在利用Django開發(fā)MVT模型項(xiàng)目時(shí)分頁器的使用,感興趣的朋友可以參考下
    2021-05-05
  • Python中schedule模塊關(guān)于定時(shí)任務(wù)使用方法

    Python中schedule模塊關(guān)于定時(shí)任務(wù)使用方法

    這篇文章主要介紹了Python中schedule模塊關(guān)于定時(shí)任務(wù)使用方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • Python玩轉(zhuǎn)Excel的讀寫改實(shí)例

    Python玩轉(zhuǎn)Excel的讀寫改實(shí)例

    今天小編就為大家分享一篇關(guān)于Python玩轉(zhuǎn)Excel的讀寫改實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Pygame實(shí)戰(zhàn)之檢測(cè)按鍵正確的小游戲

    Pygame實(shí)戰(zhàn)之檢測(cè)按鍵正確的小游戲

    這篇文章主要為大家介紹了利用Pygame模塊實(shí)現(xiàn)的檢測(cè)按鍵正確的小游戲:每個(gè)字母有10秒的按鍵時(shí)間,如果按對(duì),則隨機(jī)產(chǎn)生新的字符,一共60s,如果時(shí)間到了,則游戲結(jié)束??靵砀S小編一起學(xué)習(xí)一下吧
    2021-12-12
  • Python通過調(diào)用有道翻譯api實(shí)現(xiàn)翻譯功能示例

    Python通過調(diào)用有道翻譯api實(shí)現(xiàn)翻譯功能示例

    這篇文章主要介紹了Python通過調(diào)用有道翻譯api實(shí)現(xiàn)翻譯功能,結(jié)合實(shí)例形式分析了基于Python實(shí)現(xiàn)的有道翻譯api調(diào)用相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • Python計(jì)算任意多邊形間的重疊面積的示例代碼

    Python計(jì)算任意多邊形間的重疊面積的示例代碼

    最近有個(gè)作業(yè),給定的數(shù)據(jù)為多邊形的各個(gè)頂點(diǎn),為N*2的矩陣,N 為多邊形的頂點(diǎn)個(gè)數(shù),計(jì)算任意兩個(gè)多邊形重疊面積計(jì)算,本文就來詳細(xì)的介紹一下
    2021-08-08

最新評(píng)論

塔河县| 南阳市| 扬州市| 平和县| 八宿县| 邮箱| 磐石市| 龙江县| 苍溪县| 凤台县| 辽源市| 石泉县| 阿坝县| 桐柏县| 盱眙县| 隆林| 古交市| 伊宁市| 深州市| 阿坝| 六盘水市| 二手房| 苍溪县| 勃利县| 邳州市| 汉源县| 汕头市| 东阳市| 庄浪县| 双牌县| 宣武区| 孟津县| 漾濞| 宿州市| 彰化县| 白银市| 临夏市| 光泽县| 屯门区| 唐河县| 蕲春县|