当前位置:   article > 正文

7-pytorch-CIFAR10模型搭建

7-pytorch-CIFAR10模型搭建

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

复现CIFAR10网络结构
在这里插入图片描述

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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)
)

检查网络正确性:
假定输入

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

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

Sequential:

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/149438
推荐阅读
相关标签
  

闽ICP备14008679号