亚洲精品久久久久久久久久久,亚洲国产精品一区二区制服,亚洲精品午夜精品,国产成人精品综合在线观看,最近2019中文字幕一页二页

電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>PyTorch教程6.1之層和模塊

PyTorch教程6.1之層和模塊

2023-06-05 | pdf | 0.20 MB | 次下載 | 免費(fèi)

資料介紹

當(dāng)我們第一次引入神經(jīng)網(wǎng)絡(luò)時(shí),我們專(zhuān)注于具有單一輸出的線性模型。在這里,整個(gè)模型只包含一個(gè)神經(jīng)元。請(qǐng)注意,單個(gè)神經(jīng)元 (i) 接受一組輸入;(ii) 生成相應(yīng)的標(biāo)量輸出;(iii) 有一組相關(guān)參數(shù),可以更新這些參數(shù)以?xún)?yōu)化一些感興趣的目標(biāo)函數(shù)。然后,一旦我們開(kāi)始考慮具有多個(gè)輸出的網(wǎng)絡(luò),我們就利用矢量化算法來(lái)表征整個(gè)神經(jīng)元層。就像單個(gè)神經(jīng)元一樣,層 (i) 采用一組輸入,(ii) 生成相應(yīng)的輸出,并且 (iii) 由一組可調(diào)參數(shù)描述。當(dāng)我們進(jìn)行 softmax 回歸時(shí),單層本身就是模型。然而,即使我們隨后引入了 MLP,

有趣的是,對(duì)于 MLP,整個(gè)模型及其組成層都共享這種結(jié)構(gòu)。整個(gè)模型接受原始輸入(特征),生成輸出(預(yù)測(cè)),并擁有參數(shù)(來(lái)自所有構(gòu)成層的組合參數(shù))。同樣,每個(gè)單獨(dú)的層攝取輸入(由前一層提供)生成輸出(后續(xù)層的輸入),并擁有一組可調(diào)參數(shù),這些參數(shù)根據(jù)從后續(xù)層向后流動(dòng)的信號(hào)進(jìn)行更新。

雖然您可能認(rèn)為神經(jīng)元、層和模型為我們提供了足夠的抽象來(lái)開(kāi)展我們的業(yè)務(wù),但事實(shí)證明,我們經(jīng)常發(fā)現(xiàn)談?wù)摫葐蝹€(gè)層大但比整個(gè)模型小的組件很方便。例如,在計(jì)算機(jī)視覺(jué)領(lǐng)域廣受歡迎的 ResNet-152 架構(gòu)擁有數(shù)百層。這些層由層組的重復(fù)圖案組成。一次一層地實(shí)現(xiàn)這樣的網(wǎng)絡(luò)會(huì)變得乏味。這種擔(dān)憂不僅僅是假設(shè)——這樣的設(shè)計(jì)模式在實(shí)踐中很常見(jiàn)。上面提到的 ResNet 架構(gòu)贏得了 2015 年 ImageNet 和 COCO 計(jì)算機(jī)視覺(jué)識(shí)別和檢測(cè)競(jìng)賽He et al. , 2016并且仍然是許多視覺(jué)任務(wù)的首選架構(gòu)。層以各種重復(fù)模式排列的類(lèi)似架構(gòu)現(xiàn)在在其他領(lǐng)域無(wú)處不在,包括自然語(yǔ)言處理和語(yǔ)音。

為了實(shí)現(xiàn)這些復(fù)雜的網(wǎng)絡(luò),我們引入了神經(jīng)網(wǎng)絡(luò)模塊的概念模塊可以描述單個(gè)層、由多個(gè)層組成的組件或整個(gè)模型本身!使用模塊抽象的一個(gè)好處是它們可以組合成更大的工件,通常是遞歸的。如圖 6.1.1所示。通過(guò)定義代碼以按需生成任意復(fù)雜度的模塊,我們可以編寫(xiě)出奇緊湊的代碼并仍然實(shí)現(xiàn)復(fù)雜的神經(jīng)網(wǎng)絡(luò)。

https://file.elecfans.com/web2/M00/A9/C7/poYBAGR9NP2AcRNaAAJd7roQfBs959.svg

圖 6.1.1多層組合成模塊,形成更大模型的重復(fù)模式。

