Pytorch学习--神经网络--搭建小实战(手撕CIFAR 10 model structure)和 Sequential 的使用

一、Sequential 的使用方法

在手撕代码中进一步体现
torch.nn.Sequential

二、手撕 CIFAR 10 model structure

手撕代码:

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


class Mary(nn.Module):
    def __init__(self):
        super(Mary,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
Yorelee = Mary()
print(Yorelee)
# 检测
input = torch.ones((64,3,32,32))
output = Yorelee(input)
print(output.shape)  #如果是[64,10]即为正确

#用Tensorboard去检测
writer = SummaryWriter("logs")
writer.add_graph(Yorelee,input)
writer.close()

Tensorboard 输出:

使用nn.Sequential的代码:

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


class Mary(nn.Module):
    def __init__(self):
        super(Mary,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)
        self.model1 = nn.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.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)
        x = self.model1(x)
        return x
Yorelee = Mary()
print(Yorelee)
# 检测
input = torch.ones((64,3,32,32))
output = Yorelee(input)
print(output.shape)  #如果是[64,10]即为正确

#用Tensorboard去检测
writer = SummaryWriter("logs")
writer.add_graph(Yorelee,input)
writer.close()
相关推荐
数商思语行1 分钟前
从BA、产品、实施或开发转做FDE,先补哪种能力
人工智能·ai·供应链·商业分析·ontology·本体·fde
代码简单说2 分钟前
GPT Image 2.5 API 调用教程:Node.js 实现图片生成与编辑
人工智能
零基础1238 分钟前
VoiceStudio 开源项目深度解析:特性、对比与实战测试
人工智能·经验分享·python·开源
故七月12 分钟前
本地生活 GEO 内容质量风控体系构建 —— 基于陕西金贝儿母婴家政项目,万域智瞰 GEO 实践
人工智能·生活
2601_9628857212 分钟前
如何用 Python 做均线粘合选股?(多头发散前的变盘候选)
开发语言·python
zhangzeyuaaa14 分钟前
Ruby 方法参数完全指南:默认值、可变参数与关键字参数
前端·python·ruby
老马识码17 分钟前
Harness:Agent 运行时架构
人工智能
林伽一17 分钟前
决策模型接口趋同、缓存按字节计价,AI 技术栈的两处底层改写| 2026年10月04日
人工智能·缓存
小小张说故事20 分钟前
pytest 写了用例却不跑、fixture 不生效、参数化全失效?9 个高频坑对照表
python
vilya21 分钟前
我怎么给手机 GUI Agent 做双通道感知:无障碍树为主,投屏像素兜底
android·人工智能