|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# 动态图和静态图\n",
- "目前神经网络框架分为静态图框架和动态图框架,PyTorch 和 TensorFlow、Caffe 等框架最大的区别就是他们拥有不同的计算图表现形式。 TensorFlow 使用静态图,这意味着我们先定义计算图,然后不断使用它,而在 PyTorch 中,每次都会重新构建一个新的计算图。通过这次课程,我们会了解静态图和动态图之间的优缺点。\n",
- "\n",
- "对于使用者来说,两种形式的计算图有着非常大的区别,同时静态图和动态图都有他们各自的优点,比如动态图比较方便debug,使用者能够用任何他们喜欢的方式进行debug,同时非常直观,而静态图是通过先定义后运行的方式,之后再次运行的时候就不再需要重新构建计算图,所以速度会比动态图更快。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- ""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "下面我们比较 while 循环语句在 TensorFlow 和 PyTorch 中的定义"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## TensorFlow"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "# tensorflow\n",
- "import tensorflow as tf\n",
- "\n",
- "first_counter = tf.constant(0)\n",
- "second_counter = tf.constant(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def cond(first_counter, second_counter, *args):\n",
- " return first_counter < second_counter\n",
- "\n",
- "def body(first_counter, second_counter):\n",
- " first_counter = tf.add(first_counter, 2)\n",
- " second_counter = tf.add(second_counter, 1)\n",
- " return first_counter, second_counter"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "c1, c2 = tf.while_loop(cond, body, [first_counter, second_counter])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "with tf.Session() as sess:\n",
- " counter_1_res, counter_2_res = sess.run([c1, c2])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "20\n",
- "20\n"
- ]
- }
- ],
- "source": [
- "print(counter_1_res)\n",
- "print(counter_2_res)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可以看到 TensorFlow 需要将整个图构建成静态的,换句话说,每次运行的时候图都是一样的,是不能够改变的,所以不能直接使用 Python 的 while 循环语句,需要使用辅助函数 `tf.while_loop` 写成 TensorFlow 内部的形式\n",
- "\n",
- "这是非常反直觉的,学习成本也是比较高的\n",
- "\n",
- "下面我们来看看 PyTorch 的动态图机制,这使得我们能够使用 Python 的 while 写循环,非常方便"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## PyTorch"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "# pytorch\n",
- "import torch\n",
- "first_counter = torch.Tensor([0])\n",
- "second_counter = torch.Tensor([10])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [],
- "source": [
- "while (first_counter < second_counter)[0]:\n",
- " first_counter += 2\n",
- " second_counter += 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "\n",
- " 20\n",
- "[torch.FloatTensor of size 1]\n",
- "\n",
- "\n",
- " 20\n",
- "[torch.FloatTensor of size 1]\n",
- "\n"
- ]
- }
- ],
- "source": [
- "print(first_counter)\n",
- "print(second_counter)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可以看到 PyTorch 的写法跟 Python 的写法是完全一致的,没有任何额外的学习成本\n",
- "\n",
- "上面的例子展示如何使用静态图和动态图构建 while 循环,看起来动态图的方式更加简单且直观,你觉得呢?"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.5.2"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
|