編程的角度來(lái)看,模塊由類(lèi)表示。它的任何子類(lèi)都必須定義一個(gè)前向傳播方法,將其輸入轉(zhuǎn)換為輸出,并且必須存儲(chǔ)任何必要的參數(shù)。請(qǐng)注意,某些模塊根本不需要任何參數(shù)。最后,為了計(jì)算梯度,模塊必須具有反向傳播方法。幸運(yùn)的是,由于自動(dòng)微分(在2.5 節(jié)中介紹)在定義我們自己的模塊時(shí)提供了一些幕后魔法,我們只需要擔(dān)心參數(shù)和前向傳播方法。

import torch
from torch import nn
from torch.nn import functional as F
from mxnet import np, npx
from mxnet.gluon import nn

npx.set_np()
from typing import List
import jax
from flax import linen as nn
from jax import numpy as jnp
from d2l import jax as d2l
No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
import tensorflow as tf

首先,我們重新審視用于實(shí)現(xiàn) MLP 的代碼(第 5.1 節(jié))。以下代碼生成一個(gè)網(wǎng)絡(luò),該網(wǎng)絡(luò)具有一個(gè)具有 256 個(gè)單元和 ReLU 激活的全連接隱藏層,后跟一個(gè)具有 10 個(gè)單元的全連接輸出層(無(wú)激活函數(shù))。

net = nn.Sequential(nn.LazyLinear(256), nn.ReLU(), nn.LazyLinear(10))

X = torch.rand(2, 20)
net(X).shape
torch.Size([2, 10])

在這個(gè)例子中,我們通過(guò)實(shí)例化一個(gè) 來(lái)構(gòu)造我們的模型 nn.Sequential,層按照它們應(yīng)該被執(zhí)行的順序作為參數(shù)傳遞。簡(jiǎn)而言之,nn.Sequential定義了一種特殊的Module,在 PyTorch 中呈現(xiàn)模塊的類(lèi)。它維護(hù)一個(gè)有序的 constituent 列表Module。請(qǐng)注意,兩個(gè)完全連接的層中的每一個(gè)都是該類(lèi)的一個(gè)實(shí)例,Linear該類(lèi)本身是 的子類(lèi)Module。前向傳播 ( forward) 方法也非常簡(jiǎn)單:它將列表中的每個(gè)模塊鏈接在一起,將每個(gè)模塊的輸出作為輸入傳遞給下一個(gè)模塊。請(qǐng)注意,到目前為止,我們一直在通過(guò)構(gòu)造調(diào)用我們的模型 net(X)以獲得它們的輸出。這實(shí)際上只是 net.__call__(X).

net = nn.Sequential()
net.add(nn.Dense(256, activation='relu'))
net.add(nn.Dense(10))
net.initialize()

X = np.random.uniform(size=(2, 20))
net(X).shape
(2, 10)

In this example, we constructed our model by instantiating an nn.Sequential, assigning the returned object to the net variable. Next, we repeatedly call its add method, appending layers in the order that they should be executed. In short, nn.Sequential defines a special kind of Block, the class that presents a module in Gluon. It maintains an ordered list of constituent Blocks. The add method simply facilitates the addition of each successive Block to the list. Note that each layer is an instance of the Dense class which is itself a subclass of Block. The forward propagation (forward) method is also remarkably simple: it chains each Block in the list together, passing the output of each as input to the next. Note that until now, we have been invoking our models via the construction net(X) to obtain their outputs. This is actually just shorthand for net.forward(X), a slick Python trick achieved via the Block class’s __call__ method.

net = nn.Sequential([nn.Dense(256), nn.relu, nn.Dense(10)])

# get_key is a d2l saved function returning jax.random.PRNGKey(random_seed)
X = jax.random.uniform(d2l.get_key(), (2, 20))
params = net.init(d2l.get_key(), X)
net.apply(params, X).shape
(2, 10)
net = tf.keras.models.Sequential([
  tf.keras.layers.Dense(256, activation=tf.nn.relu),
  tf.keras.layers.Dense(10),
])

X = tf.random.uniform((2, 20))
net(X).shape
TensorShape([2, 10])

