(十一)神经网络- CIFAR-10的网络模型搭建及Sequential的使用

1.Sequential类的介绍

在这里插入图片描述

2.CIFAR-10神经网络的搭建

2.1 Structure Model

在这里插入图片描述

2.2 Conv2D计算padding、stride公式

  • dilation默认值是1,代表不使用空洞卷积
    在这里插入图片描述

2.3 代码实战

from torch import nn
import torch
from torch.utils.tensorboard import SummaryWriter


class My_Module(nn.Module):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.seq = nn.Sequential(
            nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, padding=2),
            nn.MaxPool2d(kernel_size=2),
            nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, padding=2),
            nn.MaxPool2d(kernel_size=2),
            nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, padding=2),
            nn.MaxPool2d(kernel_size=2),
            nn.Flatten(),
            nn.Linear(in_features=1024, out_features=64),
            nn.Linear(in_features=64, out_features=10)
        )

    def forward(self, x):
        output = self.seq(x)
        return output


my_module = My_Module()
input_tensor = torch.ones((64, 3, 32, 32))
writer = SummaryWriter("logs_seq")
writer.add_graph(model=my_module, input_to_model=input_tensor)  # 用tensorboard绘制计算图
writer.close()

在这里插入图片描述