7-pytorch-CIFAR10模型搭建

b站小土堆pytorch教程学习笔记

复现CIFAR10网络结构

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


class Han(nn.Module):
    def __init__(self):
        super(Han, self).__init__()
        self.conv1=Conv2d(in_channels=3,out_channels=32,
                          kernel_size=5,padding=2,stride=1)
        self.maxpool1=MaxPool2d(kernel_size=2)
        self.conv2=Conv2d(in_channels=32,out_channels=32,
                          kernel_size=5,padding=2,stride=1)
        self.maxpool2=MaxPool2d(kernel_size=2)
        self.conv3=Conv2d(in_channels=32,out_channels=64,
                          kernel_size=5,padding=2,stride=1)
        self.maxpool3=MaxPool2d(kernel_size=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

han=Han()
print(han)

Han(
(conv1): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(maxpool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(maxpool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv3): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(maxpool3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(flatten): Flatten(start_dim=1, end_dim=-1)
(linear1): Linear(in_features=1024, out_features=64, bias=True)
(linear2): Linear(in_features=64, out_features=10, bias=True)
)

检查网络正确性:

假定输入

python 复制代码
#测试网络结构正确性
input=torch.ones((64,3,32,32))#产生都是1的输入
output=han(input)
print(output)

tensor([[ 0.0063, -0.0712, 0.0809, -0.0330, -0.1598, -0.0949, 0.0303, 0.0632,
0.0453, 0.0606]...

Sequential:

python 复制代码
class Han(nn.Module):
    def __init__(self):
        super(Han, self).__init__()
        # self.conv1=Conv2d(in_channels=3,out_channels=32,
        #                   kernel_size=5,padding=2,stride=1)
        # self.maxpool1=MaxPool2d(kernel_size=2)
        # self.conv2=Conv2d(in_channels=32,out_channels=32,
        #                   kernel_size=5,padding=2,stride=1)
        # self.maxpool2=MaxPool2d(kernel_size=2)
        # self.conv3=Conv2d(in_channels=32,out_channels=64,
        #                   kernel_size=5,padding=2,stride=1)
        # self.maxpool3=MaxPool2d(kernel_size=2)
        # self.flatten=Flatten()
        # self.linear1=Linear(1024,64)
        # self.linear2=Linear(64,10)

        self.model1=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
han=Han()
# print(han)
#测试网络结构正确性
input=torch.ones((64,3,32,32))#产生都是1的输入
output=han(input)
# print(output)

writer=SummaryWriter('logs/seq')
writer.add_graph(han,input)
writer.close()
相关推荐
小han的日常9 分钟前
pycharm分支提交操作
python·pycharm
矢量赛奇13 分钟前
比ChatGPT更酷的AI工具
人工智能·ai·ai写作·视频
KuaFuAI21 分钟前
微软推出的AI无代码编程微应用平台GitHub Spark和国产AI原生无代码工具CodeFlying比到底咋样?
人工智能·github·aigc·ai编程·codeflying·github spark·自然语言开发软件
明月清风徐徐28 分钟前
Scrapy爬取豆瓣电影Top250排行榜
python·selenium·scrapy
theLuckyLong29 分钟前
SpringBoot后端解决跨域问题
spring boot·后端·python
Make_magic30 分钟前
Git学习教程(更新中)
大数据·人工智能·git·elasticsearch·计算机视觉
Yongqiang Cheng32 分钟前
Python operator.itemgetter(item) and operator.itemgetter(*items)
python·operator·itemgetter
shelly聊AI35 分钟前
语音识别原理:AI 是如何听懂人类声音的
人工智能·语音识别
MavenTalk35 分钟前
Move开发语言在区块链的开发与应用
开发语言·python·rust·区块链·solidity·move
源于花海38 分钟前
论文学习(四) | 基于数据驱动的锂离子电池健康状态估计和剩余使用寿命预测
论文阅读·人工智能·学习·论文笔记