• 神经网络-使用Sequential搭建神经网络


    我们以这个神经网络图为例子,来搭建对比看看正常情况搭建神经网络和使用Sequential搭建神经网络的区别,以及搭建神经网络中一些要注意的点。
    在这里插入图片描述

    正常情况下搭建神经网络

    搭建神经网络代码:

    import torch
    from torch import nn
    from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
    
    
    class Booze(nn.Module):
    
        def __init__(self):
            super(Booze, self).__init__()
            # 1.根据网络图搭建网络的时候,有些参数网络图上没给,是需要自己去计算的,像是padding,stride等等
            self.conv1 = Conv2d(3,32,5,padding=2)
            self.maxpool1 = MaxPool2d(2)
            self.conv2 = Conv2d(32,32,5,padding=2)
            self.maxpool2 = MaxPool2d(2)
            self.conv3 = Conv2d(32,64,5,padding=2)
            self.maxpool3 = MaxPool2d(2)
            self.flatten = Flatten()
            # 2.设置这个线性层的时候in_feature和out_feature可能也需要自己算,这个in_feature也可以通过打印flatten来查看
            self.linear1 = Linear(1024,64)
            self.linear2 = Linear(64,10)
    
    
        def forward(self,x):
            x = self.conv1(x)
            x = self.maxpool1(x)
            x = self.conv2(x)
            x = self.maxpool2(x)
            x = self.conv3(x)
            x = self.maxpool3(x)
            x = self.flatten(x)
            x = self.linear1(x)
            x = self.linear2(x)
            return x
    
    obj = Booze()
    print(obj)
    '''3.对网络结构进行一个简单的检验'''
    input = torch.ones((64,3,32,32))
    output = obj(input)
    print(output.shape)
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    上述代码中有一些要注意的点,需要单独的拿出来讲讲。
    1. 根据网络图搭建网络的时候,有些参数网络图上没给,是需要自己去计算的,像是padding,stride等等
    像是搭建第一个卷积层的时候,就需要自己去计算padding和stride。那么如何计算呢?这个时候我们就要用到官方文档提供的计算公式了。
    在这里插入图片描述
    2.搭建这个线性层的时候in_feature可能也需要自己算,这个in_feature也可以通过打印flatten来查看

    torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)
    
    • 1

    Flatten可以通过官方文档中的介绍来使用。

    # (batch_size,channels,H,W)=(32, 1, 5, 5)
    input = torch.randn(32, 1, 5, 5)
    # With default parameters
    m = nn.Flatten()
    output = m(input)
    output.size()
    # torch.Size([32, 25])  batch_size=32
    # With non-default parameters
    m = nn.Flatten(0, 2)
    output = m(input)
    output.size()
    # torch.Size([160, 5])  batch_size=160 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    使用Sequential搭建神经网络

    搭建神经网络代码:

    import torch
    from torch import nn
    from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
    from torch.utils.tensorboard import SummaryWriter
    
    
    class Booze(nn.Module):
    
        def __init__(self):
            super(Booze, self).__init__()
            self.model1 = Sequential(
                Conv2d(3, 32, 5, padding=2),
                MaxPool2d(2),
                Conv2d(32, 32, 5, padding=2),
                MaxPool2d(2),
                Conv2d(32, 64, 5, padding=2),
                MaxPool2d(2),
                Flatten(),
                Linear(1024, 64),
                Linear(64, 10)
            )
    
        def forward(self,x):
            x = self.model1(x)
            return x
    
    obj = Booze()
    print(obj)
    '''对网络结构进行一个简单的检验'''
    input = torch.ones((64,3,32,32))
    output = obj(input)
    print(output.shape)
    
    '''对网络模型进行可视化'''
    writer = SummaryWriter("logs")
    writer.add_graph(obj,input)
    writer.close()
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    上述代码中也有一些要注意的点,需要单独的拿出来讲讲。
    3.搭建完了网络之后,需要对网络结构进行一个简单的检验

    obj = Booze()
    print(obj)
    '''对网络结构进行一个简单的检验'''
    input = torch.ones((64,3,32,32))
    output = obj(input)
    print(output.shape)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    就像上述代码一样,运行之后不会报错就行。

    4.网络搭建完了之后,是可以使用tensorboard对网络模型进行可视化的

    '''对网络模型进行可视化'''
    writer = SummaryWriter("logs")
    writer.add_graph(obj,input)
    writer.close()
    
    • 1
    • 2
    • 3
    • 4

    这里用到了add_graph这个方法,具体使用方法可以参考官方文档,其实使用方法和add_images和add_scalar差不多。
    显示结果如下:
    在这里插入图片描述

    具体区别

    其实看代码就很容易看出来哈。
    正常情况:

    def __init__(self):
            super(Booze, self).__init__()
            self.conv1 = Conv2d(3,32,5,padding=2)
            self.maxpool1 = MaxPool2d(2)
            self.conv2 = Conv2d(32,32,5,padding=2)
            self.maxpool2 = MaxPool2d(2)
            self.conv3 = Conv2d(32,64,5,padding=2)
            self.maxpool3 = MaxPool2d(2)
            self.flatten = Flatten()
            self.linear1 = Linear(1024,64)
            self.linear2 = Linear(64,10)
    
    
        def forward(self,x):
            x = self.conv1(x)
            x = self.maxpool1(x)
            x = self.conv2(x)
            x = self.maxpool2(x)
            x = self.conv3(x)
            x = self.maxpool3(x)
            x = self.flatten(x)
            x = self.linear1(x)
            x = self.linear2(x)
            return x
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    Sequential搭建:

    class Booze(nn.Module):
    
        def __init__(self):
            super(Booze, self).__init__()
            self.model1 = Sequential(
                Conv2d(3, 32, 5, padding=2),
                MaxPool2d(2),
                Conv2d(32, 32, 5, padding=2),
                MaxPool2d(2),
                Conv2d(32, 64, 5, padding=2),
                MaxPool2d(2),
                Flatten(),
                Linear(1024, 64),
                Linear(64, 10)
            )
    
        def forward(self,x):
            x = self.model1(x)
            return x
    ```
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    如何找到一个靠谱的开发平台?看看低代码老厂商
    Python常用函数中NumPy的使用教程
    详解vue3的ref和reactive
    数据分析图片绘制咨询
    卷积神经网络数学建模,常见卷积神经网络模型
    华为云云耀云服务器L实例评测|在Redis的Docker容器中安装BloomFilter & 在Spring中使用Redis插件版的布隆过滤器
    c#设计模式-行为型模式 之 解释器模式
    [附源码]java毕业设计高校流动党员信息管理系统
    天软特色因子看板(2023.10 第04期)
    【机器学习】K-Means聚类的执行过程?优缺点?有哪些改进的模型?
  • 原文地址:https://blog.csdn.net/booze_/article/details/125470872