In this example, we constructed our model by instantiating an keras.models.Sequential, with layers in the order that they should be executed passed as arguments. In short, Sequential defines a special kind of keras.Model, the class that presents a module in Keras. It maintains an ordered list of constituent Models. Note that each of the two fully connected layers is an instance of the Dense class which is itself a subclass of Model. The forward propagation (call) method is also remarkably simple: it chains each module in the list together, passing the output of each as input to the next. Note that until now, we have been invoking our models via the construction net(X) to obtain their outputs. This is actually just shorthand for net.call(X), a slick Python trick achieved via the module class’s __call__ method.

6.1.1. 自定義模塊

也許培養(yǎng)關(guān)于模塊如何工作的直覺(jué)的最簡(jiǎn)單方法是我們自己實(shí)現(xiàn)一個(gè)。在我們實(shí)現(xiàn)自己的自定義模塊之前,我們先簡(jiǎn)單總結(jié)一下每個(gè)模塊必須提供的基本功能:

  1. 攝取輸入數(shù)據(jù)作為其前向傳播方法的參數(shù)。

  2. 通過(guò)讓前向傳播方法返回一個(gè)值來(lái)生成輸出。請(qǐng)注意,輸出可能具有與輸入不同的形狀。例如,我們上面模型中的第一個(gè)全連接層接收任意維度的輸入,但返回 256 維度的輸出。

  3. 計(jì)算其輸出相對(duì)于其輸入的梯度,可以通過(guò)其反向傳播方法訪問(wèn)。通常這會(huì)自動(dòng)發(fā)生。

  4. 存儲(chǔ)并提供對(duì)執(zhí)行前向傳播計(jì)算所需的那些參數(shù)的訪問(wèn)。

  5. 根據(jù)需要初始化模型參數(shù)。

在下面的代碼片段中,我們從頭開(kāi)始編寫(xiě)一個(gè)模塊,對(duì)應(yīng)于一個(gè)包含 256 個(gè)隱藏單元的隱藏層和一個(gè) 10 維輸出層的 MLP。請(qǐng)注意,MLP下面的類(lèi)繼承了代表模塊的類(lèi)。我們將嚴(yán)重依賴(lài)父類(lèi)的方法,僅提供我們自己的構(gòu)造函數(shù)(__init__ Python 中的方法)和前向傳播方法。

class MLP(nn.Module):
  def __init__(self):
    # Call the constructor of the parent class nn.Module to perform
    # the necessary initialization
    super().__init__()
    self.hidden = nn.LazyLinear(256)
    self.out = nn.LazyLinear(10)

  # Define the forward propagation of the model, that is, how to return the
  # required model output based on the input X
  def forward(self, X):
    return self.out(F.relu(self.hidden(X)))
class MLP(nn.Block):
  def __init__(self):
    # Call the constructor of the MLP parent class nn.Block to perform
    # the necessary initialization
    super().__init__()
    self.hidden = nn.Dense(256, activation='relu')
    self.out = nn.Dense(10)

  # Define the forward propagation of the model, that is, how to return the
  # required model output based on the input X
  def forward(self, X):
    return self.out(self.hidden(X))
class MLP(nn.Module):
  def setup(self):
    # Define the layers
    self.hidden = nn.Dense(256)
    self.out = nn.Dense(10)

  # Define the forward propagation of the model, that is, how to return the
  # required model output based on the input X
  def __call__(self, X):
    return self.out(nn.relu(self.hidden(X)))
class MLP(tf.keras.Model):
  def __init__(self):
    # Call the constructor of the parent class tf.keras.Model to perform
    # the necessary initialization
    super().__init__()
    self.hidden = tf.keras.layers.Dense(units=256, activation=tf.nn.relu)
    self.out = tf.keras.layers.Dense(units=10)

  # Define the forward propagation of the model, that is, how to return the
  # required model output based on the input X
  def call(self, X):
    return self.out(self.hidden((X)))

讓我們首先關(guān)注前向傳播方法。請(qǐng)注意,它以 X輸入為輸入,應(yīng)用激活函數(shù)計(jì)算隱藏表示,并輸出其對(duì)數(shù)。在這個(gè)MLP 實(shí)現(xiàn)中,兩層都是實(shí)例變量。要了解為什么這是合理的,想象一下實(shí)例化兩個(gè) MLPnet1net2,并在不同的數(shù)據(jù)上訓(xùn)練它們。自然地,我們希望它們代表兩種不同的學(xué)習(xí)模型。

