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.

pytorch-rnn.ipynb 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# PyTorch 中的循环神经网络模块\n",
  8. "前面我们讲了循环神经网络的基础知识和网络结构,下面我们教大家如何在 pytorch 下构建循环神经网络,因为 pytorch 的动态图机制,使得循环神经网络非常方便。"
  9. ]
  10. },
  11. {
  12. "cell_type": "markdown",
  13. "metadata": {},
  14. "source": [
  15. "## 一般的 RNN\n",
  16. "\n",
  17. "![](https://ws1.sinaimg.cn/large/006tKfTcly1fmt9xz889xj30kb07nglo.jpg)\n",
  18. "\n",
  19. "对于最简单的 RNN,我们可以使用下面两种方式去调用,分别是 `torch.nn.RNNCell()` 和 `torch.nn.RNN()`,这两种方式的区别在于 `RNNCell()` 只能接受序列中单步的输入,且必须传入隐藏状态,而 `RNN()` 可以接受一个序列的输入,默认会传入全 0 的隐藏状态,也可以自己申明隐藏状态传入。\n",
  20. "\n",
  21. "`RNN()` 里面的参数有\n",
  22. "\n",
  23. "input_size 表示输入 $x_t$ 的特征维度\n",
  24. "\n",
  25. "hidden_size 表示输出的特征维度\n",
  26. "\n",
  27. "num_layers 表示网络的层数\n",
  28. "\n",
  29. "nonlinearity 表示选用的非线性激活函数,默认是 'tanh'\n",
  30. "\n",
  31. "bias 表示是否使用偏置,默认使用\n",
  32. "\n",
  33. "batch_first 表示输入数据的形式,默认是 False,就是这样形式,(seq, batch, feature),也就是将序列长度放在第一位,batch 放在第二位\n",
  34. "\n",
  35. "dropout 表示是否在输出层应用 dropout\n",
  36. "\n",
  37. "bidirectional 表示是否使用双向的 rnn,默认是 False\n",
  38. "\n",
  39. "对于 `RNNCell()`,里面的参数就少很多,只有 input_size,hidden_size,bias 以及 nonlinearity"
  40. ]
  41. },
  42. {
  43. "cell_type": "code",
  44. "execution_count": 46,
  45. "metadata": {
  46. "collapsed": true
  47. },
  48. "outputs": [],
  49. "source": [
  50. "import torch\n",
  51. "from torch.autograd import Variable\n",
  52. "from torch import nn"
  53. ]
  54. },
  55. {
  56. "cell_type": "code",
  57. "execution_count": 47,
  58. "metadata": {
  59. "collapsed": true
  60. },
  61. "outputs": [],
  62. "source": [
  63. "# 定义一个单步的 rnn\n",
  64. "rnn_single = nn.RNNCell(input_size=100, hidden_size=200)"
  65. ]
  66. },
  67. {
  68. "cell_type": "code",
  69. "execution_count": 48,
  70. "metadata": {},
  71. "outputs": [
  72. {
  73. "data": {
  74. "text/plain": [
  75. "Parameter containing:\n",
  76. "1.00000e-02 *\n",
  77. " 6.2260 -5.3805 3.5870 ... -2.2162 6.2760 1.6760\n",
  78. "-5.1878 -4.6751 -5.5926 ... -1.8942 0.1589 1.0725\n",
  79. " 3.3236 -3.2726 5.5399 ... 3.3193 0.2117 1.1730\n",
  80. " ... ⋱ ... \n",
  81. " 2.4032 -3.4415 5.1036 ... -2.2035 -0.1900 -6.4016\n",
  82. " 5.2031 -1.5793 -0.0623 ... 0.3424 6.9412 6.3707\n",
  83. "-5.4495 4.5280 2.1774 ... 1.8767 2.4968 5.3403\n",
  84. "[torch.FloatTensor of size 200x200]"
  85. ]
  86. },
  87. "execution_count": 48,
  88. "metadata": {},
  89. "output_type": "execute_result"
  90. }
  91. ],
  92. "source": [
  93. "# 访问其中的参数\n",
  94. "rnn_single.weight_hh"
  95. ]
  96. },
  97. {
  98. "cell_type": "code",
  99. "execution_count": 49,
  100. "metadata": {
  101. "collapsed": true
  102. },
  103. "outputs": [],
  104. "source": [
  105. "# 构造一个序列,长为 6,batch 是 5, 特征是 100\n",
  106. "x = Variable(torch.randn(6, 5, 100)) # 这是 rnn 的输入格式"
  107. ]
  108. },
  109. {
  110. "cell_type": "code",
  111. "execution_count": 50,
  112. "metadata": {
  113. "collapsed": true
  114. },
  115. "outputs": [],
  116. "source": [
  117. "# 定义初始的记忆状态\n",
  118. "h_t = Variable(torch.zeros(5, 200))"
  119. ]
  120. },
  121. {
  122. "cell_type": "code",
  123. "execution_count": 51,
  124. "metadata": {
  125. "collapsed": true
  126. },
  127. "outputs": [],
  128. "source": [
  129. "# 传入 rnn\n",
  130. "out = []\n",
  131. "for i in range(6): # 通过循环 6 次作用在整个序列上\n",
  132. " h_t = rnn_single(x[i], h_t)\n",
  133. " out.append(h_t)"
  134. ]
  135. },
  136. {
  137. "cell_type": "code",
  138. "execution_count": 52,
  139. "metadata": {},
  140. "outputs": [
  141. {
  142. "data": {
  143. "text/plain": [
  144. "Variable containing:\n",
  145. " 0.0136 0.3723 0.1704 ... 0.4306 -0.7909 -0.5306\n",
  146. "-0.2681 -0.6261 -0.3926 ... 0.1752 0.5739 -0.2061\n",
  147. "-0.4918 -0.7611 0.2787 ... 0.0854 -0.3899 0.0092\n",
  148. " 0.6050 0.1852 -0.4261 ... -0.7220 0.6809 0.1825\n",
  149. "-0.6851 0.7273 0.5396 ... -0.7969 0.6133 -0.0852\n",
  150. "[torch.FloatTensor of size 5x200]"
  151. ]
  152. },
  153. "execution_count": 52,
  154. "metadata": {},
  155. "output_type": "execute_result"
  156. }
  157. ],
  158. "source": [
  159. "h_t"
  160. ]
  161. },
  162. {
  163. "cell_type": "code",
  164. "execution_count": 54,
  165. "metadata": {},
  166. "outputs": [
  167. {
  168. "data": {
  169. "text/plain": [
  170. "6"
  171. ]
  172. },
  173. "execution_count": 54,
  174. "metadata": {},
  175. "output_type": "execute_result"
  176. }
  177. ],
  178. "source": [
  179. "len(out)"
  180. ]
  181. },
  182. {
  183. "cell_type": "code",
  184. "execution_count": 55,
  185. "metadata": {},
  186. "outputs": [
  187. {
  188. "data": {
  189. "text/plain": [
  190. "torch.Size([5, 200])"
  191. ]
  192. },
  193. "execution_count": 55,
  194. "metadata": {},
  195. "output_type": "execute_result"
  196. }
  197. ],
  198. "source": [
  199. "out[0].shape # 每个输出的维度"
  200. ]
  201. },
  202. {
  203. "cell_type": "markdown",
  204. "metadata": {},
  205. "source": [
  206. "可以看到经过了 rnn 之后,隐藏状态的值已经被改变了,因为网络记忆了序列中的信息,同时输出 6 个结果"
  207. ]
  208. },
  209. {
  210. "cell_type": "markdown",
  211. "metadata": {},
  212. "source": [
  213. "下面我们看看直接使用 `RNN` 的情况"
  214. ]
  215. },
  216. {
  217. "cell_type": "code",
  218. "execution_count": 32,
  219. "metadata": {
  220. "collapsed": true
  221. },
  222. "outputs": [],
  223. "source": [
  224. "rnn_seq = nn.RNN(100, 200)"
  225. ]
  226. },
  227. {
  228. "cell_type": "code",
  229. "execution_count": 33,
  230. "metadata": {},
  231. "outputs": [
  232. {
  233. "data": {
  234. "text/plain": [
  235. "Parameter containing:\n",
  236. "1.00000e-02 *\n",
  237. " 1.0998 -1.5018 -1.4337 ... 3.8385 -0.8958 -1.6781\n",
  238. " 5.3302 -5.4654 5.5568 ... 4.7399 5.4110 3.6170\n",
  239. " 1.0788 -0.6620 5.7689 ... -5.0747 -2.9066 0.6152\n",
  240. " ... ⋱ ... \n",
  241. "-5.6921 0.1843 -0.0803 ... -4.5852 5.6194 -1.4734\n",
  242. " 4.4306 6.9795 -1.5736 ... 3.4236 -0.3441 3.1397\n",
  243. " 7.0349 -1.6120 -4.2840 ... -5.5676 6.8897 6.1968\n",
  244. "[torch.FloatTensor of size 200x200]"
  245. ]
  246. },
  247. "execution_count": 33,
  248. "metadata": {},
  249. "output_type": "execute_result"
  250. }
  251. ],
  252. "source": [
  253. "# 访问其中的参数\n",
  254. "rnn_seq.weight_hh_l0"
  255. ]
  256. },
  257. {
  258. "cell_type": "code",
  259. "execution_count": 34,
  260. "metadata": {
  261. "collapsed": true
  262. },
  263. "outputs": [],
  264. "source": [
  265. "out, h_t = rnn_seq(x) # 使用默认的全 0 隐藏状态"
  266. ]
  267. },
  268. {
  269. "cell_type": "code",
  270. "execution_count": 36,
  271. "metadata": {},
  272. "outputs": [
  273. {
  274. "data": {
  275. "text/plain": [
  276. "Variable containing:\n",
  277. "( 0 ,.,.) = \n",
  278. " 0.2012 0.0517 0.0570 ... 0.2316 0.3615 -0.1247\n",
  279. " 0.5307 0.4147 0.7881 ... -0.4138 -0.1444 0.3602\n",
  280. " 0.0882 0.4307 0.3939 ... 0.3244 -0.4629 -0.2315\n",
  281. " 0.2868 0.7400 0.6534 ... 0.6631 0.2624 -0.0162\n",
  282. " 0.0841 0.6274 0.1840 ... 0.5800 0.8780 0.4301\n",
  283. "[torch.FloatTensor of size 1x5x200]"
  284. ]
  285. },
  286. "execution_count": 36,
  287. "metadata": {},
  288. "output_type": "execute_result"
  289. }
  290. ],
  291. "source": [
  292. "h_t"
  293. ]
  294. },
  295. {
  296. "cell_type": "code",
  297. "execution_count": 35,
  298. "metadata": {},
  299. "outputs": [
  300. {
  301. "data": {
  302. "text/plain": [
  303. "6"
  304. ]
  305. },
  306. "execution_count": 35,
  307. "metadata": {},
  308. "output_type": "execute_result"
  309. }
  310. ],
  311. "source": [
  312. "len(out)"
  313. ]
  314. },
  315. {
  316. "cell_type": "markdown",
  317. "metadata": {},
  318. "source": [
  319. "这里的 h_t 是网络最后的隐藏状态,网络也输出了 6 个结果"
  320. ]
  321. },
  322. {
  323. "cell_type": "code",
  324. "execution_count": 40,
  325. "metadata": {
  326. "collapsed": true
  327. },
  328. "outputs": [],
  329. "source": [
  330. "# 自己定义初始的隐藏状态\n",
  331. "h_0 = Variable(torch.randn(1, 5, 200))"
  332. ]
  333. },
  334. {
  335. "cell_type": "markdown",
  336. "metadata": {},
  337. "source": [
  338. "这里的隐藏状态的大小有三个维度,分别是 (num_layers * num_direction, batch, hidden_size)"
  339. ]
  340. },
  341. {
  342. "cell_type": "code",
  343. "execution_count": 41,
  344. "metadata": {},
  345. "outputs": [],
  346. "source": [
  347. "out, h_t = rnn_seq(x, h_0)"
  348. ]
  349. },
  350. {
  351. "cell_type": "code",
  352. "execution_count": 42,
  353. "metadata": {},
  354. "outputs": [
  355. {
  356. "data": {
  357. "text/plain": [
  358. "Variable containing:\n",
  359. "( 0 ,.,.) = \n",
  360. " 0.2091 0.0353 0.0625 ... 0.2340 0.3734 -0.1307\n",
  361. " 0.5498 0.4221 0.7877 ... -0.4143 -0.1209 0.3335\n",
  362. " 0.0757 0.4204 0.3826 ... 0.3187 -0.4626 -0.2336\n",
  363. " 0.3106 0.7355 0.6436 ... 0.6611 0.2587 -0.0338\n",
  364. " 0.1025 0.6350 0.1943 ... 0.5720 0.8749 0.4525\n",
  365. "[torch.FloatTensor of size 1x5x200]"
  366. ]
  367. },
  368. "execution_count": 42,
  369. "metadata": {},
  370. "output_type": "execute_result"
  371. }
  372. ],
  373. "source": [
  374. "h_t"
  375. ]
  376. },
  377. {
  378. "cell_type": "code",
  379. "execution_count": 45,
  380. "metadata": {},
  381. "outputs": [
  382. {
  383. "data": {
  384. "text/plain": [
  385. "torch.Size([6, 5, 200])"
  386. ]
  387. },
  388. "execution_count": 45,
  389. "metadata": {},
  390. "output_type": "execute_result"
  391. }
  392. ],
  393. "source": [
  394. "out.shape"
  395. ]
  396. },
  397. {
  398. "cell_type": "markdown",
  399. "metadata": {},
  400. "source": [
  401. "同时输出的结果也是 (seq, batch, feature)"
  402. ]
  403. },
  404. {
  405. "cell_type": "markdown",
  406. "metadata": {},
  407. "source": [
  408. "一般情况下我们都是用 `nn.RNN()` 而不是 `nn.RNNCell()`,因为 `nn.RNN()` 能够避免我们手动写循环,非常方便,同时如果不特别说明,我们也会选择使用默认的全 0 初始化隐藏状态"
  409. ]
  410. },
  411. {
  412. "cell_type": "markdown",
  413. "metadata": {},
  414. "source": [
  415. "## LSTM"
  416. ]
  417. },
  418. {
  419. "cell_type": "markdown",
  420. "metadata": {},
  421. "source": [
  422. "![](https://ws1.sinaimg.cn/large/006tKfTcly1fmt9qj3uhmj30iz07ct90.jpg)"
  423. ]
  424. },
  425. {
  426. "cell_type": "markdown",
  427. "metadata": {
  428. "collapsed": true
  429. },
  430. "source": [
  431. "LSTM 和基本的 RNN 是一样的,他的参数也是相同的,同时他也有 `nn.LSTMCell()` 和 `nn.LSTM()` 两种形式,跟前面讲的都是相同的,我们就不再赘述了,下面直接举个小例子"
  432. ]
  433. },
  434. {
  435. "cell_type": "code",
  436. "execution_count": 58,
  437. "metadata": {
  438. "collapsed": true
  439. },
  440. "outputs": [],
  441. "source": [
  442. "lstm_seq = nn.LSTM(50, 100, num_layers=2) # 输入维度 100,输出 200,两层"
  443. ]
  444. },
  445. {
  446. "cell_type": "code",
  447. "execution_count": 80,
  448. "metadata": {},
  449. "outputs": [
  450. {
  451. "data": {
  452. "text/plain": [
  453. "Parameter containing:\n",
  454. "1.00000e-02 *\n",
  455. " 3.8420 5.7387 6.1351 ... 1.2680 0.9890 1.3037\n",
  456. "-4.2301 6.8294 -4.8627 ... -6.4147 4.3015 8.4103\n",
  457. " 9.4411 5.0195 9.8620 ... -1.6096 9.2516 -0.6941\n",
  458. " ... ⋱ ... \n",
  459. " 1.2930 -1.3300 -0.9311 ... -6.0891 -0.7164 3.9578\n",
  460. " 9.0435 2.4674 9.4107 ... -3.3822 -3.9773 -3.0685\n",
  461. "-4.2039 -8.2992 -3.3605 ... 2.2875 8.2163 -9.3277\n",
  462. "[torch.FloatTensor of size 400x100]"
  463. ]
  464. },
  465. "execution_count": 80,
  466. "metadata": {},
  467. "output_type": "execute_result"
  468. }
  469. ],
  470. "source": [
  471. "lstm_seq.weight_hh_l0 # 第一层的 h_t 权重"
  472. ]
  473. },
  474. {
  475. "cell_type": "markdown",
  476. "metadata": {},
  477. "source": [
  478. "**小练习:想想为什么这个系数的大小是 (400, 100)**"
  479. ]
  480. },
  481. {
  482. "cell_type": "code",
  483. "execution_count": 59,
  484. "metadata": {},
  485. "outputs": [],
  486. "source": [
  487. "lstm_input = Variable(torch.randn(10, 3, 50)) # 序列 10,batch 是 3,输入维度 50"
  488. ]
  489. },
  490. {
  491. "cell_type": "code",
  492. "execution_count": 64,
  493. "metadata": {
  494. "collapsed": true
  495. },
  496. "outputs": [],
  497. "source": [
  498. "out, (h, c) = lstm_seq(lstm_input) # 使用默认的全 0 隐藏状态"
  499. ]
  500. },
  501. {
  502. "cell_type": "markdown",
  503. "metadata": {},
  504. "source": [
  505. "注意这里 LSTM 输出的隐藏状态有两个,h 和 c,就是上图中的每个 cell 之间的两个箭头,这两个隐藏状态的大小都是相同的,(num_layers * direction, batch, feature)"
  506. ]
  507. },
  508. {
  509. "cell_type": "code",
  510. "execution_count": 66,
  511. "metadata": {},
  512. "outputs": [
  513. {
  514. "data": {
  515. "text/plain": [
  516. "torch.Size([2, 3, 100])"
  517. ]
  518. },
  519. "execution_count": 66,
  520. "metadata": {},
  521. "output_type": "execute_result"
  522. }
  523. ],
  524. "source": [
  525. "h.shape # 两层,Batch 是 3,特征是 100"
  526. ]
  527. },
  528. {
  529. "cell_type": "code",
  530. "execution_count": 67,
  531. "metadata": {},
  532. "outputs": [
  533. {
  534. "data": {
  535. "text/plain": [
  536. "torch.Size([2, 3, 100])"
  537. ]
  538. },
  539. "execution_count": 67,
  540. "metadata": {},
  541. "output_type": "execute_result"
  542. }
  543. ],
  544. "source": [
  545. "c.shape"
  546. ]
  547. },
  548. {
  549. "cell_type": "code",
  550. "execution_count": 61,
  551. "metadata": {},
  552. "outputs": [
  553. {
  554. "data": {
  555. "text/plain": [
  556. "torch.Size([10, 3, 100])"
  557. ]
  558. },
  559. "execution_count": 61,
  560. "metadata": {},
  561. "output_type": "execute_result"
  562. }
  563. ],
  564. "source": [
  565. "out.shape"
  566. ]
  567. },
  568. {
  569. "cell_type": "markdown",
  570. "metadata": {},
  571. "source": [
  572. "我们可以不使用默认的隐藏状态,这是需要传入两个张量"
  573. ]
  574. },
  575. {
  576. "cell_type": "code",
  577. "execution_count": 68,
  578. "metadata": {
  579. "collapsed": true
  580. },
  581. "outputs": [],
  582. "source": [
  583. "h_init = Variable(torch.randn(2, 3, 100))\n",
  584. "c_init = Variable(torch.randn(2, 3, 100))"
  585. ]
  586. },
  587. {
  588. "cell_type": "code",
  589. "execution_count": 69,
  590. "metadata": {
  591. "collapsed": true
  592. },
  593. "outputs": [],
  594. "source": [
  595. "out, (h, c) = lstm_seq(lstm_input, (h_init, c_init))"
  596. ]
  597. },
  598. {
  599. "cell_type": "code",
  600. "execution_count": 70,
  601. "metadata": {},
  602. "outputs": [
  603. {
  604. "data": {
  605. "text/plain": [
  606. "torch.Size([2, 3, 100])"
  607. ]
  608. },
  609. "execution_count": 70,
  610. "metadata": {},
  611. "output_type": "execute_result"
  612. }
  613. ],
  614. "source": [
  615. "h.shape"
  616. ]
  617. },
  618. {
  619. "cell_type": "code",
  620. "execution_count": 71,
  621. "metadata": {},
  622. "outputs": [
  623. {
  624. "data": {
  625. "text/plain": [
  626. "torch.Size([2, 3, 100])"
  627. ]
  628. },
  629. "execution_count": 71,
  630. "metadata": {},
  631. "output_type": "execute_result"
  632. }
  633. ],
  634. "source": [
  635. "c.shape"
  636. ]
  637. },
  638. {
  639. "cell_type": "code",
  640. "execution_count": 72,
  641. "metadata": {},
  642. "outputs": [
  643. {
  644. "data": {
  645. "text/plain": [
  646. "torch.Size([10, 3, 100])"
  647. ]
  648. },
  649. "execution_count": 72,
  650. "metadata": {},
  651. "output_type": "execute_result"
  652. }
  653. ],
  654. "source": [
  655. "out.shape"
  656. ]
  657. },
  658. {
  659. "cell_type": "markdown",
  660. "metadata": {},
  661. "source": [
  662. "# GRU\n",
  663. "![](https://ws3.sinaimg.cn/large/006tKfTcly1fmtaj38y9sj30io06bmxc.jpg)"
  664. ]
  665. },
  666. {
  667. "cell_type": "markdown",
  668. "metadata": {},
  669. "source": [
  670. "GRU 和前面讲的这两个是同样的道理,就不再细说,还是演示一下例子"
  671. ]
  672. },
  673. {
  674. "cell_type": "code",
  675. "execution_count": 73,
  676. "metadata": {
  677. "collapsed": true
  678. },
  679. "outputs": [],
  680. "source": [
  681. "gru_seq = nn.GRU(10, 20)\n",
  682. "gru_input = Variable(torch.randn(3, 32, 10))\n",
  683. "\n",
  684. "out, h = gru_seq(gru_input)"
  685. ]
  686. },
  687. {
  688. "cell_type": "code",
  689. "execution_count": 76,
  690. "metadata": {},
  691. "outputs": [
  692. {
  693. "data": {
  694. "text/plain": [
  695. "Parameter containing:\n",
  696. " 0.0766 -0.0548 -0.2008 ... -0.0250 -0.1819 0.1453\n",
  697. "-0.1676 0.1622 0.0417 ... 0.1905 -0.0071 -0.1038\n",
  698. " 0.0444 -0.1516 0.2194 ... -0.0009 0.0771 0.0476\n",
  699. " ... ⋱ ... \n",
  700. " 0.1698 -0.1707 0.0340 ... -0.1315 0.1278 0.0946\n",
  701. " 0.1936 0.1369 -0.0694 ... -0.0667 0.0429 0.1322\n",
  702. " 0.0870 -0.1884 0.1732 ... -0.1423 -0.1723 0.2147\n",
  703. "[torch.FloatTensor of size 60x20]"
  704. ]
  705. },
  706. "execution_count": 76,
  707. "metadata": {},
  708. "output_type": "execute_result"
  709. }
  710. ],
  711. "source": [
  712. "gru_seq.weight_hh_l0"
  713. ]
  714. },
  715. {
  716. "cell_type": "code",
  717. "execution_count": 75,
  718. "metadata": {},
  719. "outputs": [
  720. {
  721. "data": {
  722. "text/plain": [
  723. "torch.Size([1, 32, 20])"
  724. ]
  725. },
  726. "execution_count": 75,
  727. "metadata": {},
  728. "output_type": "execute_result"
  729. }
  730. ],
  731. "source": [
  732. "h.shape"
  733. ]
  734. },
  735. {
  736. "cell_type": "code",
  737. "execution_count": 74,
  738. "metadata": {},
  739. "outputs": [
  740. {
  741. "data": {
  742. "text/plain": [
  743. "torch.Size([3, 32, 20])"
  744. ]
  745. },
  746. "execution_count": 74,
  747. "metadata": {},
  748. "output_type": "execute_result"
  749. }
  750. ],
  751. "source": [
  752. "out.shape"
  753. ]
  754. }
  755. ],
  756. "metadata": {
  757. "kernelspec": {
  758. "display_name": "Python 3",
  759. "language": "python",
  760. "name": "python3"
  761. },
  762. "language_info": {
  763. "codemirror_mode": {
  764. "name": "ipython",
  765. "version": 3
  766. },
  767. "file_extension": ".py",
  768. "mimetype": "text/x-python",
  769. "name": "python",
  770. "nbconvert_exporter": "python",
  771. "pygments_lexer": "ipython3",
  772. "version": "3.6.8"
  773. }
  774. },
  775. "nbformat": 4,
  776. "nbformat_minor": 2
  777. }

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