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

電子發燒友App

硬聲App

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

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

3天內不再提示
電子發燒友網>電子資料下載>電子資料>PyTorch教程2.5之自動微分

PyTorch教程2.5之自動微分

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

資料介紹

回想一下2.4 節,計算導數是我們將用于訓練深度網絡的所有優化算法中的關鍵步驟。雖然計算很簡單,但手工計算可能很乏味且容易出錯,而且這個問題只會隨著我們的模型變得更加復雜而增長。

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

雖然 autograd 庫在過去十年中成為熱門話題,但它們的歷史悠久。事實上,對 autograd 的最早引用可以追溯到半個多世紀以前Wengert,1964 年現代反向傳播背后的核心思想可以追溯到 1980 年的一篇博士論文 ( Speelpenning, 1980 ),并在 80 年代后期得到進一步發展 ( 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. 一個簡單的函數

假設我們有興趣區分函數 y=2x?x關于列向量x. 首先,我們分配x一個初始值。

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

在我們計算梯度之前y關于 x,我們需要一個地方來存放它。通常,我們避免每次求導時都分配新內存,因為深度學習需要針對相同參數連續計算導數數千或數百萬次,并且我們可能會面臨內存耗盡的風險。請注意,標量值函數相對于向量的梯度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)

我們現在計算我們的函數x并將結果分配給y。

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

我們現在可以通過調用它的方法來獲取y關于的梯度接下來,我們可以通過的 屬性訪問漸變。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數據手冊
  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開發指南
  12. 31.67 MB  |  194次下載  |  免費
  13. 7元宇宙底層硬件系列報告
  14. 13.42 MB  |  182次下載  |  免費
  15. 8FP5207XR-G1中文應用手冊
  16. 1.09 MB  |  178次下載  |  免費

本月

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

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費
  3. 2protel99se軟件下載(可英文版轉中文版)
  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十天學會AVR單片機與C語言視頻教程 下載
  14. 158M  |  183279次下載  |  免費
  15. 8proe5.0野火版下載(中文版免費下載)
  16. 未知  |  138040次下載  |  免費
主站蜘蛛池模板: 日本在线一级 | 天堂网在线资源www最新版 | 全是肉的高h短篇列车 | 性做久久久久久久久 | 久久国产精品99久久久久久牛牛 | 大学生一级特黄的免费大片视频 | 亚洲一区二区三区四区在线观看 | 日本一区二区三区在线 视频观看免费 | 欧美极品xxxxⅹ另类 | 狠狠夜夜 | 天天天干干干 | 两性色午夜视频免费网 | 中文字幕一区二区三区四区五区 | 韩彩英三级无删版甜性涩爱 | 色综合久久一区二区三区 | 九九九色| 在线播放网址 | 一级特黄女人生活片 | 国产午夜在线视频 | 天天爱夜夜爽 | 在线www 天堂网在线 | 美女视频黄免费 | 天天插天天透 | 国产成人精品亚洲77美色 | 观看在线人视频 | 丝袜美腿视频一区二区三区 | 色偷偷中文字幕 | 特级毛片永久久免费观看 | 97av免费视频 | 222aaa免费| 龙口护士门91午夜国产在线 | 国产私拍视频 | 五月天婷婷免费视频 | 一区二区三区福利 | 6080伦理久久精品亚洲 | 婷婷激情狠狠综合五月 | 日本最黄视频 | 夜夜骚视频 | 男人女人的免费视频网站 | 天堂tv亚洲tv日本tv欧美人tv | 色批网站www |