torch.nn.Sequential
簡單介紹
nn.Sequential是一個有序的容器,該類將按照傳入構(gòu)造器的順序,依次創(chuàng)建相應(yīng)的函數(shù),并記錄在Sequential類對象的數(shù)據(jù)結(jié)構(gòu)中,同時以神經(jīng)網(wǎng)絡(luò)模塊為元素的有序字典也可以作為傳入?yún)?shù)。
因此,Sequential可以看成是有多個函數(shù)運算對象,串聯(lián)成的神經(jīng)網(wǎng)絡(luò),其返回的是Module類型的神經(jīng)網(wǎng)絡(luò)對象。
構(gòu)建實例
參數(shù)列表
- 以參數(shù)列表的方式來實例化
print("利用系統(tǒng)提供的神經(jīng)網(wǎng)絡(luò)模型類:Sequential,以參數(shù)列表的方式來實例化神經(jīng)網(wǎng)絡(luò)模型對象")
# A sequential container. Modules will be added to it in the order they are passed in the constructor.
# Example of using Sequential
model_c = nn.Sequential(
nn.Linear(28*28, 32),
nn.ReLU(),
nn.Linear(32, 10),
nn.Softmax(dim=1)
)
print(model_c)
print("\n顯示網(wǎng)絡(luò)模型參數(shù)")
print(model_c.parameters)
print("\n定義神經(jīng)網(wǎng)絡(luò)樣本輸入")
x_input = torch.randn(2, 28, 28, 1)
print(x_input.shape)
print("\n使用神經(jīng)網(wǎng)絡(luò)進行預(yù)測")
y_pred = model.forward(x_input.view(x_input.size()[0],-1))
print(y_pred)
利用系統(tǒng)提供的神經(jīng)網(wǎng)絡(luò)模型類:Sequential,以參數(shù)列表的方式來實例化神經(jīng)網(wǎng)絡(luò)模型對象
Sequential(
(0): Linear(in_features=784, out_features=32, bias=True)
(1): ReLU()
(2): Linear(in_features=32, out_features=10, bias=True)
(3): Softmax(dim=1)
)
顯示網(wǎng)絡(luò)模型參數(shù)
<bound method Module.parameters of Sequential(
(0): Linear(in_features=784, out_features=32, bias=True)
(1): ReLU()
(2): Linear(in_features=32, out_features=10, bias=True)
(3): Softmax(dim=1)
)>
定義神經(jīng)網(wǎng)絡(luò)樣本輸入
torch.Size([2, 28, 28, 1])
使用神經(jīng)網(wǎng)絡(luò)進行預(yù)測
tensor([[-0.1526, 0.0437, -0.1685, 0.0034, -0.0675, 0.0423, 0.2807, 0.0527,
-0.1710, 0.0668],
[-0.1820, 0.0860, 0.0174, 0.0883, 0.2046, -0.1609, 0.0165, -0.2392,
-0.2348, 0.1697]], grad_fn=<AddmmBackward>)
字典
- 以字典的方式實例化
# Example of using Sequential with OrderedDict
print("利用系統(tǒng)提供的神經(jīng)網(wǎng)絡(luò)模型類:Sequential,以字典的方式來實例化神經(jīng)網(wǎng)絡(luò)模型對象")
model = nn.Sequential(OrderedDict([('h1', nn.Linear(28*28, 32)),
('relu1', nn.ReLU()),
('out', nn.Linear(32, 10)),
('softmax', nn.Softmax(dim=1))]))
print(model)
print("\n顯示網(wǎng)絡(luò)模型參數(shù)")
print(model.parameters)
print("\n定義神經(jīng)網(wǎng)絡(luò)樣本輸入")
x_input = torch.randn(2, 28, 28, 1)
print(x_input.shape)
print("\n使用神經(jīng)網(wǎng)絡(luò)進行預(yù)測")
y_pred = model.forward(x_input.view(x_input.size()[0],-1))
print(y_pred)
利用系統(tǒng)提供的神經(jīng)網(wǎng)絡(luò)模型類:Sequential,以字典的方式來實例化神經(jīng)網(wǎng)絡(luò)模型對象
Sequential(
(h1): Linear(in_features=784, out_features=32, bias=True)
(relu1): ReLU()
(out): Linear(in_features=32, out_features=10, bias=True)
(softmax): Softmax(dim=1)
)
顯示網(wǎng)絡(luò)模型參數(shù)
<bound method Module.parameters of Sequential(
(h1): Linear(in_features=784, out_features=32, bias=True)
(relu1): ReLU()
(out): Linear(in_features=32, out_features=10, bias=True)
(softmax): Softmax(dim=1)
)>
定義神經(jīng)網(wǎng)絡(luò)樣本輸入
torch.Size([2, 28, 28, 1])
使用神經(jīng)網(wǎng)絡(luò)進行預(yù)測
tensor([[0.1249, 0.1414, 0.0708, 0.1031, 0.1080, 0.1351, 0.0859, 0.0947, 0.0753,
0.0607],
[0.0982, 0.1102, 0.0929, 0.0855, 0.0848, 0.1076, 0.1077, 0.0949, 0.1153,
0.1029]], grad_fn=<SoftmaxBackward>)
基本操作
- 查看結(jié)構(gòu)
通過打印 Sequential
對象來查看它的結(jié)構(gòu)
print(net)
# Sequential(
# (0): Linear(in_features=20, out_features=10, bias=True)
# (1): ReLU()
# (2): Linear(in_features=10, out_features=5, bias=True)
# )
- 索引
我們可以使用索引來查看其子模塊
print(net[0])
# Linear(in_features=20, out_features=10, bias=True)
print(net[1])
# ReLU()
- 長度
print(len(net))
# 3
- 修改子模塊
net[1] = nn.Sigmoid()
print(net)
# Sequential(
# (0): Linear(in_features=20, out_features=10, bias=True)
# (1): Sigmoid()
# (2): Linear(in_features=10, out_features=5, bias=True)
# )
- 刪除子模塊
del net[2]
print(net)
# Sequential(
# (0): Linear(in_features=20, out_features=10, bias=True)
# (1): Sigmoid()
# )
- 添加子模塊
net.append(nn.Linear(10, 2)) # 均會添加到末尾
print(net)
# Sequential(
# (0): Linear(in_features=20, out_features=10, bias=True)
# (1): Sigmoid()
# (2): Linear(in_features=10, out_features=2, bias=True)
# )
- 遍歷
net = nn.Sequential(
nn.Linear(20, 10),
nn.ReLU(),
nn.Linear(10, 5)
)
for sub_module in net:
print(sub_module)
# Linear(in_features=20, out_features=10, bias=True)
# ReLU()
# Linear(in_features=10, out_features=5, bias=True)
- 嵌套
'''在一個 Sequential 中嵌套兩個 Sequential'''
seq_1 = nn.Sequential(nn.Linear(15, 10), nn.ReLU(), nn.Linear(10, 5))
seq_2 = nn.Sequential(nn.Linear(25, 15), nn.Sigmoid(), nn.Linear(15, 10))
seq_3 = nn.Sequential(seq_1, seq_2)
print(seq_3)
# Sequential(
# (0): Sequential(
# (0): Linear(in_features=15, out_features=10, bias=True)
# (1): ReLU()
# (2): Linear(in_features=10, out_features=5, bias=True)
# )
# (1): Sequential(
# (0): Linear(in_features=25, out_features=15, bias=True)
# (1): Sigmoid()
# (2): Linear(in_features=15, out_features=10, bias=True)
# )
# )
''''使用多級索引進行訪問'''
print(seq_3[1])
# Sequential(
# (0): Linear(in_features=25, out_features=15, bias=True)
# (1): Sigmoid()
# (2): Linear(in_features=15, out_features=10, bias=True)
# )
print(seq_3[0][1])
# ReLU()
'''使用雙重循環(huán)進行遍歷'''
for seq in seq_3:
for module in seq:
print(module)
# Linear(in_features=15, out_features=10, bias=True)
# ReLU()
# Linear(in_features=10, out_features=5, bias=True)
# Linear(in_features=25, out_features=15, bias=True)
# Sigmoid()
# Linear(in_features=15, out_features=10, bias=True)
參考
PyTorch學(xué)習(xí)筆記(六)–Sequential類、參數(shù)管理與GPU_Lareges的博客-CSDN博客_sequential類文章來源:http://www.zghlxwxcb.cn/news/detail-759916.html
[Pytorch系列-30]:神經(jīng)網(wǎng)絡(luò)基礎(chǔ) - torch.nn庫五大基本功能:nn.Parameter、nn.Linear、nn.functioinal、nn.Module、nn.Sequentia_文火冰糖的硅基工坊的博客-CSDN博客_torch庫nn文章來源地址http://www.zghlxwxcb.cn/news/detail-759916.html
到了這里,關(guān)于【torch.nn.Sequential】序列容器的介紹和使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!