国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【torch.nn.Sequential】序列容器的介紹和使用

這篇具有很好參考價值的文章主要介紹了【torch.nn.Sequential】序列容器的介紹和使用。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

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ò)對象。
torch.nn.sequential,深度學(xué)習(xí)框架,python,深度學(xué)習(xí),人工智能

構(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類

[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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • pytorch nn.ModuleList和nn.Sequential的用法筆記

    有部分內(nèi)容轉(zhuǎn)自: pytorch小記:nn.ModuleList和nn.Sequential的用法以及區(qū)別_慕思侶的博客-CSDN博客 但是有部分內(nèi)容做了修改調(diào)整, 在構(gòu)建網(wǎng)絡(luò)的時候,pytorch有一些基礎(chǔ)概念很重要,比如nn.Module,nn.ModuleList,nn.Sequential,這些類我們稱為為容器(containers),可參考containers。本文中

    2024年02月13日
    瀏覽(24)
  • Pytorch深度學(xué)習(xí)-----神經(jīng)網(wǎng)絡(luò)之Sequential的詳細使用及實戰(zhàn)詳解

    PyTorch深度學(xué)習(xí)——Anaconda和PyTorch安裝 Pytorch深度學(xué)習(xí)-----數(shù)據(jù)模塊Dataset類 Pytorch深度學(xué)習(xí)------TensorBoard的使用 Pytorch深度學(xué)習(xí)------Torchvision中Transforms的使用(ToTensor,Normalize,Resize ,Compose,RandomCrop) Pytorch深度學(xué)習(xí)------torchvision中dataset數(shù)據(jù)集的使用(CIFAR10) Pytorch深度學(xué)習(xí)--

    2024年02月14日
    瀏覽(26)
  • 深度學(xué)習(xí)之pytorch 中 torch.nn介紹

    pytorch 中必用的包就是 torch.nn,torch.nn 中按照功能分,主要如下有幾類: 1. Layers(層):包括全連接層、卷積層、池化層等。 2. Activation Functions(激活函數(shù)):包括ReLU、Sigmoid、Tanh等。 3. Loss Functions(損失函數(shù)):包括交叉熵損失、均方誤差等。 4. Optimizers(優(yōu)化器):包括

    2024年02月22日
    瀏覽(27)
  • 搭建小實戰(zhàn)和Sequential的使用

    搭建小實戰(zhàn)和Sequential的使用

    以CIFAR 10 model結(jié)構(gòu)為例搭建網(wǎng)絡(luò) CIFAR 10 model結(jié)構(gòu) torch.nn.Flatten 是PyTorch中的一個模塊,用于將多維的輸入張量轉(zhuǎn)換為一維的輸出張量。它可以被用作神經(jīng)網(wǎng)絡(luò)模型中的一層,用于將輸入張量展平后作為全連接層的輸入。比如輸入張量的形狀是 [10, 3, 32, 32] ,即批大小為 10,通道

    2024年02月12日
    瀏覽(18)
  • 學(xué)習(xí)pytorch13 神經(jīng)網(wǎng)絡(luò)-搭建小實戰(zhàn)&Sequential的使用

    學(xué)習(xí)pytorch13 神經(jīng)網(wǎng)絡(luò)-搭建小實戰(zhàn)&Sequential的使用

    B站小土堆pytorch視頻學(xué)習(xí) https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Sequential sequential 將模型結(jié)構(gòu)組合起來 以逗號分割,按順序執(zhí)行,和compose使用方式類似。 箭頭指向部分還需要一層flatten層,展開輸入shape為一維 tensorboard 展示圖文件, 雙擊每層網(wǎng)絡(luò),可查看層

    2024年02月07日
    瀏覽(22)
  • PyTorch入門學(xué)習(xí)(十二):神經(jīng)網(wǎng)絡(luò)-搭建小實戰(zhàn)和Sequential的使用

    目錄 一、介紹 二、先決條件 三、代碼解釋 一、介紹 在深度學(xué)習(xí)領(lǐng)域,構(gòu)建復(fù)雜的神經(jīng)網(wǎng)絡(luò)模型可能是一項艱巨的任務(wù),尤其是當(dāng)您有許多層和操作需要組織時。幸運的是,PyTorch提供了一個方便的工具,稱為Sequential API,它簡化了神經(jīng)網(wǎng)絡(luò)架構(gòu)的構(gòu)建過程。在本文中,將探

    2024年02月05日
    瀏覽(21)
  • 【機器學(xué)習(xí)】P14 Tensorflow 使用指南 Dense Sequential Tensorflow 實現(xiàn)

    【機器學(xué)習(xí)】P14 Tensorflow 使用指南 Dense Sequential Tensorflow 實現(xiàn)

    有關(guān) Tensorflow/CUDA/cuDNN 安裝,見博客:https://xu-hongduo.blog.csdn.net/article/details/129927665 上圖中包含輸入層、隱藏層、輸出層; 其中輸入層為 layer 0 ,輸入到網(wǎng)絡(luò)中的內(nèi)容為 x ? vec{x} x ; 其中隱藏層有三層, layer 1 , layer 2 , layer 3 ; 其中輸出層為 layer 4 ,輸出內(nèi)容為 a ? [

    2023年04月09日
    瀏覽(45)
  • 機器學(xué)習(xí)&&深度學(xué)習(xí)——torch.nn模塊

    機器學(xué)習(xí)&&深度學(xué)習(xí)——torch.nn模塊

    torch.nn模塊包含著torch已經(jīng)準備好的層,方便使用者調(diào)用構(gòu)建網(wǎng)絡(luò)。 卷積就是 輸入和卷積核之間的內(nèi)積運算 ,如下圖: 容易發(fā)現(xiàn),卷積神經(jīng)網(wǎng)絡(luò)中通過輸入卷積核來進行卷積操作,使輸入單元(圖像或特征映射)和輸出單元(特征映射)之間的連接時稀疏的,能夠減少需要

    2024年02月15日
    瀏覽(24)
  • 【LangChain】順序(Sequential)

    基礎(chǔ) 【LangChain】LLM 【LangChain】路由(Router) 【LangChain】順序(Sequential) 我們調(diào)用語言模型后的下一步,一般都是對語言模型進行一系列調(diào)用。也就是我們想要獲取一個調(diào)用的輸出并將其用作另一個調(diào)用的輸入。 在本筆記本中,我們將通過一些示例來說明如何使用順序鏈來執(zhí)行此

    2024年02月13日
    瀏覽(18)
  • Circuits--Sequential--Registers_1

    1.4-bit shift register 2.Left/right rotator 3.Left / right arithmetic? 4.5-bit LFSR

    2024年04月17日
    瀏覽(16)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包