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()
相关推荐
hangyuekejiGEO1 分钟前
临沂GEO技术服务方案对比分析
大数据·人工智能·python
happyprince4 分钟前
08-PEFT源码阅读-使用指南与实战案例
人工智能·peft
IT_陈寒13 分钟前
Vue的响应式更新把我坑惨了,原来是这个原因
前端·人工智能·后端
weixin_5498083614 分钟前
AI招聘的下半场:当“匹配”从技巧变成组织能力
人工智能
武子康15 分钟前
调查研究-217 Fortress:当浏览器 Agent 开始被网站识别,开源 Chromium 反拦截引擎来了
人工智能·爬虫·agent
大龄牛码18 分钟前
AI大模型在金融领域的垂直应用实践:从信息检索到自动化报告
人工智能·金融·自动化
小巧的砖头20 分钟前
使用SVN+CruiseControl+ANT实现持续集成之一
学习·算法
新知图书21 分钟前
多模态智能体开发的核心挑战与解决方案
人工智能·agent·多模态·ai agent·智能体·langgraph
ACP广源盛1392462567323 分钟前
GSV2231@ACP# 增强型 8K MST 多屏扩展芯片
大数据·人工智能·分布式·嵌入式硬件
xin_yao_xin29 分钟前
Conda 环境的 CUDA PATH 配置指南
开发语言·python·conda·cuda