哈哈哈哈哈操欧洲电影,久草网在线,亚洲久久熟女熟妇视频,麻豆精品色,久久福利在线视频,日韩中文字幕的,淫乱毛视频一区,亚洲成人一二三,中文人妻日韩精品电影

電子發(fā)燒友App

硬聲App

掃碼添加小助手

加入工程師交流群

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

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

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>PyTorch教程6.2之參數(shù)管理

PyTorch教程6.2之參數(shù)管理

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

資料介紹

一旦我們選擇了一個架構(gòu)并設(shè)置了我們的超參數(shù),我們就進(jìn)入訓(xùn)練循環(huán),我們的目標(biāo)是找到最小化損失函數(shù)的參數(shù)值。訓(xùn)練后,我們將需要這些參數(shù)來進(jìn)行未來的預(yù)測。此外,我們有時會希望提取參數(shù)以在其他上下文中重用它們,將我們的模型保存到磁盤以便它可以在其他軟件中執(zhí)行,或者進(jìn)行檢查以期獲得科學(xué)理解。

大多數(shù)時候,我們將能夠忽略參數(shù)聲明和操作的具體細(xì)節(jié),依靠深度學(xué)習(xí)框架來完成繁重的工作。然而,當(dāng)我們遠(yuǎn)離具有標(biāo)準(zhǔn)層的堆疊架構(gòu)時,我們有時需要陷入聲明和操作參數(shù)的困境。在本節(jié)中,我們將介紹以下內(nèi)容:

  • 訪問用于調(diào)試、診斷和可視化的參數(shù)。

  • 跨不同模型組件共享參數(shù)。

import torch
from torch import nn
from mxnet import init, 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

我們首先關(guān)注具有一個隱藏層的 MLP。

net = nn.Sequential(nn.LazyLinear(8),
          nn.ReLU(),
          nn.LazyLinear(1))

X = torch.rand(size=(2, 4))
net(X).shape
torch.Size([2, 1])
net = nn.Sequential()
net.add(nn.Dense(8, activation='relu'))
net.add(nn.Dense(1))
net.initialize() # Use the default initialization method

X = np.random.uniform(size=(2, 4))
net(X).shape
(2, 1)
net = nn.Sequential([nn.Dense(8), nn.relu, nn.Dense(1)])

X = jax.random.uniform(d2l.get_key(), (2, 4))
params = net.init(d2l.get_key(), X)
net.apply(params, X).shape
(2, 1)
net = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(4, activation=tf.nn.relu),
  tf.keras.layers.Dense(1),
])

X = tf.random.uniform((2, 4))
net(X).shape
TensorShape([2, 1])

6.2.1. 參數(shù)訪問

讓我們從如何從您已知的模型中訪問參數(shù)開始。

當(dāng)通過類定義模型時Sequential,我們可以首先通過索引模型來訪問任何層,就好像它是一個列表一樣。每個層的參數(shù)都方便地位于其屬性中。

When a model is defined via the Sequential class, we can first access any layer by indexing into the model as though it were a list. Each layer’s parameters are conveniently located in its attribute.

Flax and JAX decouple the model and the parameters as you might have observed in the models defined previously. When a model is defined via the Sequential class, we first need to initialize the network to generate the parameters dictionary. We can access any layer’s parameters through the keys of this dictionary.

When a model is defined via the Sequential class, we can first access any layer by indexing into the model as though it were a list. Each layer’s parameters are conveniently located in its attribute.

我們可以如下檢查第二個全連接層的參數(shù)。

net[2].state_dict()
OrderedDict([('weight',
       tensor([[-0.2523, 0.2104, 0.2189, -0.0395, -0.0590, 0.3360, -0.0205, -0.1507]])),
       ('bias', tensor([0.0694]))])
