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.

3-dynamic-graph.ipynb 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# 动态图和静态图\n",
  8. "目前神经网络框架分为[静态图框架和动态图框架](https://blog.csdn.net/qq_36653505/article/details/87875279),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. "outputs": [
  39. {
  40. "ename": "ModuleNotFoundError",
  41. "evalue": "No module named 'tensorflow'",
  42. "output_type": "error",
  43. "traceback": [
  44. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  45. "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
  46. "\u001b[0;32m<ipython-input-1-7d11304356bb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# tensorflow\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mtensorflow\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mfirst_counter\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconstant\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0msecond_counter\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconstant\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  47. "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'tensorflow'"
  48. ]
  49. }
  50. ],
  51. "source": [
  52. "# tensorflow\n",
  53. "import tensorflow as tf\n",
  54. "\n",
  55. "first_counter = tf.constant(0)\n",
  56. "second_counter = tf.constant(10)"
  57. ]
  58. },
  59. {
  60. "cell_type": "code",
  61. "execution_count": 16,
  62. "metadata": {},
  63. "outputs": [],
  64. "source": [
  65. "def cond(first_counter, second_counter, *args):\n",
  66. " return first_counter < second_counter\n",
  67. "\n",
  68. "def body(first_counter, second_counter):\n",
  69. " first_counter = tf.add(first_counter, 2)\n",
  70. " second_counter = tf.add(second_counter, 1)\n",
  71. " return first_counter, second_counter"
  72. ]
  73. },
  74. {
  75. "cell_type": "code",
  76. "execution_count": 17,
  77. "metadata": {},
  78. "outputs": [],
  79. "source": [
  80. "c1, c2 = tf.while_loop(cond, body, [first_counter, second_counter])"
  81. ]
  82. },
  83. {
  84. "cell_type": "code",
  85. "execution_count": 21,
  86. "metadata": {},
  87. "outputs": [
  88. {
  89. "ename": "RuntimeError",
  90. "evalue": "The Session graph is empty. Add operations to the graph before calling run().",
  91. "output_type": "error",
  92. "traceback": [
  93. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  94. "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
  95. "\u001b[0;32m<ipython-input-21-430d26a59053>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSession\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0msess\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcounter_1_res\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcounter_2_res\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mc1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  96. "\u001b[0;32m~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m 956\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 957\u001b[0m result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[0;32m--> 958\u001b[0;31m run_metadata_ptr)\n\u001b[0m\u001b[1;32m 959\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 960\u001b[0m \u001b[0mproto_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  97. "\u001b[0;32m~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_run\u001b[0;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m 1104\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Attempted to use a closed Session.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1105\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgraph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mversion\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1106\u001b[0;31m raise RuntimeError('The Session graph is empty. Add operations to the '\n\u001b[0m\u001b[1;32m 1107\u001b[0m 'graph before calling run().')\n\u001b[1;32m 1108\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
  98. "\u001b[0;31mRuntimeError\u001b[0m: The Session graph is empty. Add operations to the graph before calling run()."
  99. ]
  100. }
  101. ],
  102. "source": [
  103. "with tf.compat.v1.Session() as sess:\n",
  104. " counter_1_res, counter_2_res = sess.run([c1, c2])"
  105. ]
  106. },
  107. {
  108. "cell_type": "code",
  109. "execution_count": 19,
  110. "metadata": {},
  111. "outputs": [
  112. {
  113. "ename": "NameError",
  114. "evalue": "name 'counter_1_res' is not defined",
  115. "output_type": "error",
  116. "traceback": [
  117. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  118. "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
  119. "\u001b[0;32m<ipython-input-19-62b1e84b7d43>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcounter_1_res\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcounter_2_res\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  120. "\u001b[0;31mNameError\u001b[0m: name 'counter_1_res' is not defined"
  121. ]
  122. }
  123. ],
  124. "source": [
  125. "print(counter_1_res)\n",
  126. "print(counter_2_res)"
  127. ]
  128. },
  129. {
  130. "cell_type": "markdown",
  131. "metadata": {},
  132. "source": [
  133. "可以看到 TensorFlow 需要将整个图构建成静态的,换句话说,每次运行的时候图都是一样的,是不能够改变的,所以不能直接使用 Python 的 while 循环语句,需要使用辅助函数 `tf.while_loop` 写成 TensorFlow 内部的形式\n",
  134. "\n",
  135. "这是非常反直觉的,学习成本也是比较高的\n",
  136. "\n",
  137. "下面我们来看看 PyTorch 的动态图机制,这使得我们能够使用 Python 的 while 写循环,非常方便"
  138. ]
  139. },
  140. {
  141. "cell_type": "markdown",
  142. "metadata": {},
  143. "source": [
  144. "## PyTorch"
  145. ]
  146. },
  147. {
  148. "cell_type": "code",
  149. "execution_count": 3,
  150. "metadata": {},
  151. "outputs": [],
  152. "source": [
  153. "# pytorch\n",
  154. "import torch\n",
  155. "first_counter = torch.Tensor([0])\n",
  156. "second_counter = torch.Tensor([10])"
  157. ]
  158. },
  159. {
  160. "cell_type": "code",
  161. "execution_count": 5,
  162. "metadata": {},
  163. "outputs": [],
  164. "source": [
  165. "while (first_counter < second_counter)[0]:\n",
  166. " first_counter += 2\n",
  167. " second_counter += 1"
  168. ]
  169. },
  170. {
  171. "cell_type": "code",
  172. "execution_count": 6,
  173. "metadata": {},
  174. "outputs": [
  175. {
  176. "name": "stdout",
  177. "output_type": "stream",
  178. "text": [
  179. "tensor([20.])\n",
  180. "tensor([20.])\n"
  181. ]
  182. }
  183. ],
  184. "source": [
  185. "print(first_counter)\n",
  186. "print(second_counter)"
  187. ]
  188. },
  189. {
  190. "cell_type": "markdown",
  191. "metadata": {},
  192. "source": [
  193. "可以看到 PyTorch 的写法跟 Python 的写法是完全一致的,没有任何额外的学习成本\n",
  194. "\n",
  195. "上面的例子展示如何使用静态图和动态图构建 while 循环,看起来动态图的方式更加简单且直观,你觉得呢?"
  196. ]
  197. }
  198. ],
  199. "metadata": {
  200. "kernelspec": {
  201. "display_name": "Python 3",
  202. "language": "python",
  203. "name": "python3"
  204. },
  205. "language_info": {
  206. "codemirror_mode": {
  207. "name": "ipython",
  208. "version": 3
  209. },
  210. "file_extension": ".py",
  211. "mimetype": "text/x-python",
  212. "name": "python",
  213. "nbconvert_exporter": "python",
  214. "pygments_lexer": "ipython3",
  215. "version": "3.6.9"
  216. }
  217. },
  218. "nbformat": 4,
  219. "nbformat_minor": 2
  220. }

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