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

電子發(fā)燒友App

硬聲App

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

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

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>PyTorch教程2.5之自動微分

PyTorch教程2.5之自動微分

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

資料介紹

回想一下2.4 節(jié),計算導(dǎo)數(shù)是我們將用于訓(xùn)練深度網(wǎng)絡(luò)的所有優(yōu)化算法中的關(guān)鍵步驟。雖然計算很簡單,但手工計算可能很乏味且容易出錯,而且這個問題只會隨著我們的模型變得更加復(fù)雜而增長。

幸運的是,所有現(xiàn)代深度學(xué)習框架都通過提供自動微分(通常簡稱為 autograd )來解決我們的工作。當我們通過每個連續(xù)的函數(shù)傳遞數(shù)據(jù)時,該框架會構(gòu)建一個計算圖來跟蹤每個值如何依賴于其他值。為了計算導(dǎo)數(shù),自動微分通過應(yīng)用鏈式法則通過該圖向后工作。以這種方式應(yīng)用鏈式法則的計算算法稱為反向傳播。

雖然 autograd 庫在過去十年中成為熱門話題,但它們的歷史悠久。事實上,對 autograd 的最早引用可以追溯到半個多世紀以前Wengert,1964 年。現(xiàn)代反向傳播背后的核心思想可以追溯到 1980 年的一篇博士論文 ( Speelpenning, 1980 ),并在 80 年代后期得到進一步發(fā)展 ( Griewank, 1989 )雖然反向傳播已成為計算梯度的默認方法,但它并不是唯一的選擇。例如,Julia 編程語言采用前向傳播 Revels等人,2016 年. 在探索方法之前,我們先來掌握autograd這個包。

import torch
from mxnet import autograd, np, npx

npx.set_np()
from jax import numpy as jnp
import tensorflow as tf

2.5.1. 一個簡單的函數(shù)

假設(shè)我們有興趣區(qū)分函數(shù) y=2x?x關(guān)于列向量x. 首先,我們分配x一個初始值。

x = torch.arange(4.0)
x
tensor([0., 1., 2., 3.])

在我們計算梯度之前y關(guān)于 x,我們需要一個地方來存放它。通常,我們避免每次求導(dǎo)時都分配新內(nèi)存,因為深度學(xué)習需要針對相同參數(shù)連續(xù)計算導(dǎo)數(shù)數(shù)千或數(shù)百萬次,并且我們可能會面臨內(nèi)存耗盡的風險。請注意,標量值函數(shù)相對于向量的梯度x是向量值的并且具有相同的形狀x.

# Can also create x = torch.arange(4.0, requires_grad=True)
x.requires_grad_(True)
x.grad # The gradient is None by default
x = np.arange(4.0)
x
array([0., 1., 2., 3.])

Before we calculate the gradient of y with respect to x, we need a place to store it. In general, we avoid allocating new memory every time we take a derivative because deep learning requires successively computing derivatives with respect to the same parameters thousands or millions of times, and we might risk running out of memory. Note that the gradient of a scalar-valued function with respect to a vector x is vector-valued and has the same shape as x.

# We allocate memory for a tensor's gradient by invoking `attach_grad`
x.attach_grad()
# After we calculate a gradient taken with respect to `x`, we will be able to
# access it via the `grad` attribute, whose values are initialized with 0s
x.grad
array([0., 0., 0., 0.])
x = jnp.arange(4.0)
x
No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
Array([0., 1., 2., 3.], dtype=float32)
x = tf.range(4, dtype=tf.float32)
x
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0., 1., 2., 3.], dtype=float32)>

Before we calculate the gradient of y with respect to x, we need a place to store it. In general, we avoid allocating new memory every time we take a derivative because deep learning requires successively computing derivatives with respect to the same parameters thousands or millions of times, and we might risk running out of memory. Note that the gradient of a scalar-valued function with respect to a vector x is vector-valued and has the same shape as x.

x = tf.Variable(x)