net[1].params
dense1_ (
 Parameter dense1_weight (shape=(1, 8), dtype=float32)
 Parameter dense1_bias (shape=(1,), dtype=float32)
)
params['params']['layers_2']
FrozenDict({
  kernel: Array([[-0.20739523],
      [ 0.16546965],
      [-0.03713543],
      [-0.04860032],
      [-0.2102929 ],
      [ 0.163712 ],
      [ 0.27240783],
      [-0.4046879 ]], dtype=float32),
  bias: Array([0.], dtype=float32),
})
net.layers[2].weights
[<tf.Variable 'dense_1/kernel:0' shape=(4, 1) dtype=float32, numpy=
 array([[-0.52124995],
    [-0.22314149],
    [ 0.20780373],
    [ 0.6839919 ]], dtype=float32)>,
 <tf.Variable 'dense_1/bias:0' shape=(1,) dtype=float32, numpy=array([0.], dtype=float32)>]

我們可以看到這個全連接層包含兩個參數(shù),分別對應(yīng)于該層的權(quán)重和偏差。

6.2.1.1. 目標(biāo)參數(shù)

請注意,每個參數(shù)都表示為參數(shù)類的一個實例。要對參數(shù)做任何有用的事情,我們首先需要訪問基礎(chǔ)數(shù)值。做這件事有很多種方法。有些更簡單,有些則更通用。以下代碼從返回參數(shù)類實例的第二個神經(jīng)網(wǎng)絡(luò)層中提取偏差,并進(jìn)一步訪問該參數(shù)的值。

type(net[2].bias), net[2].bias.data
(torch.nn.parameter.Parameter, tensor([0.0694]))

參數(shù)是復(fù)雜的對象,包含值、梯度和附加信息。這就是為什么我們需要顯式請求該值。

除了值之外,每個參數(shù)還允許我們訪問梯度。因為我們還沒有為這個網(wǎng)絡(luò)調(diào)用反向傳播,所以它處于初始狀態(tài)。

net[2].weight.grad == None
True
type(net[1].bias), net[1].bias.data()
(mxnet.gluon.parameter.Parameter, array([0.]))

Parameters are complex objects, containing values, gradients, and additional information. That is why we need to request the value explicitly.

In addition to the value, each parameter also allows us to access the gradient. Because we have not invoked backpropagation for this network yet, it is in its initial state.

net[1].weight.grad()
array([[0., 0., 0., 0., 0., 0., 0., 0.]])
bias = params['params']['layers_2']['bias']
type(bias), bias
(jaxlib.xla_extension.Array, Array([0.], dtype=float32))

Unlike the other frameworks, JAX does not keep a track of the gradients over the neural network parameters, instead the parameters and the network are decoupled. It allows the user to express their computation as a Python function, and use the grad transformation for the same purpose.

type(net.layers[2].weights[1]), tf.convert_to_tensor(net.layers[2].weights[1])
(tensorflow.python.ops.resource_variable_ops.ResourceVariable,
 <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>)

6.2.1.2. 一次所有參數(shù)

當(dāng)我們需要對所有參數(shù)執(zhí)行操作時,一個一個地訪問它們會變得乏味。當(dāng)我們使用更復(fù)雜的模塊(例如,嵌套模塊)時,情況會變得特別笨拙,因為我們需要遞歸遍歷整個樹以提取每個子模塊的參數(shù)。下面我們演示訪問所有層的參數(shù)。