我們?cè)跇?gòu)造函數(shù)中實(shí)例化 MLP 的層,隨后在每次調(diào)用前向傳播方法時(shí)調(diào)用這些層。注意幾個(gè)關(guān)鍵細(xì)節(jié)。首先,我們的自定義方法通過(guò)讓我們免于重述適用于大多數(shù)模塊的樣板代碼的痛苦來(lái)__init__調(diào)用父類(lèi)的方法。然后我們實(shí)例化我們的兩個(gè)完全連接的層,將它們分配給 請(qǐng)注意,除非我們實(shí)現(xiàn)一個(gè)新層,否則我們不必?fù)?dān)心反向傳播方法或參數(shù)初始化。系統(tǒng)會(huì)自動(dòng)生成這些方法。讓我們?cè)囋囘@個(gè)。__init__super().__init__()self.hidden


下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評(píng)論

查看更多

下載排行

本周

  1. 1山景DSP芯片AP8248A2數(shù)據(jù)手冊(cè)
  2. 1.06 MB  |  532次下載  |  免費(fèi)
  3. 2RK3399完整板原理圖(支持平板,盒子VR)
  4. 3.28 MB  |  339次下載  |  免費(fèi)
  5. 3TC358743XBG評(píng)估板參考手冊(cè)
  6. 1.36 MB  |  330次下載  |  免費(fèi)
  7. 4DFM軟件使用教程
  8. 0.84 MB  |  295次下載  |  免費(fèi)
  9. 5元宇宙深度解析—未來(lái)的未來(lái)-風(fēng)口還是泡沫
  10. 6.40 MB  |  227次下載  |  免費(fèi)
  11. 6迪文DGUS開(kāi)發(fā)指南
  12. 31.67 MB  |  194次下載  |  免費(fèi)
  13. 7元宇宙底層硬件系列報(bào)告
  14. 13.42 MB  |  182次下載  |  免費(fèi)
  15. 8FP5207XR-G1中文應(yīng)用手冊(cè)
  16. 1.09 MB  |  178次下載  |  免費(fèi)

本月

  1. 1OrCAD10.5下載OrCAD10.5中文版軟件
  2. 0.00 MB  |  234315次下載  |  免費(fèi)
  3. 2555集成電路應(yīng)用800例(新編版)
  4. 0.00 MB  |  33566次下載  |  免費(fèi)
  5. 3接口電路圖大全
  6. 未知  |  30323次下載  |  免費(fèi)
  7. 4開(kāi)關(guān)電源設(shè)計(jì)實(shí)例指南
  8. 未知  |  21549次下載  |  免費(fèi)
  9. 5電氣工程師手冊(cè)免費(fèi)下載(新編第二版pdf電子書(shū))
  10. 0.00 MB  |  15349次下載  |  免費(fèi)
  11. 6數(shù)字電路基礎(chǔ)pdf(下載)
  12. 未知  |  13750次下載  |  免費(fèi)
  13. 7電子制作實(shí)例集錦 下載
  14. 未知  |  8113次下載  |  免費(fèi)
  15. 8《LED驅(qū)動(dòng)電路設(shè)計(jì)》 溫德?tīng)栔?/a>
  16. 0.00 MB  |  6656次下載  |  免費(fèi)

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費(fèi)
  3. 2protel99se軟件下載(可英文版轉(zhuǎn)中文版)
  4. 78.1 MB  |  537798次下載  |  免費(fèi)
  5. 3MATLAB 7.1 下載 (含軟件介紹)
  6. 未知  |  420027次下載  |  免費(fèi)
  7. 4OrCAD10.5下載OrCAD10.5中文版軟件
  8. 0.00 MB  |  234315次下載  |  免費(fèi)
  9. 5Altium DXP2002下載入口
  10. 未知  |  233046次下載  |  免費(fèi)
  11. 6電路仿真軟件multisim 10.0免費(fèi)下載
  12. 340992  |  191187次下載  |  免費(fèi)
  13. 7十天學(xué)會(huì)AVR單片機(jī)與C語(yǔ)言視頻教程 下載
  14. 158M  |  183279次下載  |  免費(fèi)
  15. 8proe5.0野火版下載(中文版免費(fèi)下載)
  16. 未知  |  138040次下載  |  免費(fèi)