我們現(xiàn)在計算我們的函數(shù)x并將結(jié)果分配給y

y = 2 * torch.dot(x, x)
y
tensor(28., grad_fn=<MulBackward0>)

我們現(xiàn)在可以通過調(diào)用它的方法來獲取y關(guān)于的梯度接下來,我們可以通過的 屬性訪問漸變。xbackwardxgrad

y.backward()
x.grad
tensor([ 0., 4., 8., 12.])
# Our code is inside an `autograd.record` scope to build the computational
# graph
with autograd.record():
  y = 2 * np.dot(x, x)
y
array(28.)

We can now take the gradient of y with respect to x by calling its backward method. Next, we can access the gradient via x’s grad attribute.

y.backward()
x.grad
[09:38:36] src/base.cc:49: GPU context requested, but no GPUs found.
array([ 0., 4., 8., 12.])
y = lambda x: 2 * jnp.dot(x, x)
y(x)
Array(28., dtype=float32)

We can now take the gradient of y with respect to x by passing through the grad transform.

from jax import grad

# The `grad` transform returns a Python function that
# computes the gradient of the original function
x_grad = grad(y)(x)
x_grad
Array([ 0., 4., 8., 12.], dtype=float32)
# Record all computations onto a tape
with tf.GradientTape() as t:
  y = 2 * tf.tensordot(x, x, axes=1)
y
<tf.Tensor: shape=(), dtype=float32, numpy=28.0>

We can now calculate the gradient of y with respect to x by calling the gradient method.

x_grad = t.gradient(y, x)
x_grad
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 

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

評論

查看更多

下載排行

本周

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

本月

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

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費
  3. 2protel99se軟件下載(可英文版轉(zhuǎn)中文版)
  4. 78.1 MB  |  537798次下載  |  免費
  5. 3MATLAB 7.1 下載 (含軟件介紹)
  6. 未知  |  420027次下載  |  免費
  7. 4OrCAD10.5下載OrCAD10.5中文版軟件
  8. 0.00 MB  |  234315次下載  |  免費
  9. 5Altium DXP2002下載入口
  10. 未知  |  233046次下載  |  免費
  11. 6電路仿真軟件multisim 10.0免費下載
  12. 340992  |  191187次下載  |  免費
  13. 7十天學(xué)會AVR單片機與C語言視頻教程 下載
  14. 158M  |  183279次下載  |  免費
  15. 8proe5.0野火版下載(中文版免費下載)
  16. 未知  |  138040次下載  |  免費
主站蜘蛛池模板: 黄网站在线观看高清免费 | 黄色毛片免费网站 | 狠狠色噜噜狠狠狠狠 | 男人的天堂网在线 | 好吊色青青青国产在线观看 | 日韩免费| 天堂在线www天堂中文在线 | 男人午夜网站 | 国产一级特黄 | 末成年一级在线看片 | 色之综合天天综合色天天棕色 | 亚洲最大的成人网 | 成 人网站免费 | 永久免费av网站 | 精品理论片 | 六九视频在线观看 | 国产精品久久久久久久久久影院 | 成年男人午夜片免费观看 | 久久午夜免费视频 | 天天色天天干天天射 | 亚洲一区免费在线 | 国产叼嘿网站免费观看不用充会员 | 亚洲伊人久久综合影院2021 | aa黄色大片 | 亚洲精品综合网在线8050影院 | 欧美亚洲韩国国产综合五月天 | japanese日本护士xx亚洲 | 韩国三级久久精品 | 日本三级在线播放线观看2021 | 宅男在线看片 | 一级毛片不收费 | 欧美性淫爽www视频播放 | 国产成人综合自拍 | 成人羞羞视频国产 | 免费毛片软件 | 亚洲不卡网 | 日本在线视频一区二区 | 成人精品视频在线观看播放 | 亚洲大成色www永久网 | 天天干在线影院 | 亚洲欧美日韩在线观看你懂的 |