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()
相关推荐
强哥之神7 分钟前
Nexa AI发布OmniAudio-2.6B:一款快速的音频语言模型,专为边缘部署设计
人工智能·深度学习·机器学习·语言模型·自然语言处理·音视频·openai
yusaisai大鱼11 分钟前
tensorflow_probability与tensorflow版本依赖关系
人工智能·python·tensorflow
18号房客11 分钟前
一个简单的深度学习模型例程,使用Keras(基于TensorFlow)构建一个卷积神经网络(CNN)来分类MNIST手写数字数据集。
人工智能·深度学习·机器学习·生成对抗网络·语言模型·自然语言处理·tensorflow
Biomamba生信基地15 分钟前
R语言基础| 功效分析
开发语言·python·r语言·医药
神秘的土鸡19 分钟前
神经网络图像隐写术:用AI隐藏信息的艺术
人工智能·深度学习·神经网络
数据分析能量站20 分钟前
神经网络-LeNet
人工智能·深度学习·神经网络·机器学习
Jaly_W28 分钟前
用于航空发动机故障诊断的深度分层排序网络
人工智能·深度学习·故障诊断·航空发动机
CodeClimb30 分钟前
【华为OD-E卷-木板 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
小嗷犬30 分钟前
【论文笔记】Cross-lingual few-shot sign language recognition
论文阅读·人工智能·多模态·少样本·手语翻译
夜幕龙37 分钟前
iDP3复现代码数据预处理全流程(二)——vis_dataset.py
人工智能·python·机器人