小土堆-pytorch框架学习-P22-Sequential搭建实例

一个序列模型,按照顺序到层中,比如官方案例👇

# Using Sequential to create a small model. When `model` is run,
# input will first be passed to `Conv2d(1,20,5)`. The output of
# `Conv2d(1,20,5)` will be used as the input to the first
# `ReLU`; the output of the first `ReLU` will become the input
# for `Conv2d(20,64,5)`. Finally, the output of
# `Conv2d(20,64,5)` will be used as input to the second `ReLU`
model = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )

# Using Sequential with OrderedDict. This is functionally the
# same as the above code
model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))

先有输入通道为1,输出通道为20,卷积核为5的卷积层,接着接一层激活函数,再来一层输入通道为20,输出通道为64,卷积核为5的卷积层,最后接激活函数。
$$
H_{out} = \left \lfloor \dfrac{H_{in}+2\times padding[0]-(kernel_size[0]-1)-1}{stride[0]}+1 \right \rfloor

\
W_{out} = \left \lfloor \dfrac{W_{in}+2\times padding[1]-(kernel_size[1]-1)-1}{stride[1]}+1 \right \rfloor
$$
这里的 H i n , W i n H_{in},W_{in} Hin,Win说的是图片的宽和高,stride和padding是未知,需要求,dilation是默认1。

以CIFAR10作为模型来处理,网络结构是这样的👇

image-20230705175645243

经过8步处理,分别是:

  1. 卷积
  2. 最大池化
  3. 卷积
  4. 最大池化
  5. 卷积
  6. 最大池化
  7. 展开
  8. 全连接

案例代码👇

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.conv1 = nn.Conv2d(3,32,5,padding = 2)
        self.maxpool1 = nn.MaxPool2d(kernel_size = 2)
        self.conv2 = nn.Conv2d(16, 16 ,5,padding = 2)
        self.maxpool2 = nn.MaxPool2d(kernel_size = 2)
        self.conv3 = nn.Conv2d(8 ,8 , 5, padding = 2)
        self.maxpool3 = nn.MaxPool2d(2)
        self.flatten = nn.Flatten()
        # self.linear = nn.Linear()
    def forward(self , input):
        input = self.conv1(input)
        input = self.maxpool1(input)
        input = self.conv2(input)
        input = self.maxpool2(input)
        input = self.conv3(input)
        input = self.maxpool3(input)
        input = self.flatten(input)



tudui = Tudui()
print(tudui)
#64是batch_size ,一次传入64张图片
#3,32,32是3通道,32×32的图片大小,看最上面那个网络模型架构图就行
input = torch.ones((64,3,32,32))
output = tudui(input)
print(output.shape)
#打印完输出的形状后,就知道要传入linear层有多少特征

如果不知道要传入线性层多少个特征,可以在flatten层据停止,看输出是多少。

image-20230706092345689

接着在flatten层后添加线性层,有两层线性层,一层是将1024变换为64,一层是将64变换为10。

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.conv1 = nn.Conv2d(3,32,5,padding = 2)
        self.maxpool1 = nn.MaxPool2d(kernel_size = 2)
        self.conv2 = nn.Conv2d(32 , 32 ,5,padding = 2)
        self.maxpool2 = nn.MaxPool2d(kernel_size = 2)
        self.conv3 = nn.Conv2d(32 ,64 , 5, padding = 2)
        self.maxpool3 = nn.MaxPool2d(kernel_size = 2)
        self.flatten = nn.Flatten()
        self.linear1 = nn.Linear(in_features = 1024 , out_features = 64)
        self.linear2 = nn.Linear(in_features = 64, out_features = 10)
    def forward(self , input):
        input = self.conv1(input)
        input = self.maxpool1(input)
        input = self.conv2(input)
        input = self.maxpool2(input)
        input = self.conv3(input)
        input = self.maxpool3(input)
        input = self.flatten(input)
        input = self.linear1(input)
        input = self.linear2(input)
        return input

运行结果如下👇

image-20230706092600067

也可以用tensorboard进行网络模型的可视化。

import torch
import torchvision
from tensorboardX import SummaryWriter
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        # self.conv1 = nn.Conv2d(3,32,5,padding = 2)
        # self.maxpool1 = nn.MaxPool2d(kernel_size = 2)
        # self.conv2 = nn.Conv2d(32 , 32 ,5,padding = 2)
        # self.maxpool2 = nn.MaxPool2d(kernel_size = 2)
        # self.conv3 = nn.Conv2d(32 ,64 , 5, padding = 2)
        # self.maxpool3 = nn.MaxPool2d(kernel_size = 2)
        # self.flatten = nn.Flatten()
        # self.linear1 = nn.Linear(in_features = 1024 , out_features = 64)
        # self.linear2 = nn.Linear(in_features = 64, out_features = 10)
        self.model = nn.Sequential(
            nn.Conv2d(3,32,5,padding = 2),
            nn.MaxPool2d(kernel_size = 2),
            nn.Conv2d(32,32,5,padding = 2),
            nn.MaxPool2d(kernel_size = 2),
            nn.Conv2d(32 , 64 , 5 ,padding = 2),
            nn.MaxPool2d(kernel_size = 2),
            nn.Flatten(),
            nn.Linear(in_features = 1024 ,
                    out_features = 64),
            nn.Linear(in_features = 64 ,
                    out_features = 10)
        )
    def forward(self , input):
        # input = self.conv1(input)
        # input = self.maxpool1(input)
        # input = self.conv2(input)
        # input = self.maxpool2(input)
        # input = self.conv3(input)
        # input = self.maxpool3(input)
        # input = self.flatten(input)
        # input = self.linear1(input)
        # input = self.linear2(input)
        input = self.model(input)
        return input


tudui = Tudui()
print(tudui)

input = torch.ones((64, 3,32,32))
output = tudui(input)

print(output.shape)
#可视化网络结构
writer = SummaryWriter("tb_logs/seq")
writer.add_graph(model = tudui , input_to_model = input)
writer.close()

打开网络结构👇

image-20230706095931130

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HelpFireCode

随缘惜缘不攀缘。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值