搭建神经网络+sequential
(联邦学习笔记,资料来源于b站小土堆)
sequential:
顺序容器。模块将按照它们在构造函数中传递的顺序添加到它当中。或者,可以传入模块的OrderedDict。Sequential的forward()方法接受任何输入并将其转发到它包含的第一个模块。然后,它将每个后续模块的输出顺序链接为输入,最后返回最后一个模块的输出。(能简化代码)
下面以CIFR10的卷积神经网络搭建模型。
1、有三个卷积层,卷积核均为5x5
2、最大池化层有三个,内核为2x2
3、数据经过 卷积层1—>池化层1—>卷积层2—>池化层2—>卷积层3—>池化层3—>展平(Flatten,用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡。Flatten不影响batch的大小。就是把高纬度的数组按照x轴或者y轴 进行拉伸,变成一维的数组)—>线性层1—>线性层2—>输出
(不用sequential时)
#sequential序列
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear
class Mymodule(nn.Module):
def __init__(self):
super(Mymodule, self).__init__()
self.conv1 = Conv2d(3,32,5,padding=2)
self.maxpool1 = MaxPool2d(2)
self.conv2 = Conv2d(32,32,5,padding=2)
self.maxpool2 = MaxPool2d(2)
self.conv3 = Conv2d(32,64,5,padding=2)
self.maxpool3 = MaxPool2d(2)
self.flatten = Flatten()
#不知道展平后的大小,可以先不要下面的部分,先输出类型看看,然后补充
self.linear1 = Linear(1024,64)
self.linear2 = Linear(64,10)
def forward(self,input):
output = self.conv1(input)
output = self.maxpool1(output)
output = self.conv2(output)
output = self.maxpool2(output)
output = self.conv3(output)
output = self.maxpool3(output)
output = self.flatten(output)
output = self.linear1(output)
output = self.linear2(output)
return output
mymodule = Mymodule()
print(mymodule)
#指定输入图片大小64,3通道,32x32
input = torch.ones(64,3,32,32)
output = mymodule(input)
print(output.shape)
结果:
(使用sequential)
#sequential序列
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter
class Mymodule(nn.Module):
def __init__(self):
super(Mymodule, self).__init__()
self.model = 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,input):
output = self.model(input)
return output
mymodule = Mymodule()
print(mymodule)
#指定输入图片大小64,3通道,32x32
input = torch.ones(64,3,32,32)
output = mymodule(input)
print(output.shape)
#用tensorborad可视化神经网络结构
writter = SummaryWriter("../logs_seq")
writter.add_graph(mymodule,input)
writter.close()
结果:
用tensorboard可视化神经网络结构后,在terminal执行以下操作:
tensorboard --logdir=logs_seq --port=6007
(执行有误的参考tensorboard的使用方法)
结果: