PyTorch搭建神经网络模型的4种方法
PyTorch有多种方法搭建神经网络,下面识别手写数字为例,介绍4种搭建神经网络的方法。
方法一:torch.nn.Sequential()
torch.nn.Sequential类是torch.nn中的一种序列容器,参数会按照我们定义好的序列自动传递下去。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import torch.nn as nn class Net(nn.Module): def __init__( self ): super (Net, self ).__init__() self .conv1 = nn.Sequential( # input shape (1, 28, 28) nn.Conv2d( 1 , 16 , 5 , 1 , 2 ), # output shape (16, 28, 28) nn.ReLU(), nn.MaxPool2d( 2 ), # output shape (16, 14, 14) ) self .conv2 = nn.Sequential( nn.Conv2d( 16 , 32 , 5 , 1 , 2 ), # output shape (32, 14, 14) nn.ReLU(), nn.MaxPool2d( 2 ), # output shape (32, 7, 7) ) self .linear = nn.Linear( 32 * 7 * 7 , 10 ) def forward( self , x): x = self .conv1(x) x = self .conv2(x) x = x.view(x.size( 0 ), - 1 ) output = self .linear(x) return output net = Net() print (net) |
运行结果:
注意:这样做有一个问题,每一个层是没有名称,默认的是以0、1、2、3来命名,从上面的运行结果也可以看出。
方法二:torch.nn.Sequential() 搭配 collections.OrderDict()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import torch.nn as nn from collections import OrderedDict # OrderedDict是字典的子类,可以记住元素的添加顺序 class Net(nn.Module): def __init__( self ): super (Net, self ).__init__() self .conv1 = nn.Sequential(OrderedDict([ ( 'conv1' , nn.Conv2d( 1 , 16 , 5 , 1 , 2 )), ( 'ReLU1' , nn.ReLU()), ( 'pool1' , nn.MaxPool2d( 2 )), ])) self .conv2 = nn.Sequential(OrderedDict([ ( 'conv2' , nn.Conv2d( 16 , 32 , 5 , 1 , 2 )), ( 'ReLU2' , nn.ReLU()), ( 'pool2' , nn.MaxPool2d( 2 )), ])) self .linear = nn.Linear( 32 * 7 * 7 , 10 ) def forward( self , x): x = self .conv1(x) x = self .conv2(x) x = x.view(x.size( 0 ), - 1 ) output = self .linear(x) return output net = Net() print (net) |
运行结果:
从上面的结果中可以看出,这个时候每一个层都有了自己的名称,但是此时需要注意,我们并不能够通过名称直接获取层,依然只能通过索引index,即net.conv1[1] 是正确的,net.conv1['ReLU1']是错误的。这是因为torch.nn.Sequential()只支持index访问。
方法三:torch.nn.Sequential() 搭配 add_module()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import torch.nn as nn class Net(nn.Module): def __init__( self ): super (Net, self ).__init__() self .conv1 = nn.Sequential() self .conv1.add_module( 'conv1' , nn.Conv2d( 1 , 16 , 5 , 1 , 2 )) self .conv1.add_module( 'ReLU1' , nn.ReLU()) self .conv1.add_module( 'pool1' , nn.MaxPool2d( 2 )) self .conv2 = nn.Sequential() self .conv2.add_module( 'conv2' , nn.Conv2d( 16 , 32 , 5 , 1 , 2 )) self .conv2.add_module( 'ReLU2' , nn.ReLU()) self .conv2.add_module( 'pool2' , nn.MaxPool2d( 2 )) self .linear = nn.Linear( 32 * 7 * 7 , 10 ) def forward( self , x): x = self .conv1(x) x = self .conv2(x) x = x.view(x.size( 0 ), - 1 ) output = self .linear(x) return output net = Net() print (net) |
运行结果:
方法四
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import torch.nn as nn import torch.nn.functional as F class Net(nn.Module): def __init__( self ): super (Net, self ).__init__() self .conv1 = nn.Conv2d( 1 , 16 , 5 , 1 , 2 ) self .conv2 = nn.Conv2d( 16 , 32 , 5 , 1 , 2 ) self .linear = nn.Linear( 32 * 7 * 7 , 10 ) def forward( self , x): x = F.max_pool2d(F.relu( self .conv1(x)), 2 ) x = F.max_pool2d(F.relu( self .conv2(x)), 2 ) output = self .linear(x) return output net = Net() print (net) |
运行结果:
参考资料
[1] pytorch教程之nn.Sequential类详解——使用Sequential类来自定义顺序连接模型
[3] 《深度学习之PyTorch实战计算机视觉》
分类:
Pytorch
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通