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.

dynamic-graph.ipynb 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# 动态图和静态图\n",
  8. "目前神经网络框架分为静态图框架和动态图框架,PyTorch 和 TensorFlow、Caffe 等框架最大的区别就是他们拥有不同的计算图表现形式。 TensorFlow 使用静态图,这意味着我们先定义计算图,然后不断使用它,而在 PyTorch 中,每次都会重新构建一个新的计算图。通过这次课程,我们会了解静态图和动态图之间的优缺点。\n",
  9. "\n",
  10. "对于使用者来说,两种形式的计算图有着非常大的区别,同时静态图和动态图都有他们各自的优点,比如动态图比较方便debug,使用者能够用任何他们喜欢的方式进行debug,同时非常直观,而静态图是通过先定义后运行的方式,之后再次运行的时候就不再需要重新构建计算图,所以速度会比动态图更快。"
  11. ]
  12. },
  13. {
  14. "cell_type": "markdown",
  15. "metadata": {},
  16. "source": [
  17. "![](https://ws3.sinaimg.cn/large/006tNc79ly1fmai482qumg30rs0fmq6e.gif)"
  18. ]
  19. },
  20. {
  21. "cell_type": "markdown",
  22. "metadata": {},
  23. "source": [
  24. "下面我们比较 while 循环语句在 TensorFlow 和 PyTorch 中的定义"
  25. ]
  26. },
  27. {
  28. "cell_type": "markdown",
  29. "metadata": {},
  30. "source": [
  31. "## TensorFlow"
  32. ]
  33. },
  34. {
  35. "cell_type": "code",
  36. "execution_count": 1,
  37. "metadata": {
  38. "collapsed": true
  39. },
  40. "outputs": [],
  41. "source": [
  42. "# tensorflow\n",
  43. "import tensorflow as tf\n",
  44. "\n",
  45. "first_counter = tf.constant(0)\n",
  46. "second_counter = tf.constant(10)"
  47. ]
  48. },
  49. {
  50. "cell_type": "code",
  51. "execution_count": 2,
  52. "metadata": {
  53. "collapsed": true
  54. },
  55. "outputs": [],
  56. "source": [
  57. "def cond(first_counter, second_counter, *args):\n",
  58. " return first_counter < second_counter\n",
  59. "\n",
  60. "def body(first_counter, second_counter):\n",
  61. " first_counter = tf.add(first_counter, 2)\n",
  62. " second_counter = tf.add(second_counter, 1)\n",
  63. " return first_counter, second_counter"
  64. ]
  65. },
  66. {
  67. "cell_type": "code",
  68. "execution_count": 3,
  69. "metadata": {},
  70. "outputs": [],
  71. "source": [
  72. "c1, c2 = tf.while_loop(cond, body, [first_counter, second_counter])"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 4,
  78. "metadata": {
  79. "collapsed": true
  80. },
  81. "outputs": [],
  82. "source": [
  83. "with tf.Session() as sess:\n",
  84. " counter_1_res, counter_2_res = sess.run([c1, c2])"
  85. ]
  86. },
  87. {
  88. "cell_type": "code",
  89. "execution_count": 5,
  90. "metadata": {},
  91. "outputs": [
  92. {
  93. "name": "stdout",
  94. "output_type": "stream",
  95. "text": [
  96. "20\n",
  97. "20\n"
  98. ]
  99. }
  100. ],
  101. "source": [
  102. "print(counter_1_res)\n",
  103. "print(counter_2_res)"
  104. ]
  105. },
  106. {
  107. "cell_type": "markdown",
  108. "metadata": {},
  109. "source": [
  110. "可以看到 TensorFlow 需要将整个图构建成静态的,换句话说,每次运行的时候图都是一样的,是不能够改变的,所以不能直接使用 Python 的 while 循环语句,需要使用辅助函数 `tf.while_loop` 写成 TensorFlow 内部的形式\n",
  111. "\n",
  112. "这是非常反直觉的,学习成本也是比较高的\n",
  113. "\n",
  114. "下面我们来看看 PyTorch 的动态图机制,这使得我们能够使用 Python 的 while 写循环,非常方便"
  115. ]
  116. },
  117. {
  118. "cell_type": "markdown",
  119. "metadata": {},
  120. "source": [
  121. "## PyTorch"
  122. ]
  123. },
  124. {
  125. "cell_type": "code",
  126. "execution_count": 6,
  127. "metadata": {
  128. "collapsed": true
  129. },
  130. "outputs": [],
  131. "source": [
  132. "# pytorch\n",
  133. "import torch\n",
  134. "first_counter = torch.Tensor([0])\n",
  135. "second_counter = torch.Tensor([10])"
  136. ]
  137. },
  138. {
  139. "cell_type": "code",
  140. "execution_count": 11,
  141. "metadata": {},
  142. "outputs": [],
  143. "source": [
  144. "while (first_counter < second_counter)[0]:\n",
  145. " first_counter += 2\n",
  146. " second_counter += 1"
  147. ]
  148. },
  149. {
  150. "cell_type": "code",
  151. "execution_count": 12,
  152. "metadata": {},
  153. "outputs": [
  154. {
  155. "name": "stdout",
  156. "output_type": "stream",
  157. "text": [
  158. "\n",
  159. " 20\n",
  160. "[torch.FloatTensor of size 1]\n",
  161. "\n",
  162. "\n",
  163. " 20\n",
  164. "[torch.FloatTensor of size 1]\n",
  165. "\n"
  166. ]
  167. }
  168. ],
  169. "source": [
  170. "print(first_counter)\n",
  171. "print(second_counter)"
  172. ]
  173. },
  174. {
  175. "cell_type": "markdown",
  176. "metadata": {},
  177. "source": [
  178. "可以看到 PyTorch 的写法跟 Python 的写法是完全一致的,没有任何额外的学习成本\n",
  179. "\n",
  180. "上面的例子展示如何使用静态图和动态图构建 while 循环,看起来动态图的方式更加简单且直观,你觉得呢?"
  181. ]
  182. }
  183. ],
  184. "metadata": {
  185. "kernelspec": {
  186. "display_name": "Python 3",
  187. "language": "python",
  188. "name": "python3"
  189. },
  190. "language_info": {
  191. "codemirror_mode": {
  192. "name": "ipython",
  193. "version": 3
  194. },
  195. "file_extension": ".py",
  196. "mimetype": "text/x-python",
  197. "name": "python",
  198. "nbconvert_exporter": "python",
  199. "pygments_lexer": "ipython3",
  200. "version": "3.5.2"
  201. }
  202. },
  203. "nbformat": 4,
  204. "nbformat_minor": 2
  205. }

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