pytorch学习: 构建网络模型的几种方法
2023-09-11 14:17:15 时间
利用pytorch来构建网络模型有很多种方法,以下简单列出其中的四种。
假设构建一个网络模型如下:
卷积层--》Relu层--》池化层--》全连接层--》Relu层--》全连接层
首先导入几种方法用到的包:
import torch import torch.nn.functional as F from collections import OrderedDict
第一种方法
# Method 1 ----------------------------------------- class Net1(torch.nn.Module): def __init__(self): super(Net1, self).__init__() self.conv1 = torch.nn.Conv2d(3, 32, 3, 1, 1) self.dense1 = torch.nn.Linear(32 * 3 * 3, 128) self.dense2 = torch.nn.Linear(128, 10) def forward(self, x): x = F.max_pool2d(F.relu(self.conv(x)), 2) x = x.view(x.size(0), -1) x = F.relu(self.dense1(x)) x = self.dense2(x) return x print("Method 1:") model1 = Net1() print(model1)
这种方法比较常用,早期的教程通常就是使用这种方法。
第二种方法
# Method 2 ------------------------------------------ class Net2(torch.nn.Module): def __init__(self): super(Net2, self).__init__() self.conv = torch.nn.Sequential( torch.nn.Conv2d(3, 32, 3, 1, 1), torch.nn.ReLU(), torch.nn.MaxPool2d(2)) self.dense = torch.nn.Sequential( torch.nn.Linear(32 * 3 * 3, 128), torch.nn.ReLU(), torch.nn.Linear(128, 10) ) def forward(self, x): conv_out = self.conv1(x) res = conv_out.view(conv_out.size(0), -1) out = self.dense(res) return out print("Method 2:") model2 = Net2() print(model2)
这种方法利用torch.nn.Sequential()容器进行快速搭建,模型的各层被顺序添加到容器中。缺点是每层的编号是默认的阿拉伯数字,不易区分。
第三种方法:
# Method 3 ------------------------------- class Net3(torch.nn.Module): def __init__(self): super(Net3, self).__init__() self.conv=torch.nn.Sequential() self.conv.add_module("conv1",torch.nn.Conv2d(3, 32, 3, 1, 1)) self.conv.add_module("relu1",torch.nn.ReLU()) self.conv.add_module("pool1",torch.nn.MaxPool2d(2)) self.dense = torch.nn.Sequential() self.dense.add_module("dense1",torch.nn.Linear(32 * 3 * 3, 128)) self.dense.add_module("relu2",torch.nn.ReLU()) self.dense.add_module("dense2",torch.nn.Linear(128, 10)) def forward(self, x): conv_out = self.conv1(x) res = conv_out.view(conv_out.size(0), -1) out = self.dense(res) return out print("Method 3:") model3 = Net3() print(model3)
这种方法是对第二种方法的改进:通过add_module()添加每一层,并且为每一层增加了一个单独的名字。
第四种方法:
# Method 4 ------------------------------------------ class Net4(torch.nn.Module): def __init__(self): super(Net4, self).__init__() self.conv = torch.nn.Sequential( OrderedDict( [ ("conv1", torch.nn.Conv2d(3, 32, 3, 1, 1)), ("relu1", torch.nn.ReLU()), ("pool", torch.nn.MaxPool2d(2)) ] )) self.dense = torch.nn.Sequential( OrderedDict([ ("dense1", torch.nn.Linear(32 * 3 * 3, 128)), ("relu2", torch.nn.ReLU()), ("dense2", torch.nn.Linear(128, 10)) ]) ) def forward(self, x): conv_out = self.conv1(x) res = conv_out.view(conv_out.size(0), -1) out = self.dense(res) return out print("Method 4:") model4 = Net4() print(model4)
是第三种方法的另外一种写法,通过字典的形式添加每一层,并且设置单独的层名称。
完整代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import torch import torch.nn.functional as F from collections import OrderedDict # Method 1 ----------------------------------------- class Net1(torch.nn.Module): def __init__(self): super(Net1, self).__init__() self.conv1 = torch.nn.Conv2d(3, 32, 3, 1, 1) self.dense1 = torch.nn.Linear(32 * 3 * 3, 128) self.dense2 = torch.nn.Linear(128, 10) def forward(self, x): x = F.max_pool2d(F.relu(self.conv(x)), 2) x = x.view(x.size(0), -1) x = F.relu(self.dense1(x)) x = self.dense2() return x print("Method 1:") model1 = Net1() print(model1) # Method 2 ------------------------------------------ class Net2(torch.nn.Module): def __init__(self): super(Net2, self).__init__() self.conv = torch.nn.Sequential( torch.nn.Conv2d(3, 32, 3, 1, 1), torch.nn.ReLU(), torch.nn.MaxPool2d(2)) self.dense = torch.nn.Sequential( torch.nn.Linear(32 * 3 * 3, 128), torch.nn.ReLU(), torch.nn.Linear(128, 10) ) def forward(self, x): conv_out = self.conv1(x) res = conv_out.view(conv_out.size(0), -1) out = self.dense(res) return out print("Method 2:") model2 = Net2() print(model2) # Method 3 ------------------------------- class Net3(torch.nn.Module): def __init__(self): super(Net3, self).__init__() self.conv=torch.nn.Sequential() self.conv.add_module("conv1",torch.nn.Conv2d(3, 32, 3, 1, 1)) self.conv.add_module("relu1",torch.nn.ReLU()) self.conv.add_module("pool1",torch.nn.MaxPool2d(2)) self.dense = torch.nn.Sequential() self.dense.add_module("dense1",torch.nn.Linear(32 * 3 * 3, 128)) self.dense.add_module("relu2",torch.nn.ReLU()) self.dense.add_module("dense2",torch.nn.Linear(128, 10)) def forward(self, x): conv_out = self.conv1(x) res = conv_out.view(conv_out.size(0), -1) out = self.dense(res) return out print("Method 3:") model3 = Net3() print(model3) # Method 4 ------------------------------------------ class Net4(torch.nn.Module): def __init__(self): super(Net4, self).__init__() self.conv = torch.nn.Sequential( OrderedDict( [ ("conv1", torch.nn.Conv2d(3, 32, 3, 1, 1)), ("relu1", torch.nn.ReLU()), ("pool", torch.nn.MaxPool2d(2)) ] )) self.dense = torch.nn.Sequential( OrderedDict([ ("dense1", torch.nn.Linear(32 * 3 * 3, 128)), ("relu2", torch.nn.ReLU()), ("dense2", torch.nn.Linear(128, 10)) ]) ) def forward(self, x): conv_out = self.conv1(x) res = conv_out.view(conv_out.size(0), -1) out = self.dense(res) return out print("Method 4:") model4 = Net4() print(model4)
相关文章
- 神经网络与机器学习 笔记—核方法和径向基函数网络(上)
- 为什么Java里的Arrays.asList不能用add和remove方法?
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V3 -- 远程方法调用 整合 Spring
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V2 -- 本地方法调用 整合 Spring
- 关于 redux-saga 中 take 使用方法详解
- no space left on device解决方法
- C#【文件操作篇】一种提高C#实时存储excel文件速度的方法
- Eclipse创建的包变成文件夹的解决方法
- 程序包com.sun.image.codec.jpeg不存在解决方法
- Oracle字符串连接的方法
- 2022burpsuite最新使用方法.
- 《用Python写网络爬虫》——2.2 三种网页抓取方法
- Python多线程下载网络URL图片的方法
- Android 用ping的方法判断当前网络是否可用
- 一种非常巧妙的读取串口数据的方法--C#
- windows下 go get下载速度慢(网络原因)的解决方法
- HTTP请求中的User-Agent 判断浏览器类型的各种方法 网络爬虫的请求标示
- 网络I/O模型--04非阻塞模式(解除accept()、 read()方法阻塞)的基础上加入多线程技术
- 海量数据处理的方法总结
- Android学习之修改主题的方法
- Unity 基础之 实现动态加载网络端、PC端和 Android 端指定路径下的图片的简单方法
- 从贝叶斯方法谈到贝叶斯网络
- 快速备份sqlserver2005以上版本数据库的方法-摘自网络
- windows上zend server安装 报The server encountered an internal error or misconfiguration and was unable to complete your request -解决方法 摘自网络
- Excel中VBA 连接 数据库 方法- 摘自网络
- Radware:防御现代鱼叉式网络钓鱼攻击的方法