【初级】用nn.Module, nn.Sequential构造深度学习网络

在Pytorch的网络构造代码中,都会看到继承nn.Module类的子类
然后重新编写子类的构造函数__init__()和forward函数 。
这里编写也有一些经验之谈。
1:
一般把conv,dense等函数放到init函数里面,而nn.Functional一般用来连接一些不需要训练参数的层比如relu,bn等等
2:
forward方法是必须要重写的,它是实现模型的功能,实现各个层之间的连接关系的核心。
下面先看一个简单的例子。

本文,先介绍nn.Sequential来构建基本网络。

Abcnet(nn.Module):
	def __init__(self, in_channels, out_channels, xxxx xxxx):
		super().__init__() 
	def forward(sefl, xxxxx):

关于super函数的解读,可以参考我的另一篇文章。

nn.Sequential是nn.Module的一个子类,它作为一个有序的容器,网络模块将按照在传入构造器的顺序依次被添加到计算图中执行。

我们直接看看nn.Sequential的源码

   def __init__(self, *args):
        super(Sequential, self).__init__()
        if len(args) == 1 and isinstance(args[0], OrderedDict):
            for key, module in args[0].items():
                self.add_module(key, module)
        else:
            for idx, module in enumerate(args):
                self.add_module(str(idx), module)
                
    def forward(self, input):
        for module in self:
            input = module(input)
        return input

if len(args) == 1 and isinstance(args[0], OrderedDict) 其判断是否使用OrderedDict.
OrderDict的构造方法是

collections.OrderDict([('a', nn.conv), ('b', nn.pool), ('c', nn.dense)])

如果自己定义了名称的话使用自定义的名称,否则将使用idx自动定义。
再看看nn.Sequential的三种实现

第一种实现

import torch.nn as nn
model = nn.Sequential(
                  nn.Conv2d(1,20,5), # in_channels=1, out_channels=20, kernel_size=5
                  nn.ReLU(),
                  nn.Conv2d(20,64,5),
                  nn.ReLU()
                )
 
print(model)
print(model[2]) # 通过索引获取第几个层
'''运行结果为:
Sequential(
  (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (1): ReLU()
  (2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  (3): ReLU()
)
Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
'''

这里nn.Sequential里面不是OrderDict
所以运行的是nn.Sequential源码里面的
for idx, module in enumerate(args):
self.add_module(str(idx), module)
注意这个add_module是nn.Module的方法之一,def add_module(self, name, module):
这样的网络构造有一个问题
每一个层是没有名称,默认的是以0、1、2、3来命名,从上面的运行结果也可以看出。

第二种实现

import torch.nn as nn
from collections import OrderedDict
model = nn.Sequential(OrderedDict([
                  ('conv1', nn.Conv2d(1,20,5)),
                  ('relu1', nn.ReLU()),
                  ('conv2', nn.Conv2d(20,64,5)),
                  ('relu2', nn.ReLU())
                ]))
 
print(model)
print(model[2]) # 通过索引获取第几个层 
#注意model["conv2"] 是错误的
#这其实是由它的定义实现的,看上面的Sequenrial定义可知,只支持index访问。
'''
运行结果为:
Sequential(
  (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (relu1): ReLU()
  (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  (relu2): ReLU()
)
Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
'''

第三种实现

import torch.nn as nn
from collections import OrderedDict
model = nn.Sequential()
model.add_module("conv1",nn.Conv2d(1,20,5))
model.add_module('relu1', nn.ReLU())
model.add_module('conv2', nn.Conv2d(20,64,5))
model.add_module('relu2', nn.ReLU())
 
print(model)
print(model[2]) # 通过索引获取第几个层
这个就是直接用父类nn.Module的add_module(self, name, module)方法,语法上类似keras

reference

https://blog.csdn.net/qq_27825451/article/details/90550890
https://blog.csdn.net/qq_27825451/article/details/90551513

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值