{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 命令式和符号式混合编程" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-08-29T16:56:39.722075Z", "start_time": "2019-08-29T16:56:39.434870Z" } }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import d2l\n", "from mxnet import np, npx, sym\n", "from mxnet.gluon import nn\n", "npx.set_np()\n", "\n", "def add(a, b):\n", " return a + b\n", "def fancy_func(a, b, c):\n", " e = add(a, b)\n", " return add(c, e)\n", "fancy_func(1, 2, 3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "符号式编程。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2019-08-29T16:56:39.724022Z", "start_time": "2019-08-29T16:56:39.362Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "def add_str():\n", " return '''def add(a, b):\n", " return a + b\n", "'''\n", "def fancy_func_str():\n", " return '''def fancy_func(a, b, c):\n", " e = add(a, b)\n", " return add(c, e)\n", "'''\n", "def evoke_str():\n", " return add_str() + fancy_func_str() + '''\n", "print(fancy_func(1, 2, 3))\n", "'''\n", "prog = evoke_str()\n", "y = compile(prog, '', 'exec')\n", "exec(y)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "使用 ``HybridSequential`` 类来构建网络。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2019-08-29T16:56:39.724910Z", "start_time": "2019-08-29T16:56:39.364Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.18370919, -0.02053997]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def get_net():\n", " net = nn.HybridSequential()\n", " net.add(nn.Dense(256, activation='relu'),\n", " nn.Dense(128, activation='relu'),\n", " nn.Dense(2))\n", " net.initialize()\n", " return net\n", "\n", "x = np.random.normal(size=(1, 512))\n", "net = get_net()\n", "net(x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "编译优化并运行。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2019-08-29T16:56:39.725781Z", "start_time": "2019-08-29T16:56:39.365Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.18370919, -0.02053997]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "net.hybridize()\n", "net(x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "测试速度。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-08-29T16:56:39.726672Z", "start_time": "2019-08-29T16:56:39.366Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "before hybridizing: 0.6084 sec\n", "after hybridizing: 0.2034 sec\n" ] } ], "source": [ "def benchmark(net, x):\n", " timer = d2l.Timer()\n", " for i in range(1000):\n", " _ = net(x)\n", " npx.waitall()\n", " return timer.stop()\n", "\n", "net = get_net()\n", "print('before hybridizing: %.4f sec' % (benchmark(net, x)))\n", "net.hybridize()\n", "print('after hybridizing: %.4f sec' % (benchmark(net, x)))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "导出到其他语言。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2019-08-29T16:56:39.727577Z", "start_time": "2019-08-29T16:56:39.367Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_mlp-0000.params my_mlp-symbol.json\r\n", "{\r\n", " \"nodes\": [\r\n", " {\r\n", " \"op\": \"null\", \r\n", " \"name\": \"data\", \r\n", " \"inputs\": []\r\n", " }, \r\n", " {\r\n", " \"op\": \"null\", \r\n", " \"name\": \"dense3_weight\", \r\n", " \"attrs\": {\r\n", " \"__dtype__\": \"0\", \r\n", " \"__lr_mult__\": \"1.0\", \r\n", " \"__shape__\": \"(256, -1)\", \r\n", " \"__storage_type__\": \"0\", \r\n", " \"__wd_mult__\": \"1.0\"\r\n", " }, \r\n", " \"inputs\": []\r\n", " }, \r\n", " {\r\n" ] } ], "source": [ "net.export('my_mlp')\n", "!ls my_mlp*\n", "!head -n20 my_mlp-symbol.json" ] } ], "metadata": { "celltoolbar": "Slideshow", "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.7.1" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }