当前位置:网站首页>PyTorch 14. Module class
PyTorch 14. Module class
2022-04-23 07:29:00 【DCGJ666】
PyTorch 14. module class
module class
pytorch In fact, there is generally no particularly obvious Layer and Module The difference between , Whether it's a custom layer 、 Custom blocks 、 Custom model , It's all through inheritance Module Class , Actually Sequential Class is also inheritance Module class .
module The definition of a class :
class Module(object):
def __init__(self):
def forward(self, *input):
def add_module(self,name,module):
def cuda(self,device=None):
def cpu(self):
def __call__(self, *input, **kwargs):
def parameters(self, recurse=True):
def named_parameters(self, prefix='', recurse=True):
def children(self):
def named_children(self):
def modules(self):
def named_modules(self,memo=None,prefix=''):
def train(self,mode=True):
def eval(self):
def zero_grad(self):
def __repr__(self):
def __dir__(self):
When we define our own network , Need to inherit nn.Module class , And re implement the constructor __init__ Constructors and forward These two methods .
Attention skills :
(1) Generally, the layers with learnable parameters in the network ( Such as full connection layer , Convolution layer, etc ) Put it in the constructor __init__() in , Of course, I can also put layers without parameters inside
(2) Generally, layers that do not have learnable parameters ( Such as ReLU, dropout, BatchNormalnation layer ) Can be placed in the constructor , It can also be left out of the constructor , If you don't put it in the constructor __init__ Inside , It's in forward You can use nn.functional Instead of
(3)forward Methods must be rewritten , It is the function of implementing the model , The core of realizing the connection relationship between various layers
Be careful : As long as module in __init__ Defined layer , Even if forward Not used in , When saving the model , The network layer will also be saved , So if you don't need something, don't put it in __init__
torch.nn.Module Multiple implementations of classes
Method 1: adopt Sequential Layer to layer , That is, several layers are packed together as a large layer
import torch.nn as nn
from collections import OrderedDict
class MyNet(nn.Module):
def __init__(self):
super(MyNet, self).__init__()
self.conv_block = nn.Sequential(
nn.Conv2d(3, 32, 3,1,1),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.dense_block = nn.Sequential(
nn.Linear(32*3*3, 128),
nn.ReLU(),
nn.Linear(128,10)
)
def forward(self,x):
conv_out = self.conv_block(x)
res = conv_out.view(conv_out.size(0),-1)
out = self.dense_block(res)
return out
model = MyNet()
print(model)
Here, in every package , Each layer has no name , The default in accordance with the 0,1,2,3,4 Sort .
Method 2
import torch.nn as nn
from collections import OrderedDict
class MyNet(nn.Module):
def __init__(self):
super(MyNet, self).__init__()
self.conv_block = nn.Sequential(
OrderedDict(
[
("conv1", nn.Conv2d(3, 32, 3, 1, 1)),
("relu1", nn.ReLU()),
("pool", nn.MaxPool2d(2))
]
))
self.dense_block = nn.Sequential(
OrderedDict([
("dense1", nn.Linear(32 * 3 * 3, 128)),
("relu2", nn.ReLU()),
("dense2", nn.Linear(128, 10))
])
)
def forward(self, x):
conv_out = self.conv_block(x)
res = conv_out.view(conv_out.size(0), -1)
out = self.dense_block(res)
return out
model = MyNet()
print(model)
Method 3
import torch.nn as nn
from collections import OrderedDict
class MyNet(nn.Module):
def __init__(self):
super(MyNet, self).__init__()
self.conv_block=torch.nn.Sequential()
self.conv_block.add_module("conv1",torch.nn.Conv2d(3, 32, 3, 1, 1))
self.conv_block.add_module("relu1",torch.nn.ReLU())
self.conv_block.add_module("pool1",torch.nn.MaxPool2d(2))
self.dense_block = torch.nn.Sequential()
self.dense_block.add_module("dense1",torch.nn.Linear(32 * 3 * 3, 128))
self.dense_block.add_module("relu2",torch.nn.ReLU())
self.dense_block.add_module("dense2",torch.nn.Linear(128, 10))
def forward(self, x):
conv_out = self.conv_block(x)
res = conv_out.view(conv_out.size(0), -1)
out = self.dense_block(res)
return out
model = MyNet()
print(model)
The above method 2 And methods 3, In each package , Each layer has a name .
Particular attention :Sequential Although class inherits from Module class , The two have similar parts , But there are many different parts , Focus on :
Sequential Class implements integer indexing , Therefore, it can be used model[index] Get a layer in this way , however Module Class does not implement integer indexing , It is not possible to obtain the layer by integer index , But it provides several main methods , as follows :
The details can be found in My blog
children and modules Differences between
Be careful pytorch Whether it's a model 、 layer 、 Activation function 、 The loss function can be regarded as Module Development of , therefore modules and named_modules Will iterate layer by layer , from the shallower to the deeper , Add each custom block block、 then block Every layer inside is regarded as module To iterate , and children It's more intuitive , It means the so-called “ children ”, So there are no layers of depth
Reference resources :
https://zhuanlan.zhihu.com/p/156127643
版权声明
本文为[DCGJ666]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230611343827.html
边栏推荐
- Modifying a column with the 'identity' pattern is not supported
- 【点云系列】 A Rotation-Invariant Framework for Deep Point Cloud Analysis
- GIS实战应用案例100篇(五十二)-ArcGIS中用栅格裁剪栅格,如何保持行列数量一致并且对齐?
- 初探智能指针之std::shared_ptr、std::unique_ptr
- 重大安保事件应急通信系统解决方案
- 【点云系列】PnP-3D: A Plug-and-Play for 3D Point Clouds
- 商业广场无线对讲系统解决方案
- 【点云系列】点云隐式表达相关论文概要
- 【51单片机交通灯仿真】
- Infrared sensor control switch
猜你喜欢
rearrange 和 einsum 真的优雅吗
【51单片机交通灯仿真】
Systrace 解析
【点云系列】DeepMapping: Unsupervised Map Estimation From Multiple Point Clouds
Gephi tutorial [1] installation
Wechat applet uses wxml2canvas plug-in to generate some problem records of pictures
GIS实用小技巧(三)-CASS怎么添加图例?
imx6ull-qemu 裸机教程1:GPIO,IOMUX,I2C
Face_ Recognition face detection
项目文件“ ”已被重命名或已不在解决方案中、未能找到与解决方案关联的源代码管理提供程序——两个工程问题
随机推荐
基于openmv的无人机Apriltag动态追踪降落完整项目资料(labview+openmv+apriltag+正点原子四轴)
Systrace 解析
. net encountered failed to decode downloaded font while loading font:
美摄科技推出桌面端专业视频编辑解决方案——美映PC版
unhandled system error, NCCL version 2.7.8
PyTorch 11.正则化
【点云系列】Unsupervised Multi-Task Feature Learning on Point Clouds
【期刊会议系列】IEEE系列模板下载指南
GIS实战应用案例100篇(五十二)-ArcGIS中用栅格裁剪栅格,如何保持行列数量一致并且对齐?
【技术规范】:如何写好技术文档?
AUTOSAR从入门到精通100讲(五十二)-诊断和通信管理功能单元
【点云系列】Multi-view Neural Human Rendering (NHR)
Write a wechat double open gadget to your girlfriend
AUTOSAR从入门到精通100讲(五十一)-AUTOSAR网络管理
【点云系列】Learning Representations and Generative Models for 3D pointclouds
excel实战应用案例100讲(八)-Excel的报表连接功能
《Attention in Natural Language Processing》翻译
使用proteus仿真STM32超声波SRF04测距!Code+Proteus
防汛救灾应急通信系统
Chapter 2 pytoch foundation 2