在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

電子發燒友App

硬聲App

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示
創作
電子發燒友網>電子資料下載>電子資料>PyTorch教程6.4之惰性初始化

PyTorch教程6.4之惰性初始化

2023-06-05 | pdf | 0.11 MB | 次下載 | 免費

資料介紹

到目前為止,似乎我們在建立網絡時草率地逃脫了懲罰。具體來說,我們做了以下不符合直覺的事情,這些事情可能看起來不應該起作用:

  • 我們在沒有指定輸入維度的情況下定義了網絡架構。

  • 我們添加層時沒有指定前一層的輸出維度。

  • 在提供足夠的信息來確定我們的模型應該包含多少參數之前,我們甚至“初始化”了這些參數。

您可能會對我們的代碼完全運行感到驚訝。畢竟,深度學習框架無法判斷網絡的輸入維數。這里的技巧是框架推遲初始化,等到我們第一次通過模型傳遞數據時,動態推斷每一層的大小。

稍后,當使用卷積神經網絡時,該技術將變得更加方便,因為輸入維度(即圖像的分辨率)將影響每個后續層的維度。因此,在編寫代碼時無需知道維度是多少就可以設置參數的能力可以極大地簡化指定和隨后修改模型的任務。接下來,我們將更深入地研究初始化機制。

import torch
from torch import nn
from d2l import torch as d2l
from mxnet import np, npx
from mxnet.gluon import nn

npx.set_np()
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

首先,讓我們實例化一個 MLP。

net = nn.Sequential(nn.LazyLinear(256), nn.ReLU(), nn.LazyLinear(10))
net = nn.Sequential()
net.add(nn.Dense(256, activation='relu'))
net.add(nn.Dense(10))
net = nn.Sequential([nn.Dense(256), nn.relu, nn.Dense(10)])
net = tf.keras.models.Sequential([
  tf.keras.layers.Dense(256, activation=tf.nn.relu),
  tf.keras.layers.Dense(10),
])

此時,網絡不可能知道輸入層權重的維度,因為輸入維度仍然未知。

因此框架還沒有初始化任何參數。我們通過嘗試訪問以下參數來確認。

net[0].weight
<UninitializedParameter>

Consequently the framework has not yet initialized any parameters. We confirm by attempting to access the parameters below.

print(net.collect_params)
print(net.collect_params())
<bound method Block.collect_params of Sequential(
 (0): Dense(-1 -> 256, Activation(relu))
 (1): Dense(-1 -> 10, linear)
)>
sequential0_ (
 Parameter dense0_weight (shape=(256, -1), dtype=float32)
 Parameter dense0_bias (shape=(256,), dtype=float32)
 Parameter dense1_weight (shape=(10, -1), dtype=float32)
 Parameter dense1_bias (shape=(10,), dtype=float32)
)

Note that while the parameter objects exist, the input dimension to each layer is listed as -1. MXNet uses the special value -1 to indicate that the parameter dimension remains unknown. At this point, attempts to access net[0].weight.data() would trigger a runtime error stating that the network must be initialized before the parameters can be accessed. Now let’s see what happens when we attempt to initialize parameters via the initialize method.

net.initialize()
net.collect_params()
sequential0_ (
 Parameter dense0_weight (shape=(256, -1), dtype=float32)
 Parameter dense0_bias (shape=(256,), dtype=float32)
 Parameter dense1_weight (shape=(10, -1), dtype=float32)
 Parameter dense1_bias (shape=(10,), dtype=float32)
)

As we can see, nothing has changed. When input dimensions are unknown, calls to initialize do not truly initialize the parameters. Instead, this call registers to MXNet that we wish (and optionally, according to which distribution) to initialize the parameters.

As mentioned in Section 6.2.1, parameters and the network definition are decoupled in Jax and Flax, and the user handles both manually. Flax models are stateless hence there is no parameters attribute.

Consequently the framework has not yet initialized any parameters. We confirm by attempting to access the parameters below.

[net.layers[i].get_weights() for i in range(len(net.layers))]
[[], []]

Note that each layer objects exist but the weights are empty. Using net.get_weights() would throw an error since the weights have not been initialized yet.

接下來讓我們通過網絡傳遞數據,讓框架最終初始化參數。

X = torch.rand(2, 20)
net(X)

net[0].weight.shape
torch.Size([256, 20])
X = np.random.uniform(size=(2, 20))
net(X)

net.collect_params()
sequential0_ (
 Parameter dense0_weight (shape=(256, 20), dtype=float32)
 Parameter dense0_bias (shape=(256,), dtype=float32)
 Parameter dense1_weight (shape=(10, 256), dtype=float32)
 Parameter dense1_bias (shape=(10,), dtype=float32)
)
params = net.init(d2l.get_key(), jnp.zeros((2, 20)))
jax.tree_util.tree_map(lambda x: x.shape, params).tree_flatten()
(({'params': {'layers_0': {'bias': (256,), 'kernel': (20, 256)},
  'layers_2': {'bias': (10,), 'kernel': (256, 10)}}},),
 ())
X = tf.random.uniform((2, 20))
net(X)
[w.shape for w in net.get_weights()]
[(20, 256), (256,), (256, 10), (10,)]

一旦我們知道輸入維度 20,框架就可以通過插入值 20 來識別第一層權重矩陣的形狀。識別出第一層的形狀后,框架進入第二層,依此類推計算圖,直到所有形狀都已知。請注意,在這種情況下,只有第一層需要延遲初始化,但框架會按順序進行初始化。一旦知道所有參數形狀,框架就可以最終初始化參數。

以下方法通過網絡傳入虛擬輸入以進行試運行,以推斷所有參數形狀并隨后初始化參數。當不需要默認隨機初始化時,稍后將使用它。