[(name, param.shape) for name, param in net.named_parameters()]
[('0.weight', torch

參數(shù) 調(diào)試 pytorch
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

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

評論

查看更多

下載排行

本周

  1. 1矽力杰 Silergy SY7215A 同步升壓調(diào)節(jié)器 規(guī)格書 Datasheet 佰祥電子
  2. 1.12 MB  |  5次下載  |  免費
  3. 2HT81696H 內(nèi)置升壓的30W立體聲D類音頻功放數(shù)據(jù)手冊
  4. 1.21 MB   |  1次下載  |  免費
  5. 3HTA6863 3W超低噪聲超低功耗單聲道D類音頻功率放大器數(shù)據(jù)手冊
  6. 0.87 MB   |  次下載  |  免費
  7. 4南芯 Southchip SC8802C 充電控制器 規(guī)格書 Datasheet 佰祥電子
  8. 88.16 KB  |  次下載  |  免費
  9. 5矽力杰 Silergy SY7065 同步升壓轉(zhuǎn)換器 規(guī)格書 Datasheet 佰祥電子
  10. 910.67 KB  |  次下載  |  免費
  11. 6矽力杰 Silergy SY7066 同步升壓轉(zhuǎn)換器 規(guī)格書 Datasheet 佰祥電子
  12. 989.14 KB  |  次下載  |  免費
  13. 7WD6208A產(chǎn)品規(guī)格書
  14. 631.24 KB  |  次下載  |  免費
  15. 8NB685 26 V,12 A,低靜態(tài)電流,大電流 同步降壓變換器數(shù)據(jù)手冊
  16. 1.64 MB   |  次下載  |  2 積分

本月

  1. 1EMC PCB設(shè)計總結(jié)
  2. 0.33 MB   |  12次下載  |  免費
  3. 2PD取電芯片 ECP5702規(guī)格書
  4. 0.88 MB   |  5次下載  |  免費
  5. 3矽力杰 Silergy SY7215A 同步升壓調(diào)節(jié)器 規(guī)格書 Datasheet 佰祥電子
  6. 1.12 MB  |  5次下載  |  免費
  7. 4氮化鎵GaN FET/GaN HEMT 功率驅(qū)動電路選型表
  8. 0.10 MB   |  3次下載  |  免費
  9. 5PD取電芯片,可取5/9/12/15/20V電壓ECP5702數(shù)據(jù)手冊
  10. 0.88 MB   |  3次下載  |  免費
  11. 6SY50655 用于高輸入電壓應(yīng)用的偽固定頻率SSR反激式穩(wěn)壓器英文資料
  12. 1.01 MB   |  3次下載  |  免費
  13. 7怎么為半導(dǎo)體測試儀選擇精密放大器
  14. 0.65 MB   |  2次下載  |  免費
  15. 8SY52341 次級側(cè)同步整流英文手冊
  16. 0.94 MB   |  2次下載  |  免費

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935137次下載  |  10 積分
  3. 2開源硬件-PMP21529.1-4 開關(guān)降壓/升壓雙向直流/直流轉(zhuǎn)換器 PCB layout 設(shè)計
  4. 1.48MB  |  420064次下載  |  10 積分
  5. 3Altium DXP2002下載入口
  6. 未知  |  233095次下載  |  10 積分
  7. 4電路仿真軟件multisim 10.0免費下載
  8. 340992  |  191469次下載  |  10 積分
  9. 5十天學(xué)會AVR單片機(jī)與C語言視頻教程 下載
  10. 158M  |  183360次下載  |  10 積分
  11. 6labview8.5下載
  12. 未知  |  81606次下載  |  10 積分
  13. 7Keil工具M(jìn)DK-Arm免費下載
  14. 0.02 MB  |  73832次下載  |  10 積分
  15. 8LabVIEW 8.6下載
  16. 未知  |  65991次下載  |  10 積分
玉田县| 白城市| 西平县| 保康县| 云和县| 廊坊市| 八宿县| 山东省| 宣武区| 永宁县| 新密市| 墨江| 洪江市| 鄂伦春自治旗| 仙游县| 吉木萨尔县| 高州市| 克什克腾旗| 瑞丽市| 泰顺县| 年辖:市辖区| 蕲春县| 克拉玛依市| 米脂县| 铁岭市| 龙州县| 维西| 朝阳县| 衡阳县| 连山| 南昌市| 大荔县| 集贤县| 津市市| 彰化县| 长治县| 新野县| 晴隆县| 峨山| 容城县| 洞头县|