生活随笔
收集整理的這篇文章主要介紹了
第八讲,nn模型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一,torch.nn組成和屬性
nn模塊組成
nn.Module的屬性
nn.Module的特點和注意點:
一個module可以包含多個子module 一個module相當于一個運算,必須實現forward()函數 每個module都有8個字典管理他的屬性
二,模型的容器,裝有子module
nn.Sequential:按順序包裝多個網絡層
順序性 自帶forward()–》自動通過for循環依次執行前向傳播運算
import torch
import torchvision
import torch
. nn
as nn
from collections
import OrderedDict
class LeNetSequential ( nn
. Module
) : def __init__ ( self
, classes
) : super ( LeNetSequential
, self
) . __init__
( ) self
. features
= nn
. Sequential
( nn
. Conv2d
( 3 , 6 , 5 ) , nn
. ReLU
( ) , nn
. MaxPool2d
( kernel_size
= 2 , stride
= 2 ) , nn
. Conv2d
( 6 , 16 , 5 ) , nn
. ReLU
( ) , nn
. MaxPool2d
( kernel_size
= 2 , stride
= 2 ) , ) self
. classifier
= nn
. Sequential
( nn
. Linear
( 16 * 5 * 5 , 120 ) , nn
. ReLU
( ) , nn
. Linear
( 120 , 84 ) , nn
. ReLU
( ) , nn
. Linear
( 84 , classes
) , ) def forward ( self
, x
) : x
= self
. features
( x
) x
= x
. view
( x
. size
( ) [ 0 ] , - 1 ) x
= self
. classifier
( x
) return x
class LeNetSequentialOrderDict ( nn
. Module
) : def __init__ ( self
, classes
) : super ( LeNetSequentialOrderDict
, self
) . __init__
( ) self
. features
= nn
. Sequential
( OrderedDict
( { 'conv1' : nn
. Conv2d
( 3 , 6 , 5 ) , 'relu1' : nn
. ReLU
( inplace
= True ) , 'pool1' : nn
. MaxPool2d
( kernel_size
= 2 , stride
= 2 ) , 'conv2' : nn
. Conv2d
( 6 , 16 , 5 ) , 'relu2' : nn
. ReLU
( inplace
= True ) , 'pool2' : nn
. MaxPool2d
( kernel_size
= 2 , stride
= 2 ) , } ) ) self
. classifier
= nn
. Sequential
( OrderedDict
( { 'fc1' : nn
. Linear
( 16 * 5 * 5 , 120 ) , 'relu3' : nn
. ReLU
( ) , 'fc2' : nn
. Linear
( 120 , 84 ) , 'relu4' : nn
. ReLU
( inplace
= True ) , 'fc3' : nn
. Linear
( 84 , classes
) , } ) ) def forward ( self
, x
) : x
= self
. features
( x
) x
= x
. view
( x
. size
( ) [ 0 ] , - 1 ) x
= self
. classifier
( x
) return xnet
= LeNetSequential
( classes
= 2 )
net
= LeNetSequentialOrderDict
( classes
= 2 ) fake_img
= torch
. randn
( ( 4 , 3 , 32 , 32 ) , dtype
= torch
. float32
) output
= net
( fake_img
) print ( net
)
print ( output
)
nn.ModuleList:像Python的list一樣包裝多個網絡層
append():在ModuleList后面添加網絡層 extend():拼接兩個ModuleList insert():指定在ModuleList中位置插入網絡層
import torch
import torchvision
import torch
. nn
as nn
from collections
import OrderedDict
class ModuleList ( nn
. Module
) : def __init__ ( self
) : super ( ModuleList
, self
) . __init__
( ) self
. linears
= nn
. ModuleList
( [ nn
. Linear
( 10 , 10 ) for i
in range ( 20 ) ] ) def forward ( self
, x
) : for i
, linear
in enumerate ( self
. linears
) : x
= linear
( x
) return xnet
= ModuleList
( ) print ( net
) fake_data
= torch
. ones
( ( 10 , 10 ) ) output
= net
( fake_data
) print ( output
)
nn.ModuleDict:像Python的dict一樣包裝多個網絡層
clear():清空ModuleDict items():返回可迭代的鍵值對 keys():返回鍵 values():返回值 pop():返回一對鍵值,并從字典中刪除
import torch
import torchvision
import torch
. nn
as nn
from collections
import OrderedDict
class ModuleDict ( nn
. Module
) : def __init__ ( self
) : super ( ModuleDict
, self
) . __init__
( ) self
. choices
= nn
. ModuleDict
( { 'conv' : nn
. Conv2d
( 10 , 10 , 3 ) , 'pool' : nn
. MaxPool2d
( 3 ) } ) self
. activations
= nn
. ModuleDict
( { 'relu' : nn
. ReLU
( ) , 'prelu' : nn
. PReLU
( ) } ) def forward ( self
, x
, choice
, act
) : x
= self
. choices
[ choice
] ( x
) x
= self
. activations
[ act
] ( x
) return xnet
= ModuleDict
( ) fake_img
= torch
. randn
( ( 4 , 10 , 32 , 32 ) ) output
= net
( fake_img
, 'conv' , 'relu' ) print ( output
)
三,常用nn網絡層
nn.Conv2d():二維卷積
in_channels:輸入通道數 out_channels:輸出通道數 kernel_size:卷積核尺寸 stride:步長 padding:填充個數 dilation:空洞卷積大小 groups:分組卷積設置 bias:偏置
nn.ConvTranspose2d:轉置卷積,實現上采樣
in_channels:輸入通道數 out_channels:輸出通道數 kernel_size:卷積核尺寸 stride:步長 padding:填充個數 dilation:空洞卷積大小 groups:分組卷積設置 bias:偏置
nn.MaxPool2d:二維最大池化
kernel_size:池化核尺寸 stride:步長 padding:填充個數 dilation:池化核間隔大小 ceil_mode:尺寸向上取整 return_indices:記錄池化像素索引(取出最大值時當時最大的值所處的位置索引)
nn.AvgPool2d:二維平均池化
kernel_size:池化核尺寸 stride:步長 padding:填充個數 dilation:池化核間隔大小 ceil_mode:尺寸向上取整 count_include_pad:填充值用于計算 divisor_override:除法因子–》當設置了除法因子之后,全部權值加和之后不除以權值個數而除以因子
nn.MaxUnpool2d:反池化—》對二維圖像進行最大池化上采樣
kernel_size:池化核尺寸 stride:步長 padding:填充個數 需將正常池化以后記錄的索引值傳入反池化函數
nn.Linear:線性層—》對一維信號進行線性組合
in_features:輸入節點數 out_features:輸出節點數 bias:是否需要偏置
總結
以上是生活随笔 為你收集整理的第八讲,nn模型 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。