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