dynamic_graph.ipynb 6.9 KB
Notebook
Newer Older
J
jzhang533 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 动态图\n",
    "\n",
    "从飞桨开源框架2.0beta版本开始,飞桨默认为用户开启了动态图模式。在这种模式下,每次执行一个运算,可以立即得到结果(而不是事先定义好网络结构,然后再执行)。\n",
    "\n",
    "在动态图模式下,您可以更加方便的组织代码,更容易的调试程序,本示例教程将向你介绍飞桨的动态图的使用。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 设置环境\n",
    "\n",
    "我们将使用飞桨2.0beta版本,并确认已经开启了动态图模式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0.0\n",
33
      "7f2aa2db3c69cb9ebb8bae9e19280e75f964e1d0\n"
J
jzhang533 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "import paddle.nn.functional as F\n",
    "import numpy as np\n",
    "\n",
    "paddle.disable_static()\n",
    "print(paddle.__version__)\n",
    "print(paddle.__git_commit__)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基本用法\n",
    "\n",
    "在动态图模式下,您可以直接运行一个飞桨提供的API,它会立刻返回结果到python。不再需要首先创建一个计算图,然后再给定数据去运行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
65 66 67 68
      "[[ 0.40741017  0.2083312 ]\n",
      " [-1.7567089   0.72117436]\n",
      " [ 0.8870686  -1.1389219 ]\n",
      " [ 1.1233491   0.34348443]]\n",
J
jzhang533 已提交
69
      "[1. 2.]\n",
70 71 72 73 74
      "[[ 1.4074101   2.208331  ]\n",
      " [-0.75670886  2.7211742 ]\n",
      " [ 1.8870686   0.86107814]\n",
      " [ 2.1233492   2.3434844 ]]\n",
      "[ 0.8240726  -0.31436014 -1.3907751   1.810318  ]\n"
J
jzhang533 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
     ]
    }
   ],
   "source": [
    "a = paddle.randn([4, 2])\n",
    "b = paddle.arange(1, 3, dtype='float32')\n",
    "\n",
    "print(a.numpy())\n",
    "print(b.numpy())\n",
    "\n",
    "c = a + b\n",
    "print(c.numpy())\n",
    "\n",
    "d = paddle.matmul(a, b)\n",
    "print(d.numpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用python的控制流\n",
    "\n",
    "动态图模式下,您可以使用python的条件判断和循环,这类控制语句来执行神经网络的计算。(不再需要`cond`, `loop`这类OP)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 +> [5 6 7]\n",
111 112 113 114
      "1 +> [5 7 9]\n",
      "2 +> [ 5  9 15]\n",
      "3 -> [-3  3 21]\n",
      "4 +> [ 5 21 87]\n",
J
jzhang533 已提交
115 116
      "5 +> [  5  37 249]\n",
      "6 -> [ -3  59 723]\n",
117
      "7 +> [   5  133 2193]\n",
J
jzhang533 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
      "8 -> [  -3  251 6555]\n",
      "9 -> [   -3   507 19677]\n"
     ]
    }
   ],
   "source": [
    "a = paddle.to_tensor(np.array([1, 2, 3]))\n",
    "b = paddle.to_tensor(np.array([4, 5, 6]))\n",
    "\n",
    "for i in range(10):\n",
    "    r = paddle.rand([1,])\n",
    "    if r > 0.5:\n",
    "        c = paddle.pow(a, i) + b\n",
    "        print(\"{} +> {}\".format(i, c.numpy()))\n",
    "    else:\n",
    "        c = paddle.pow(a, i) - b\n",
    "        print(\"{} -> {}\".format(i, c.numpy()))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 构建更加灵活的网络\n",
    "\n",
    "- 使用动态图可以用来创建更加灵活的网络,比如根据控制流选择不同的分支网络,和方便的构建权重共享的网络。接下来我们来看一个具体的例子,在这个例子中,第二个线性变换只有0.5的可能性会运行。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyModel(paddle.nn.Layer):\n",
    "    def __init__(self, input_size, hidden_size):\n",
    "        super(MyModel, self).__init__()\n",
    "        self.linear1 = paddle.nn.Linear(input_size, hidden_size)\n",
    "        self.linear2 = paddle.nn.Linear(hidden_size, hidden_size)\n",
    "        self.linear3 = paddle.nn.Linear(hidden_size, 1)\n",
    "\n",
    "    def forward(self, inputs):\n",
    "        x = self.linear1(inputs)\n",
    "        x = F.relu(x)\n",
    "\n",
    "        if paddle.rand([1,]) > 0.5: \n",
    "            x = self.linear2(x)\n",
    "            x = F.relu(x)\n",
    "\n",
    "        x = self.linear3(x)\n",
    "        \n",
    "        return x     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
      "0 [2.0915627]\n",
      "200 [0.67530334]\n",
      "400 [0.52042854]\n",
      "600 [0.28010666]\n",
      "800 [0.09739777]\n",
      "1000 [0.09307177]\n",
      "1200 [0.04252927]\n",
      "1400 [0.03095707]\n",
      "1600 [0.03022156]\n",
      "1800 [0.01616007]\n",
      "2000 [0.01069116]\n",
      "2200 [0.0055158]\n",
      "2400 [0.00195092]\n",
      "2600 [0.00101116]\n",
      "2800 [0.00192219]\n"
J
jzhang533 已提交
197 198 199 200 201 202 203 204 205 206 207
     ]
    }
   ],
   "source": [
    "total_data, batch_size, input_size, hidden_size = 1000, 64, 128, 256\n",
    "\n",
    "x_data = np.random.randn(total_data, input_size).astype(np.float32)\n",
    "y_data = np.random.randn(total_data, 1).astype(np.float32)\n",
    "\n",
    "model = MyModel(input_size, hidden_size)\n",
    "\n",
208 209 210
    "loss_fn = paddle.nn.MSELoss(reduction='mean')\n",
    "optimizer = paddle.optimizer.SGD(learning_rate=0.01, \n",
    "                                 parameters=model.parameters())\n",
J
jzhang533 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    "\n",
    "for t in range(200 * (total_data // batch_size)):\n",
    "    idx = np.random.choice(total_data, batch_size, replace=False)\n",
    "    x = paddle.to_tensor(x_data[idx,:])\n",
    "    y = paddle.to_tensor(y_data[idx,:])\n",
    "    y_pred = model(x)\n",
    "\n",
    "    loss = loss_fn(y_pred, y)\n",
    "    if t % 200 == 0:\n",
    "        print(t, loss.numpy())\n",
    "\n",
    "    loss.backward()\n",
    "    optimizer.minimize(loss)\n",
    "    model.clear_gradients()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The end\n",
    "\n",
    "可以看到使用动态图带来了更灵活易用的方式来组网和训练。"
   ]
  }
 ],
 "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}