搭建小实战及Sequential
搭建小实战
torch.nn.Sequential
是 PyTorch 中用于顺序组合多个神经网络层(模块)的容器。它使得将多个层组合成一个单一模块变得简单和直观,这样整个容器可以被当作一个单独的模块来处理。
主要特点
-
顺序组合:在
torch.nn.Sequential
中,模块按照它们传递给构造函数的顺序进行添加。这意味着输入数据会首先传递给第一个模块,然后其输出会作为下一个模块的输入,依此类推,直到最后一个模块的输出作为整个Sequential
的输出。 -
灵活性:
torch.nn.Sequential
可以接受一个普通的模块列表或一个OrderedDict
,其中键是模块的名称,值是模块本身。使用OrderedDict
可以在后续更容易地通过名称访问特定模块。 -
模块注册:添加到
Sequential
中的每个模块都会被注册为其子模块。这意味着当对Sequential
执行某些操作时(如将模型移动到GPU上),这些操作会自动应用到它包含的所有子模块上。
与 torch.nn.ModuleList
的区别
torch.nn.ModuleList
仅仅是一个存储模块的列表,它不提供模块之间的连接。而Sequential
则确保了模块之间的顺序连接,使得输入数据可以顺序地通过每个模块。- 使用
ModuleList
时,你需要手动管理数据在每个模块之间的传递。而使用Sequential
,这一过程是自动的。
示例
以下两个示例展示了如何使用 torch.nn.Sequential
来创建一个简单的神经网络模型:
- 使用普通列表:
import torch.nn as nn
model = nn.Sequential(
nn.Conv2d(1, 20, 5),
nn.ReLU(),
nn.Conv2d(20, 64, 5),
nn.ReLU()
)
- 使用
OrderedDict
:
import torch.nn as nn
from collections import OrderedDict
model = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1, 20, 5)),
('relu1', nn.ReLU()),
('conv2', nn.Conv2d(20, 64, 5)),
('relu2', nn.ReLU())
]))
在这两个示例中,模型都包含了两个卷积层和两个ReLU激活函数,它们按照顺序连接。输入数据会首先经过第一个卷积层,然后是ReLU,接着是第二个卷积层,最后是另一个ReLU。
append
方法
append
方法允许你在 Sequential
的末尾添加一个新的模块。例如:
import torch.nn as nn
model = nn.Sequential(nn.Conv2d(1, 20, 5))
model.append(nn.ReLU()) # 添加一个新的ReLU层
这样,你就可以动态地向现有的 Sequential
模型中添加新的层。
搭建简单的网络模型
简单的网络实现:
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear
class Test(nn.Module):
def __init__(self):
super(Test,self).__init__()
self.conv1 = Conv2d(3,32,5,padding=2)
#torch.nn.Conv2d(in_channels, out_channels,
# kernel_size, stride=1, padding=0, dilation=1,
# groups=1, bias=True, padding_mode='zeros',
# device=None, dtype=None)
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,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
test = Test()
print(test)
input = torch.ones(64,3,32,32)
output = test(input)
print(output.shape)
结果为:
C:\Anaconda3\envs\pytorch_test\python.exe H:\Python\Test\nn_seq.py
Test(
(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)
)
torch.Size([64, 10])
进程已结束,退出代码0
搭建实战Sequential
import torch
from tensorflow.python.keras.saving.saved_model_experimental import sequential
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
class Test(nn.Module):
def __init__(self):
super(Test,self).__init__()
# self.conv1 = Conv2d(3,32,5,padding=2)
#torch.nn.Conv2d(in_channels, out_channels,
# kernel_size, stride=1, padding=0, dilation=1,
# groups=1, bias=True, padding_mode='zeros',
# device=None, dtype=None)
# 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)
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
test = Test()
print(test)
input = torch.ones(64,3,32,32)
output = test(input)
print(output.shape)
结果为,结合还按照步骤进行了表述:
C:\Anaconda3\envs\pytorch_test\python.exe H:\Python\Test\nn_seq.py
Test(
(model1): Sequential(
(0): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(4): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Flatten(start_dim=1, end_dim=-1)
(7): Linear(in_features=1024, out_features=64, bias=True)
(8): Linear(in_features=64, out_features=10, bias=True)
)
)
torch.Size([64, 10])
进程已结束,退出代码0
可视化展示
import torch
# tensorboard --logdir=H:\Python\Test\log_SEQ --host=127.0.0.1 --port=6008
from tensorflow.python.keras.saving.saved_model_experimental import sequential
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter
class Test(nn.Module):
def __init__(self):
super(Test,self).__init__()
# self.conv1 = Conv2d(3,32,5,padding=2)
#torch.nn.Conv2d(in_channels, out_channels,
# kernel_size, stride=1, padding=0, dilation=1,
# groups=1, bias=True, padding_mode='zeros',
# device=None, dtype=None)
# 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)
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
test = Test()
print(test)
input = torch.ones(64,3,32,32)
output = test(input)
print(output.shape)
write = SummaryWriter("log_SEQ")
write.add_graph(test,input)
write.close()
结果为:
C:\Anaconda3\envs\pytorch_test\python.exe H:\Python\Test\nn_seq.py
Test(
(model1): Sequential(
(0): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(4): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Flatten(start_dim=1, end_dim=-1)
(7): Linear(in_features=1024, out_features=64, bias=True)
(8): Linear(in_features=64, out_features=10, bias=True)
)
)
torch.Size([64, 10])
进程已结束,退出代码0