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()
相关推荐
agicall.com10 分钟前
座机通话双方语音分离技术解决方案详解
人工智能·语音识别·信创电话助手·座机语音转文字·固话座机录音转文字
AI机器学习算法13 分钟前
《动手学深度学习PyTorch版》笔记
人工智能·学习·机器学习
贺一航【Niki】22 分钟前
【学习笔记】杂乱知识
笔记·学习
Goboy26 分钟前
「我的第一次移动端 AI 办公」TRAE SOLO 三端联动, 通勤路上就把活干了,这设计,老罗看了都想当场退役
人工智能·ai编程·trae
qq_4523962332 分钟前
第二十篇:《UI自动化测试的未来:AI驱动的智能测试与低代码平台》
人工智能·低代码·ui
视觉&物联智能1 小时前
【杂谈】-人工智能风险文化对组织决策的深远影响
人工智能·安全·ai·agi
白雪茫茫1 小时前
监督学习、半监督学习、无监督学习算法详解
python·学习·算法·ai
β添砖java1 小时前
深度学习(12)Kaggle房价竞赛
人工智能·深度学习
冬奇Lab1 小时前
RAG 系列(十):混合检索——让召回更全面
人工智能·llm
冬奇Lab1 小时前
一天一个开源项目(第95篇):Claude for Financial Services - Anthropic 官方金融行业 AI 代理套件
人工智能·开源·资讯