PyTorch学习—9.模型容器与AlexNet构建

文章目录一、模型容器1. 三种容器的总结二、AlexNet构建一、模型容器  PyTorch提供的模型容器中有三个非常常用的,这三个容器如下:nn.Sequetial功能:按顺序的将一组网络层包装起来其特性总结如下:顺序性:各网络层之间严格按照顺序构建自带forward():自带的forward里,通过for循环依次执行前向传播运算下面使用Sequetial来包装LeNet模型 class LeNetSequential(nn.Module): def __init_
摘要由CSDN通过智能技术生成

一、模型容器

  PyTorch提供的模型容器中有三个非常常用的,这三个容器如下:

  • nn.Sequetial
    功能:按顺序的将一组网络层包装起来

    其特性总结如下:

    • 顺序性:各网络层之间严格按照顺序构建
    • 自带forward():自带的forward里,通过for循环依次执行前向传播运算

    下面使用Sequetial来包装LeNet模型

    	class LeNetSequential(nn.Module):
    	    def __init__(self, classes):
    	        super(LeNetSequential, self).__init__()
    	        # 将卷积层与池化层包装成features网络层
    	        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),)
    			
    			# 将全连接网络包装成classifier层
    	        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 Sequential(Module):
    	    r"""A sequential container.
    	    Modules will be added to it in the order they are passed in the constructor.
    	    Alternatively, an ordered dict of modules can also be passed in.
    	
    	    To make it easier to understand, here is a small example::
    	
    	        # Example of using Sequential
    	        model = nn.Sequential(
    	                  nn.Conv2d(1,20,5),
    	                  nn.ReLU(),
    	                  nn.Conv2d(20,64,5),
    	                  nn.ReLU()
    	                )
    	
    	        # Example of using Sequential with OrderedDict
    	        model = nn.Sequential(OrderedDict([
    	                  ('conv1', nn.Conv2d(1,20,5)),
    	                  ('relu1', nn.ReLU()),
    	                  ('conv2', nn.Conv2d(20,64,5)),
    	                  ('relu2', nn.ReLU())
    	                ]))
    	    """
    	
    	    def __init__(self, *args):
    	        super(Sequential, self).__init__()
    	        # 将传入的网络层添加到Sequential中
    	        # 判断输入的参数是否为有序字典
    	        if len(args) == 1 and isinstance(args[0], OrderedDict):
    	            for key, module in args[0].items():
    	                self.add_module(key, module)
    	        else:
    	        # 如果不是的话,就直接将传入的网络层添加到Sequential中
    	            for idx, module in enumerate(args):
    	                self.add_module(str(idx), module)
    	
    	    def _get_item_by_idx(self, iterator, idx):
    	        """Get the idx-th item of the iterator"""
    	        size = len(self)
    	        idx = operator.index(idx)
    	        if not -size <= idx < size:
    	            raise IndexError('index {} is out of range'.format(idx))
    	        idx %= size
    	        return next(islice(iterator,
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值