You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

autograd.ipynb 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# 自动求导\n",
  8. "这次课程我们会了解 PyTorch 中的自动求导机制,自动求导是 PyTorch 中非常重要的特性,能够让我们避免手动去计算非常复杂的导数,这能够极大地减少了我们构建模型的时间,这也是其前身 Torch 这个框架所不具备的特性,下面我们通过例子看看 PyTorch 自动求导的独特魅力以及探究自动求导的更多用法。"
  9. ]
  10. },
  11. {
  12. "cell_type": "code",
  13. "execution_count": 2,
  14. "metadata": {},
  15. "outputs": [],
  16. "source": [
  17. "import torch\n",
  18. "from torch.autograd import Variable"
  19. ]
  20. },
  21. {
  22. "cell_type": "markdown",
  23. "metadata": {},
  24. "source": [
  25. "## 简单情况的自动求导\n",
  26. "下面我们显示一些简单情况的自动求导,\"简单\"体现在计算的结果都是标量,也就是一个数,我们对这个标量进行自动求导。"
  27. ]
  28. },
  29. {
  30. "cell_type": "code",
  31. "execution_count": 3,
  32. "metadata": {},
  33. "outputs": [
  34. {
  35. "name": "stdout",
  36. "output_type": "stream",
  37. "text": [
  38. "tensor([19.], grad_fn=<AddBackward>)\n"
  39. ]
  40. }
  41. ],
  42. "source": [
  43. "x = Variable(torch.Tensor([2]), requires_grad=True)\n",
  44. "y = x + 2\n",
  45. "z = y ** 2 + 3\n",
  46. "print(z)"
  47. ]
  48. },
  49. {
  50. "cell_type": "markdown",
  51. "metadata": {},
  52. "source": [
  53. "通过上面的一些列操作,我们从 x 得到了最后的结果out,我们可以将其表示为数学公式\n",
  54. "\n",
  55. "$$\n",
  56. "z = (x + 2)^2 + 3\n",
  57. "$$\n",
  58. "\n",
  59. "那么我们从 z 对 x 求导的结果就是 \n",
  60. "\n",
  61. "$$\n",
  62. "\\frac{\\partial z}{\\partial x} = 2 (x + 2) = 2 (2 + 2) = 8\n",
  63. "$$\n",
  64. "如果你对求导不熟悉,可以查看以下[网址进行复习](https://baike.baidu.com/item/%E5%AF%BC%E6%95%B0#1)"
  65. ]
  66. },
  67. {
  68. "cell_type": "code",
  69. "execution_count": 4,
  70. "metadata": {},
  71. "outputs": [
  72. {
  73. "name": "stdout",
  74. "output_type": "stream",
  75. "text": [
  76. "tensor([8.])\n"
  77. ]
  78. }
  79. ],
  80. "source": [
  81. "# 使用自动求导\n",
  82. "z.backward()\n",
  83. "print(x.grad)"
  84. ]
  85. },
  86. {
  87. "cell_type": "markdown",
  88. "metadata": {},
  89. "source": [
  90. "对于上面这样一个简单的例子,我们验证了自动求导,同时可以发现发现使用自动求导非常方便。如果是一个更加复杂的例子,那么手动求导就会显得非常的麻烦,所以自动求导的机制能够帮助我们省去麻烦的数学计算,下面我们可以看一个更加复杂的例子。"
  91. ]
  92. },
  93. {
  94. "cell_type": "code",
  95. "execution_count": 5,
  96. "metadata": {},
  97. "outputs": [],
  98. "source": [
  99. "x = Variable(torch.randn(10, 20), requires_grad=True)\n",
  100. "y = Variable(torch.randn(10, 5), requires_grad=True)\n",
  101. "w = Variable(torch.randn(20, 5), requires_grad=True)\n",
  102. "\n",
  103. "out = torch.mean(y - torch.matmul(x, w)) # torch.matmul 是做矩阵乘法\n",
  104. "out.backward()"
  105. ]
  106. },
  107. {
  108. "cell_type": "markdown",
  109. "metadata": {},
  110. "source": [
  111. "如果你对矩阵乘法不熟悉,可以查看下面的[网址进行复习](https://baike.baidu.com/item/%E7%9F%A9%E9%98%B5%E4%B9%98%E6%B3%95/5446029?fr=aladdin)"
  112. ]
  113. },
  114. {
  115. "cell_type": "code",
  116. "execution_count": 5,
  117. "metadata": {},
  118. "outputs": [
  119. {
  120. "name": "stdout",
  121. "output_type": "stream",
  122. "text": [
  123. "Variable containing:\n",
  124. "\n",
  125. "Columns 0 to 9 \n",
  126. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  127. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  128. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  129. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  130. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  131. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  132. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  133. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  134. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  135. "-0.0600 -0.0242 -0.0514 0.0882 0.0056 -0.0400 -0.0300 -0.0052 -0.0289 -0.0172\n",
  136. "\n",
  137. "Columns 10 to 19 \n",
  138. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  139. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  140. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  141. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  142. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  143. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  144. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  145. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  146. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  147. "-0.0372 0.0144 -0.1074 -0.0363 -0.0189 0.0209 0.0618 0.0435 -0.0591 0.0103\n",
  148. "[torch.FloatTensor of size 10x20]\n",
  149. "\n"
  150. ]
  151. }
  152. ],
  153. "source": [
  154. "# 得到 x 的梯度\n",
  155. "print(x.grad)"
  156. ]
  157. },
  158. {
  159. "cell_type": "code",
  160. "execution_count": 6,
  161. "metadata": {},
  162. "outputs": [
  163. {
  164. "name": "stdout",
  165. "output_type": "stream",
  166. "text": [
  167. "Variable containing:\n",
  168. "1.00000e-02 *\n",
  169. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  170. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  171. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  172. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  173. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  174. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  175. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  176. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  177. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  178. " 2.0000 2.0000 2.0000 2.0000 2.0000\n",
  179. "[torch.FloatTensor of size 10x5]\n",
  180. "\n"
  181. ]
  182. }
  183. ],
  184. "source": [
  185. "# 得到 y 的的梯度\n",
  186. "print(y.grad)"
  187. ]
  188. },
  189. {
  190. "cell_type": "code",
  191. "execution_count": 7,
  192. "metadata": {},
  193. "outputs": [
  194. {
  195. "name": "stdout",
  196. "output_type": "stream",
  197. "text": [
  198. "Variable containing:\n",
  199. " 0.1342 0.1342 0.1342 0.1342 0.1342\n",
  200. " 0.0507 0.0507 0.0507 0.0507 0.0507\n",
  201. " 0.0328 0.0328 0.0328 0.0328 0.0328\n",
  202. "-0.0086 -0.0086 -0.0086 -0.0086 -0.0086\n",
  203. " 0.0734 0.0734 0.0734 0.0734 0.0734\n",
  204. "-0.0042 -0.0042 -0.0042 -0.0042 -0.0042\n",
  205. " 0.0078 0.0078 0.0078 0.0078 0.0078\n",
  206. "-0.0769 -0.0769 -0.0769 -0.0769 -0.0769\n",
  207. " 0.0672 0.0672 0.0672 0.0672 0.0672\n",
  208. " 0.1614 0.1614 0.1614 0.1614 0.1614\n",
  209. "-0.0042 -0.0042 -0.0042 -0.0042 -0.0042\n",
  210. "-0.0970 -0.0970 -0.0970 -0.0970 -0.0970\n",
  211. "-0.0364 -0.0364 -0.0364 -0.0364 -0.0364\n",
  212. "-0.0419 -0.0419 -0.0419 -0.0419 -0.0419\n",
  213. " 0.0134 0.0134 0.0134 0.0134 0.0134\n",
  214. "-0.0251 -0.0251 -0.0251 -0.0251 -0.0251\n",
  215. " 0.0586 0.0586 0.0586 0.0586 0.0586\n",
  216. "-0.0050 -0.0050 -0.0050 -0.0050 -0.0050\n",
  217. " 0.1125 0.1125 0.1125 0.1125 0.1125\n",
  218. "-0.0096 -0.0096 -0.0096 -0.0096 -0.0096\n",
  219. "[torch.FloatTensor of size 20x5]\n",
  220. "\n"
  221. ]
  222. }
  223. ],
  224. "source": [
  225. "# 得到 w 的梯度\n",
  226. "print(w.grad)"
  227. ]
  228. },
  229. {
  230. "cell_type": "markdown",
  231. "metadata": {},
  232. "source": [
  233. "上面数学公式就更加复杂,矩阵乘法之后对两个矩阵对应元素相乘,然后所有元素求平均,有兴趣的同学可以手动去计算一下梯度,使用 PyTorch 的自动求导,我们能够非常容易得到 x, y 和 w 的导数,因为深度学习中充满大量的矩阵运算,所以我们没有办法手动去求这些导数,有了自动求导能够非常方便地解决网络更新的问题。"
  234. ]
  235. },
  236. {
  237. "cell_type": "markdown",
  238. "metadata": {},
  239. "source": [
  240. "\n"
  241. ]
  242. },
  243. {
  244. "cell_type": "markdown",
  245. "metadata": {},
  246. "source": [
  247. "## 复杂情况的自动求导\n",
  248. "上面我们展示了简单情况下的自动求导,都是对标量进行自动求导,可能你会有一个疑问,如何对一个向量或者矩阵自动求导了呢?感兴趣的同学可以自己先去尝试一下,下面我们会介绍对多维数组的自动求导机制。"
  249. ]
  250. },
  251. {
  252. "cell_type": "code",
  253. "execution_count": 8,
  254. "metadata": {},
  255. "outputs": [
  256. {
  257. "name": "stdout",
  258. "output_type": "stream",
  259. "text": [
  260. "Variable containing:\n",
  261. " 2 3\n",
  262. "[torch.FloatTensor of size 1x2]\n",
  263. "\n",
  264. "Variable containing:\n",
  265. " 0 0\n",
  266. "[torch.FloatTensor of size 1x2]\n",
  267. "\n"
  268. ]
  269. }
  270. ],
  271. "source": [
  272. "m = Variable(torch.FloatTensor([[2, 3]]), requires_grad=True) # 构建一个 1 x 2 的矩阵\n",
  273. "n = Variable(torch.zeros(1, 2)) # 构建一个相同大小的 0 矩阵\n",
  274. "print(m)\n",
  275. "print(n)"
  276. ]
  277. },
  278. {
  279. "cell_type": "code",
  280. "execution_count": 9,
  281. "metadata": {},
  282. "outputs": [
  283. {
  284. "name": "stdout",
  285. "output_type": "stream",
  286. "text": [
  287. "Variable containing:\n",
  288. " 4 27\n",
  289. "[torch.FloatTensor of size 1x2]\n",
  290. "\n"
  291. ]
  292. }
  293. ],
  294. "source": [
  295. "# 通过 m 中的值计算新的 n 中的值\n",
  296. "n[0, 0] = m[0, 0] ** 2\n",
  297. "n[0, 1] = m[0, 1] ** 3\n",
  298. "print(n)"
  299. ]
  300. },
  301. {
  302. "cell_type": "markdown",
  303. "metadata": {},
  304. "source": [
  305. "将上面的式子写成数学公式,可以得到 \n",
  306. "$$\n",
  307. "n = (n_0,\\ n_1) = (m_0^2,\\ m_1^3) = (2^2,\\ 3^3) \n",
  308. "$$"
  309. ]
  310. },
  311. {
  312. "cell_type": "markdown",
  313. "metadata": {},
  314. "source": [
  315. "下面我们直接对 n 进行反向传播,也就是求 n 对 m 的导数。\n",
  316. "\n",
  317. "这时我们需要明确这个导数的定义,即如何定义\n",
  318. "\n",
  319. "$$\n",
  320. "\\frac{\\partial n}{\\partial m} = \\frac{\\partial (n_0,\\ n_1)}{\\partial (m_0,\\ m_1)}\n",
  321. "$$\n"
  322. ]
  323. },
  324. {
  325. "cell_type": "markdown",
  326. "metadata": {},
  327. "source": [
  328. "在 PyTorch 中,如果要调用自动求导,需要往`backward()`中传入一个参数,这个参数的形状和 n 一样大,比如是 $(w_0,\\ w_1)$,那么自动求导的结果就是:\n",
  329. "$$\n",
  330. "\\frac{\\partial n}{\\partial m_0} = w_0 \\frac{\\partial n_0}{\\partial m_0} + w_1 \\frac{\\partial n_1}{\\partial m_0}\n",
  331. "$$\n",
  332. "$$\n",
  333. "\\frac{\\partial n}{\\partial m_1} = w_0 \\frac{\\partial n_0}{\\partial m_1} + w_1 \\frac{\\partial n_1}{\\partial m_1}\n",
  334. "$$"
  335. ]
  336. },
  337. {
  338. "cell_type": "code",
  339. "execution_count": 10,
  340. "metadata": {},
  341. "outputs": [],
  342. "source": [
  343. "n.backward(torch.ones_like(n)) # 将 (w0, w1) 取成 (1, 1)"
  344. ]
  345. },
  346. {
  347. "cell_type": "code",
  348. "execution_count": 11,
  349. "metadata": {},
  350. "outputs": [
  351. {
  352. "name": "stdout",
  353. "output_type": "stream",
  354. "text": [
  355. "Variable containing:\n",
  356. " 4 27\n",
  357. "[torch.FloatTensor of size 1x2]\n",
  358. "\n"
  359. ]
  360. }
  361. ],
  362. "source": [
  363. "print(m.grad)"
  364. ]
  365. },
  366. {
  367. "cell_type": "markdown",
  368. "metadata": {},
  369. "source": [
  370. "通过自动求导我们得到了梯度是 4 和 27,我们可以验算一下\n",
  371. "$$\n",
  372. "\\frac{\\partial n}{\\partial m_0} = w_0 \\frac{\\partial n_0}{\\partial m_0} + w_1 \\frac{\\partial n_1}{\\partial m_0} = 2 m_0 + 0 = 2 \\times 2 = 4\n",
  373. "$$\n",
  374. "$$\n",
  375. "\\frac{\\partial n}{\\partial m_1} = w_0 \\frac{\\partial n_0}{\\partial m_1} + w_1 \\frac{\\partial n_1}{\\partial m_1} = 0 + 3 m_1^2 = 3 \\times 3^2 = 27\n",
  376. "$$\n",
  377. "通过验算我们可以得到相同的结果"
  378. ]
  379. },
  380. {
  381. "cell_type": "markdown",
  382. "metadata": {},
  383. "source": [
  384. "\n"
  385. ]
  386. },
  387. {
  388. "cell_type": "markdown",
  389. "metadata": {},
  390. "source": [
  391. "## 多次自动求导\n",
  392. "通过调用 backward 我们可以进行一次自动求导,如果我们再调用一次 backward,会发现程序报错,没有办法再做一次。这是因为 PyTorch 默认做完一次自动求导之后,计算图就被丢弃了,所以两次自动求导需要手动设置一个东西,我们通过下面的小例子来说明。"
  393. ]
  394. },
  395. {
  396. "cell_type": "code",
  397. "execution_count": 12,
  398. "metadata": {},
  399. "outputs": [
  400. {
  401. "name": "stdout",
  402. "output_type": "stream",
  403. "text": [
  404. "Variable containing:\n",
  405. " 18\n",
  406. "[torch.FloatTensor of size 1]\n",
  407. "\n"
  408. ]
  409. }
  410. ],
  411. "source": [
  412. "x = Variable(torch.FloatTensor([3]), requires_grad=True)\n",
  413. "y = x * 2 + x ** 2 + 3\n",
  414. "print(y)"
  415. ]
  416. },
  417. {
  418. "cell_type": "code",
  419. "execution_count": 13,
  420. "metadata": {},
  421. "outputs": [],
  422. "source": [
  423. "y.backward(retain_graph=True) # 设置 retain_graph 为 True 来保留计算图"
  424. ]
  425. },
  426. {
  427. "cell_type": "code",
  428. "execution_count": 14,
  429. "metadata": {},
  430. "outputs": [
  431. {
  432. "name": "stdout",
  433. "output_type": "stream",
  434. "text": [
  435. "Variable containing:\n",
  436. " 8\n",
  437. "[torch.FloatTensor of size 1]\n",
  438. "\n"
  439. ]
  440. }
  441. ],
  442. "source": [
  443. "print(x.grad)"
  444. ]
  445. },
  446. {
  447. "cell_type": "code",
  448. "execution_count": 15,
  449. "metadata": {
  450. "collapsed": true
  451. },
  452. "outputs": [],
  453. "source": [
  454. "y.backward() # 再做一次自动求导,这次不保留计算图"
  455. ]
  456. },
  457. {
  458. "cell_type": "code",
  459. "execution_count": 16,
  460. "metadata": {},
  461. "outputs": [
  462. {
  463. "name": "stdout",
  464. "output_type": "stream",
  465. "text": [
  466. "Variable containing:\n",
  467. " 16\n",
  468. "[torch.FloatTensor of size 1]\n",
  469. "\n"
  470. ]
  471. }
  472. ],
  473. "source": [
  474. "print(x.grad)"
  475. ]
  476. },
  477. {
  478. "cell_type": "markdown",
  479. "metadata": {},
  480. "source": [
  481. "可以发现 x 的梯度变成了 16,因为这里做了两次自动求导,所以讲第一次的梯度 8 和第二次的梯度 8 加起来得到了 16 的结果。"
  482. ]
  483. },
  484. {
  485. "cell_type": "markdown",
  486. "metadata": {},
  487. "source": [
  488. "\n"
  489. ]
  490. },
  491. {
  492. "cell_type": "markdown",
  493. "metadata": {},
  494. "source": [
  495. "**小练习**\n",
  496. "\n",
  497. "定义\n",
  498. "\n",
  499. "$$\n",
  500. "x = \n",
  501. "\\left[\n",
  502. "\\begin{matrix}\n",
  503. "x_0 \\\\\n",
  504. "x_1\n",
  505. "\\end{matrix}\n",
  506. "\\right] = \n",
  507. "\\left[\n",
  508. "\\begin{matrix}\n",
  509. "2 \\\\\n",
  510. "3\n",
  511. "\\end{matrix}\n",
  512. "\\right]\n",
  513. "$$\n",
  514. "\n",
  515. "$$\n",
  516. "k = (k_0,\\ k_1) = (x_0^2 + 3 x_1,\\ 2 x_0 + x_1^2)\n",
  517. "$$\n",
  518. "\n",
  519. "我们希望求得\n",
  520. "\n",
  521. "$$\n",
  522. "j = \\left[\n",
  523. "\\begin{matrix}\n",
  524. "\\frac{\\partial k_0}{\\partial x_0} & \\frac{\\partial k_0}{\\partial x_1} \\\\\n",
  525. "\\frac{\\partial k_1}{\\partial x_0} & \\frac{\\partial k_1}{\\partial x_1}\n",
  526. "\\end{matrix}\n",
  527. "\\right]\n",
  528. "$$\n",
  529. "\n",
  530. "参考答案:\n",
  531. "\n",
  532. "$$\n",
  533. "\\left[\n",
  534. "\\begin{matrix}\n",
  535. "4 & 3 \\\\\n",
  536. "2 & 6 \\\\\n",
  537. "\\end{matrix}\n",
  538. "\\right]\n",
  539. "$$"
  540. ]
  541. },
  542. {
  543. "cell_type": "code",
  544. "execution_count": 6,
  545. "metadata": {
  546. "collapsed": true
  547. },
  548. "outputs": [],
  549. "source": [
  550. "x = Variable(torch.FloatTensor([2, 3]), requires_grad=True)\n",
  551. "k = Variable(torch.zeros(2))\n",
  552. "\n",
  553. "k[0] = x[0] ** 2 + 3 * x[1]\n",
  554. "k[1] = x[1] ** 2 + 2 * x[0]"
  555. ]
  556. },
  557. {
  558. "cell_type": "code",
  559. "execution_count": 7,
  560. "metadata": {},
  561. "outputs": [
  562. {
  563. "name": "stdout",
  564. "output_type": "stream",
  565. "text": [
  566. "Variable containing:\n",
  567. " 13\n",
  568. " 13\n",
  569. "[torch.FloatTensor of size 2]\n",
  570. "\n"
  571. ]
  572. }
  573. ],
  574. "source": [
  575. "print(k)"
  576. ]
  577. },
  578. {
  579. "cell_type": "code",
  580. "execution_count": 8,
  581. "metadata": {},
  582. "outputs": [],
  583. "source": [
  584. "j = torch.zeros(2, 2)\n",
  585. "\n",
  586. "k.backward(torch.FloatTensor([1, 0]), retain_graph=True)\n",
  587. "j[0] = x.grad.data\n",
  588. "\n",
  589. "x.grad.data.zero_() # 归零之前求得的梯度\n",
  590. "\n",
  591. "k.backward(torch.FloatTensor([0, 1]))\n",
  592. "j[1] = x.grad.data"
  593. ]
  594. },
  595. {
  596. "cell_type": "code",
  597. "execution_count": 9,
  598. "metadata": {},
  599. "outputs": [
  600. {
  601. "name": "stdout",
  602. "output_type": "stream",
  603. "text": [
  604. "\n",
  605. " 4 3\n",
  606. " 2 6\n",
  607. "[torch.FloatTensor of size 2x2]\n",
  608. "\n"
  609. ]
  610. }
  611. ],
  612. "source": [
  613. "print(j)"
  614. ]
  615. },
  616. {
  617. "cell_type": "markdown",
  618. "metadata": {},
  619. "source": [
  620. "下一次课我们会介绍两种神经网络的编程方式,动态图编程和静态图编程"
  621. ]
  622. }
  623. ],
  624. "metadata": {
  625. "kernelspec": {
  626. "display_name": "Python 3",
  627. "language": "python",
  628. "name": "python3"
  629. },
  630. "language_info": {
  631. "codemirror_mode": {
  632. "name": "ipython",
  633. "version": 3
  634. },
  635. "file_extension": ".py",
  636. "mimetype": "text/x-python",
  637. "name": "python",
  638. "nbconvert_exporter": "python",
  639. "pygments_lexer": "ipython3",
  640. "version": "3.5.2"
  641. }
  642. },
  643. "nbformat": 4,
  644. "nbformat_minor": 2
  645. }

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。