This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
{ | ||
"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", | ||
"edf5f3173a25ae2230e9619ab5426317b4bd7cde\n" | ||
] | ||
} | ||
], | ||
"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": [ | ||
"[[-1.4396907 -0.59741247]\n", | ||
" [ 1.4717171 -0.06998838]\n", | ||
" [-1.2790705 0.4278928 ]\n", | ||
" [ 1.1862146 -1.895377 ]]\n", | ||
"[1. 2.]\n", | ||
"[[-0.4396907 1.4025875 ]\n", | ||
" [ 2.4717171 1.9300116 ]\n", | ||
" [-0.2790705 2.4278927 ]\n", | ||
" [ 2.1862144 0.10462296]]\n", | ||
"[-2.6345158 1.3317404 -0.4232849 -2.6045394]\n" | ||
] | ||
} | ||
], | ||
"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", | ||
"1 -> [-3 -3 -3]\n", | ||
"2 -> [-3 -1 3]\n", | ||
"3 +> [ 5 13 33]\n", | ||
"4 -> [-3 11 75]\n", | ||
"5 +> [ 5 37 249]\n", | ||
"6 -> [ -3 59 723]\n", | ||
"7 -> [ -3 123 2181]\n", | ||
"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": [ | ||
"0 [87.28088]\n", | ||
"200 [57.775795]\n", | ||
"400 [42.70884]\n", | ||
"600 [45.509155]\n", | ||
"800 [29.966158]\n", | ||
"1000 [11.885025]\n", | ||
"1200 [16.888378]\n", | ||
"1400 [3.5780585]\n", | ||
"1600 [5.3149533]\n", | ||
"1800 [4.501356]\n", | ||
"2000 [3.022315]\n", | ||
"2200 [1.7214009]\n", | ||
"2400 [0.3694626]\n", | ||
"2600 [0.31249344]\n", | ||
"2800 [0.1450614]\n" | ||
] | ||
} | ||
], | ||
"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", | ||
"loss_fn = paddle.nn.MSELoss(reduction='sum')\n", | ||
"optimizer = paddle.optimizer.SGD(learning_rate=0.0001, \n", | ||
" parameter_list=model.parameters())\n", | ||
"\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 | ||
} |