Pytorch个人学习记录总结 08

目录

神经网络-搭建小实战和Sequential的使用

版本1------未用Sequential

版本2------用Sequential


神经网络-搭建小实战和Sequential的使用

  1. torch.nn.Sequential官方文档地址,模块将按照它们在构造函数中传递的顺序添加。
  2. 代码实现的是下图:

版本1------未用Sequential

python 复制代码
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        # 3,32,32 ---> 32,32,32
        self.conv1 = Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2)
        # 32,32,32 ---> 32,16,16
        self.maxpool1 = MaxPool2d(kernel_size=2, stride=2)
        # 32,16,16 ---> 32,16,16
        self.conv2 = Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2)
        # 32,16,16 ---> 32,8,8
        self.maxpool2 = MaxPool2d(kernel_size=2, stride=2)
        # 32,8,8 ---> 64,8,8
        self.conv3 = Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2)
        # 64,8,8 ---> 64,4,4
        self.maxpool3 = MaxPool2d(kernel_size=2, stride=2)
        # 64,4,4 ---> 1024
        self.flatten = Flatten()  # 因为start_dim默认为1,所以可不再另外设置
        # 1024 ---> 64
        self.linear1 = Linear(1024, 64)
        # 64 ---> 10
        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


model = Model()
print(model)

input = torch.ones((64, 3, 32, 32))
out = model(input)
print(out.shape)	# torch.Size([64, 10])

版本2------用Sequential

代码更简洁,而且会给每层自动从0开始编序。

python 复制代码
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.model = Sequential(
            Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2),
            MaxPool2d(kernel_size=2, stride=2),
            Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2),
            MaxPool2d(kernel_size=2, stride=2),
            Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2),
            MaxPool2d(kernel_size=2, stride=2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        return self.model(x)


model = Model()
print(model)

input = torch.ones((64, 3, 32, 32))
out = model(input)
print(out.shape)	# torch.Size([64, 10])

在代码最末尾加上writer.add_gragh(model, input)就可看到模型计算图,可放大查看。

python 复制代码
writer = SummaryWriter('./logs/Seq')
writer.add_graph(model, input)
writer.close()
相关推荐
小北方城市网5 分钟前
Spring Cloud 服务治理实战:构建高可用微服务体系
spring boot·python·rabbitmq·java-rabbitmq·数据库架构
WJSKad123510 分钟前
基于改进YOLO11的超市商品与电子设备多类别目标检测方法C3k2-ConvAttn
人工智能·目标检测·计算机视觉
wangmengxxw24 分钟前
SpringAi-mcp高德
人工智能·高德·springai·mcp
写代码的【黑咖啡】24 分钟前
Python中的Statsmodels:统计建模与假设检验
开发语言·python
ooo-p28 分钟前
FPGA学习篇——Verilog学习之“流水灯”
学习·fpga开发
程序员杰哥29 分钟前
Pytest自动化测试框架实战
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
丝瓜蛋汤30 分钟前
Proof of the contraction mapping theorem
人工智能·算法
觉醒大王34 分钟前
如何整理文献阅读笔记? (精读与泛读)
前端·css·笔记·深度学习·自然语言处理·html·学习方法
weixin_4331793336 分钟前
python - 函数 function
开发语言·python
renhongxia138 分钟前
数字孪生国内外发展现状,数字孪生技术在工程项目上的应用情况及效益分析
人工智能·深度学习·机器学习·语言模型·制造