@d2l.add_to_class(d2l.Module) #@save
def apply_init(self, inputs, init=None):
  self.forward(*inputs)
  if init is not None:
    self.net.apply(init)

Parameter initialization in Flax is always done manually and handled by the user. The following method takes a dummy input and a key dictionary as argument. This key dictionary has the rngs for initializing the model parameters and dropout rng for generating the dropout mask for the models with dropout layers. More about dropout will be covered later in Section 5.6. Ultimately the method initializes the model returning the parameters. We have been using it under the hood in the previous sections as well.

@d2l.add_to_class(d2l.Module) #@save
def apply_init(self, dummy_input, key):
  params = self.init(key, *dummy_input) # dummy_input tuple unpacked
  return params

6.4.1. 概括

延遲初始化可能很方便,允許框架自動推斷參數形狀,從而輕松修改架構并消除一種常見的錯誤來源。我們可以通過模型傳遞數據,讓框架最終初始化參數。

6.4.2. 練習

  1. 如果您將輸入維度指定給第一層而不是后續層,會發生什么情況?你會立即初始化嗎?

  2. 如果您指定不匹配的尺寸會發生什么?

  3. 如果你有不同維度的輸入,你需要做什么?提示:查看參數綁定。

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

評論

查看更多

下載排行

本周

  1. 1使用單片機實現七人表決器的程序和仿真資料免費下載
  2. 2.96 MB   |  44次下載  |  免費
  3. 2聯想E46L DAOLL6筆記本電腦圖紙
  4. 1.10 MB   |  2次下載  |  5 積分
  5. 3MATLAB繪圖合集
  6. 27.12 MB   |  2次下載  |  5 積分
  7. 4PR735,使用UCC28060的600W交錯式PFC轉換器
  8. 540.03KB   |  1次下載  |  免費
  9. 5UCC38C42 30W同步降壓轉換器參考設計
  10. 428.07KB   |  1次下載  |  免費
  11. 6DV2004S1/ES1/HS1快速充電開發系統
  12. 2.08MB   |  1次下載  |  免費
  13. 7模態分解合集matlab代碼
  14. 3.03 MB   |  1次下載  |  2 積分
  15. 8美的電磁爐維修手冊大全
  16. 1.56 MB   |  1次下載  |  5 積分

本月

  1. 1使用單片機實現七人表決器的程序和仿真資料免費下載
  2. 2.96 MB   |  44次下載  |  免費
  3. 2UC3842/3/4/5電源管理芯片中文手冊
  4. 1.75 MB   |  15次下載  |  免費
  5. 3DMT0660數字萬用表產品說明書
  6. 0.70 MB   |  13次下載  |  免費
  7. 4TPS54202H降壓轉換器評估模塊用戶指南
  8. 1.02MB   |  8次下載  |  免費
  9. 5STM32F101x8/STM32F101xB手冊
  10. 1.69 MB   |  8次下載  |  1 積分
  11. 6HY12P65/HY12P66數字萬用表芯片規格書
  12. 0.69 MB   |  6次下載  |  免費
  13. 7華瑞昇CR216芯片數字萬用表規格書附原理圖及校正流程方法
  14. 0.74 MB   |  6次下載  |  3 積分
  15. 8華瑞昇CR215芯片數字萬用表原理圖
  16. 0.21 MB   |  5次下載  |  3 積分

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935119次下載  |  10 積分
  3. 2開源硬件-PMP21529.1-4 開關降壓/升壓雙向直流/直流轉換器 PCB layout 設計
  4. 1.48MB  |  420061次下載  |  10 積分
  5. 3Altium DXP2002下載入口
  6. 未知  |  233084次下載  |  10 積分
  7. 4電路仿真軟件multisim 10.0免費下載
  8. 340992  |  191367次下載  |  10 積分
  9. 5十天學會AVR單片機與C語言視頻教程 下載
  10. 158M  |  183335次下載  |  10 積分
  11. 6labview8.5下載
  12. 未知  |  81581次下載  |  10 積分
  13. 7Keil工具MDK-Arm免費下載
  14. 0.02 MB  |  73807次下載  |  10 積分
  15. 8LabVIEW 8.6下載
  16. 未知  |  65987次下載  |  10 積分
主站蜘蛛池模板: 国产在线一区二区三区四区 | 伊人天天干| 1000部又爽又黄的做黄禁片 | 午夜免费影视 | 国产亚洲一区二区三区啪 | 国产成人在线影院 | 糖心vlog麻豆精东影业传媒 | 日本免费视频 | 日本一本一道久久香蕉免费 | 国产精品免费久久久免费 | 亚洲美国avcom | 精品手机在线视频 | 四虎永久精品免费网址大全 | 手机在线看片你懂的 | 天天躁日日躁成人字幕aⅴ 天天躁夜夜躁 | 黄视频免费| 狠狠狠狼鲁欧美综合网免费 | 噜噜噜噜私人影院 | 天堂8资源8在线 | 久久香蕉综合色一综合色88 | 免费手机黄色网址 | 俄罗斯欧美色黄激情 | 成年香蕉大黄美女美女 | 天天干天天操天天爱 | 色欧美综合| 97影院午夜在线观看视频 | 国产成人精品日本亚洲直接 | 日韩图区 | 欧美综合网站 | se97se成人亚洲网站 | 成人理伦 | 男男互攻h啪肉np文厉世 | 国产精品免费看久久久香蕉 | 美女扒开尿口让男人捅 | 久久久久免费精品国产小说 | 亚洲va久久久久综合 | 久久久久国产精品免费免费不卡 | 波多野结衣在线观看一区二区 | 国产在线播放一区 | 38pao强力打造永久免费高清视频 | 黄色免费的视频 |