電子發(fā)燒友App

硬聲App

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

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

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

PyTorch教程2.5之自動(dòng)微分

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

資料介紹

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

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

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

import torch
from mxnet import autograd, np, npx

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

2.5.1. 一個(gè)簡(jiǎn)單的函數(shù)

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

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

在我們計(jì)算梯度之前y關(guān)于 x,我們需要一個(gè)地方來(lái)存放它。通常,我們避免每次求導(dǎo)時(shí)都分配新內(nèi)存,因?yàn)樯疃葘W(xué)習(xí)需要針對(duì)相同參數(shù)連續(xù)計(jì)算導(dǎo)數(shù)數(shù)千或數(shù)百萬(wàn)次,并且我們可能會(huì)面臨內(nèi)存耗盡的風(fēng)險(xiǎn)。請(qǐng)注意,標(biāo)量值函數(shù)相對(duì)于向量的梯度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)在計(jì)算我們的函數(shù)x并將結(jié)果分配給y

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

我們現(xiàn)在可以通過(guò)調(diào)用它的方法來(lái)獲取y關(guān)于的梯度。接下來(lái),我們可以通過(guò)的 屬性訪(fǎng)問(wè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([ 

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

評(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)