Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
fcce28ed
B
book
项目概览
PaddlePaddle
/
book
通知
17
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
fcce28ed
编写于
9月 09, 2020
作者:
D
dingjiaweiww
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop' of
https://github.com/PaddlePaddle/book
into course
上级
4ccb6e7e
1ede63d1
变更
7
展开全部
隐藏空白更改
内联
并排
Showing
7 changed file
with
435 addition
and
697 deletion
+435
-697
paddle2.0_docs/convnet_image_classification/convnet_image_classification.ipynb
...t_image_classification/convnet_image_classification.ipynb
+89
-104
paddle2.0_docs/dynamic_graph/dynamic_graph.ipynb
paddle2.0_docs/dynamic_graph/dynamic_graph.ipynb
+71
-21
paddle2.0_docs/hello_paddle/hello_paddle.ipynb
paddle2.0_docs/hello_paddle/hello_paddle.ipynb
+41
-67
paddle2.0_docs/image_classification/mnist_lenet_classification.ipynb
...ocs/image_classification/mnist_lenet_classification.ipynb
+85
-267
paddle2.0_docs/image_search/image_search.ipynb
paddle2.0_docs/image_search/image_search.ipynb
+62
-70
paddle2.0_docs/imdb_bow_classification/imdb_bow_classification.ipynb
...ocs/imdb_bow_classification/imdb_bow_classification.ipynb
+28
-49
paddle2.0_docs/n_gram_model/n_gram_model.ipynb
paddle2.0_docs/n_gram_model/n_gram_model.ipynb
+59
-119
未找到文件。
paddle2.0_docs/convnet_image_classification/convnet_image_classification.ipynb
浏览文件 @
fcce28ed
此差异已折叠。
点击以展开。
paddle2.0_docs/dynamic_graph/dynamic_graph.ipynb
浏览文件 @
fcce28ed
...
...
@@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count":
6
,
"execution_count":
2
,
"metadata": {},
"outputs": [
{
...
...
@@ -30,7 +30,7 @@
"output_type": "stream",
"text": [
"0.0.0\n",
"
7f2aa2db3c69cb9ebb8bae9e19280e75f964e1d0
\n"
"
89af2088b6e74bdfeef2d4d78e08461ed2aafee5
\n"
]
}
],
...
...
@@ -55,23 +55,23 @@
},
{
"cell_type": "code",
"execution_count":
2
,
"execution_count":
3
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[
0.40741017 0.2083312
]\n",
" [
-1.7567089 0.7211743
6]\n",
" [
0.8870686 -1.1389219
]\n",
" [
1.1233491 0.34348443
]]\n",
"[[
-0.49341336 -0.8112665
]\n",
" [
0.8929015 0.2466117
6]\n",
" [
-0.64440054 -0.7945008
]\n",
" [
-0.07345356 1.3641853
]]\n",
"[1. 2.]\n",
"[[
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"
"[[
0.5065867 1.1887336
]\n",
" [
1.8929014 2.2466118
]\n",
" [
0.35559946 1.2054992
]\n",
" [
0.92654645 3.3641853
]]\n",
"[
-2.1159463 1.386125 -2.2334023 2.654917
]\n"
]
}
],
...
...
@@ -100,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count":
3
,
"execution_count":
4
,
"metadata": {},
"outputs": [
{
...
...
@@ -111,12 +111,12 @@
"1 +> [5 7 9]\n",
"2 +> [ 5 9 15]\n",
"3 -> [-3 3 21]\n",
"4
+> [ 5 21 87
]\n",
"4
-> [-3 11 75
]\n",
"5 +> [ 5 37 249]\n",
"6
-> [ -3 59 723
]\n",
"7
+> [ 5 133 2193
]\n",
"8
-> [ -3 251 6555
]\n",
"9
-> [ -3 507 19677
]\n"
"6
+> [ 5 69 735
]\n",
"7
-> [ -3 123 2181
]\n",
"8
+> [ 5 261 6567
]\n",
"9
+> [ 5 517 19689
]\n"
]
}
],
...
...
@@ -138,15 +138,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# 构建更加灵活的网络\n",
"# 构建更加灵活的网络
:控制流
\n",
"\n",
"- 使用动态图可以用来创建更加灵活的网络,比如根据控制流选择不同的分支网络,和方便的构建权重共享的网络。接下来我们来看一个具体的例子,在这个例子中,第二个线性变换只有0.5的可能性会运行。\n",
"\n"
"
- 在sequence to sequence with attention的机器翻译的示例中,你会看到更实际的使用动态图构建RNN类的网络带来的灵活性。
\n"
]
},
{
"cell_type": "code",
"execution_count":
4
,
"execution_count":
5
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -224,6 +224,56 @@
" model.clear_gradients()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 构建更加灵活的网络:共享权重\n",
"\n",
"- 使用动态图还可以更加方便的创建共享权重的网络,下面的示例展示了一个共享了权重的简单的AutoEncoder的示例。\n",
"- 你也可以参考图像搜索的示例看到共享参数权重的更实际的使用。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"step: 0, loss: [0.37666085]\n",
"step: 1, loss: [0.3063845]\n",
"step: 2, loss: [0.2647248]\n",
"step: 3, loss: [0.23831272]\n",
"step: 4, loss: [0.21714918]\n",
"step: 5, loss: [0.1955545]\n",
"step: 6, loss: [0.17261818]\n",
"step: 7, loss: [0.15009595]\n",
"step: 8, loss: [0.13051331]\n",
"step: 9, loss: [0.11537809]\n"
]
}
],
"source": [
"inputs = paddle.rand((256, 64))\n",
"\n",
"linear = paddle.nn.Linear(64, 8, bias_attr=False)\n",
"loss_fn = paddle.nn.MSELoss()\n",
"optimizer = paddle.optimizer.Adam(0.01, parameters=linear.parameters())\n",
"\n",
"for i in range(10):\n",
" hidden = linear(inputs)\n",
" # weight from input to hidden is shared with the linear mapping from hidden to output\n",
" outputs = paddle.matmul(hidden, linear.weight, transpose_y=True) \n",
" loss = loss_fn(outputs, inputs)\n",
" loss.backward()\n",
" print(\"step: {}, loss: {}\".format(i, loss.numpy()))\n",
" optimizer.minimize(loss)\n",
" linear.clear_gradients()"
]
},
{
"cell_type": "markdown",
"metadata": {},
...
...
paddle2.0_docs/hello_paddle/hello_paddle.ipynb
浏览文件 @
fcce28ed
...
...
@@ -37,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count":
1
,
"execution_count":
24
,
"metadata": {},
"outputs": [
{
...
...
@@ -54,8 +54,8 @@
}
],
"source": [
"def calculate_fee(
minutes_dia
led):\n",
" return 10 + 2 *
minutes_dia
led\n",
"def calculate_fee(
distance_travel
led):\n",
" return 10 + 2 *
distance_travel
led\n",
"\n",
"for x in [1.0, 3.0, 5.0, 9.0, 10.0, 20.0]:\n",
" print(calculate_fee(x))"
...
...
@@ -90,21 +90,20 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 2
5
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"paddle version
2.0.0-alpha
0\n"
"paddle version
0.0.
0\n"
]
}
],
"source": [
"import numpy as np\n",
"import paddle\n",
"\n",
"
paddle.disable_static()
\n",
"print(\"paddle version \" + paddle.__version__)"
]
},
...
...
@@ -117,34 +116,17 @@
"在这个机器学习任务中,我们已经知道了乘客的行驶里程`distance_travelled`,和对应的,这些乘客的总费用`total_fee`。\n",
"通常情况下,在机器学习任务中,像`distance_travelled`这样的输入值,一般被称为`x`(或者特征`feature`),像`total_fee`这样的输出值,一般被称为`y`(或者标签`label`)。\n",
"\n",
"我们用
numpy当中的数组来表示这两组数据,然后用`np.hstack`把这两组数据拼成最终的
数据。"
"我们用
`paddle.to_tensor`把示例数据转换为paddle的Tensor
数据。"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1. 12.]\n",
" [ 3. 16.]\n",
" [ 5. 20.]\n",
" [ 9. 28.]\n",
" [10. 30.]\n",
" [20. 50.]]\n"
]
}
],
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"x_data = np.array([[1.], [3.0], [5.0], [9.0], [10.0], [20.0]]).astype('float32')\n",
"y_data = np.array( [[12.], [16.0], [20.0], [28.0], [30.0], [50.0]]).astype('float32')\n",
"data = np.hstack((x_data, y_data))\n",
"print(data)"
"x_data = paddle.to_tensor([[1.], [3.0], [5.0], [9.0], [10.0], [20.0]])\n",
"y_data = paddle.to_tensor([[12.], [16.0], [20.0], [28.0], [30.0], [50.0]])"
]
},
{
...
...
@@ -168,13 +150,11 @@
},
{
"cell_type": "code",
"execution_count":
4
,
"execution_count":
27
,
"metadata": {},
"outputs": [],
"source": [
"# this line will be removed after 2.0beta's official release\n",
"paddle.enable_imperative()\n",
"linear = paddle.nn.Linear(input_dim=1, output_dim=1)"
"linear = paddle.nn.Linear(in_features=1, out_features=1)"
]
},
{
...
...
@@ -188,24 +168,24 @@
},
{
"cell_type": "code",
"execution_count":
5
,
"execution_count":
28
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"w before optimize:
1.7059897184371948
\n",
"w before optimize:
-1.7107375860214233
\n",
"b before optimize: 0.0\n"
]
}
],
"source": [
"w_before_opt = linear.weight.numpy()
[0][0]
\n",
"b_before_opt = linear.bias.numpy()
[0]
\n",
"w_before_opt = linear.weight.numpy()
.item()
\n",
"b_before_opt = linear.bias.numpy()
.item()
\n",
"\n",
"print(\"w before optimize: {}\".format(w_before_opt))\n",
"print(\"b before optimize: {}\".format(b_before_opt))
\n
"
"print(\"b before optimize: {}\".format(b_before_opt))"
]
},
{
...
...
@@ -220,17 +200,17 @@
"\n",
"用更加技术的语言来说,衡量**差距**的函数(一个公式)就是损失函数,用来**调整**参数的方法就是优化算法。\n",
"\n",
"在本示例当中,我们用最简单的均方误差(mean square error)作为损失函数(`paddle.nn.MSELoss`);和最常见的优化算法SGD(stocastic gradient descent)作为优化算法(传给`paddle.optimizer.SGD
Optimizer
`的参数`learning_rate`,你可以理解为控制每次调整的步子大小的参数)。"
"在本示例当中,我们用最简单的均方误差(mean square error)作为损失函数(`paddle.nn.MSELoss`);和最常见的优化算法SGD(stocastic gradient descent)作为优化算法(传给`paddle.optimizer.SGD`的参数`learning_rate`,你可以理解为控制每次调整的步子大小的参数)。"
]
},
{
"cell_type": "code",
"execution_count":
6
,
"execution_count":
29
,
"metadata": {},
"outputs": [],
"source": [
"mse_loss = paddle.nn.MSELoss()\n",
"sgd_optimizer = paddle.optimizer.SGD
Optimizer(learning_rate=0.001, parameter_list
= linear.parameters())"
"sgd_optimizer = paddle.optimizer.SGD
(learning_rate=0.001, parameters
= linear.parameters())"
]
},
{
...
...
@@ -244,41 +224,35 @@
},
{
"cell_type": "code",
"execution_count":
7
,
"execution_count":
30
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch 0
average loss [155.91637
]\n",
"epoch 1000
average loss [8.280919
]\n",
"epoch 2000
average loss [1.851555
]\n",
"epoch 3000
average loss [0.4139988
]\n",
"epoch 4000
average loss [0.09256991
]\n",
"finished training,
average loss [0.02072961
]\n"
"epoch 0
loss [2107.3943
]\n",
"epoch 1000
loss [7.8432994
]\n",
"epoch 2000
loss [1.7537074
]\n",
"epoch 3000
loss [0.39211753
]\n",
"epoch 4000
loss [0.08767726
]\n",
"finished training,
loss [0.01963376
]\n"
]
}
],
"source": [
"TOTAL_EPOCH = 5000\n",
"for i in range(TOTAL_EPOCH):\n",
" # 每个epoch对数据进行随机打乱,会让优化算法更好的前进。\n",
" np.random.shuffle(data)\n",
" # 把numpy数据转换为paddle的tensor数据\n",
" x = paddle.imperative.to_variable(data[:,:1])\n",
" y = paddle.imperative.to_variable(data[:,1:])\n",
" \n",
" y_predict = linear(x)\n",
" loss = mse_loss(y_predict, y)\n",
"total_epoch = 5000\n",
"for i in range(total_epoch):\n",
" y_predict = linear(x_data)\n",
" loss = mse_loss(y_predict, y_data)\n",
" loss.backward()\n",
" sgd_optimizer.minimize(loss)\n",
" linear.clear_gradients()\n",
" \n",
" if i%1000 == 0:\n",
" print(\"epoch {}
average
loss {}\".format(i, loss.numpy()))\n",
" print(\"epoch {} loss {}\".format(i, loss.numpy()))\n",
" \n",
"print(\"finished training,
average
loss {}\".format(loss.numpy()))"
"print(\"finished training, loss {}\".format(loss.numpy()))"
]
},
{
...
...
@@ -292,21 +266,21 @@
},
{
"cell_type": "code",
"execution_count":
8
,
"execution_count":
31
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"w after optimize: 2.01
8333911895752
\n",
"b after optimize: 9.7
65570640563965
\n"
"w after optimize: 2.01
7843246459961
\n",
"b after optimize: 9.7
71851539611816
\n"
]
}
],
"source": [
"w_after_opt = linear.weight.numpy()
[0][0]
\n",
"b_after_opt = linear.bias.numpy()
[0]
\n",
"w_after_opt = linear.weight.numpy()
.item()
\n",
"b_after_opt = linear.bias.numpy()
.item()
\n",
"\n",
"print(\"w after optimize: {}\".format(w_after_opt))\n",
"print(\"b after optimize: {}\".format(b_after_opt))\n"
...
...
@@ -323,7 +297,7 @@
},
{
"cell_type": "code",
"execution_count":
9
,
"execution_count":
32
,
"metadata": {},
"outputs": [
{
...
...
paddle2.0_docs/image_classification/mnist_lenet_classification.ipynb
浏览文件 @
fcce28ed
此差异已折叠。
点击以展开。
paddle2.0_docs/image_search/image_search.ipynb
浏览文件 @
fcce28ed
此差异已折叠。
点击以展开。
paddle2.0_docs/imdb_bow_classification/imdb_bow_classification.ipynb
浏览文件 @
fcce28ed
...
...
@@ -25,14 +25,15 @@
},
{
"cell_type": "code",
"execution_count":
1
,
"execution_count":
4
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.0.0-alpha0\n"
"0.0.0\n",
"264e76cae6861ad9b1d4bcd8c3212f7a78c01e4d\n"
]
}
],
...
...
@@ -40,8 +41,9 @@
"import paddle\n",
"import numpy as np\n",
"\n",
"paddle.disable_static()\n",
"print(paddle.__version__)\n",
"p
addle.enable_imperative()
"
"p
rint(paddle.__git_commit__)\n
"
]
},
{
...
...
@@ -55,7 +57,7 @@
},
{
"cell_type": "code",
"execution_count":
2
,
"execution_count":
5
,
"metadata": {},
"outputs": [
{
...
...
@@ -76,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count":
3
,
"execution_count":
6
,
"metadata": {},
"outputs": [
{
...
...
@@ -124,7 +126,7 @@
},
{
"cell_type": "code",
"execution_count":
4
,
"execution_count":
7
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -155,7 +157,7 @@
},
{
"cell_type": "code",
"execution_count":
5
,
"execution_count":
8
,
"metadata": {},
"outputs": [
{
...
...
@@ -188,7 +190,7 @@
},
{
"cell_type": "code",
"execution_count":
6
,
"execution_count":
9
,
"metadata": {},
"outputs": [
{
...
...
@@ -239,16 +241,16 @@
},
{
"cell_type": "code",
"execution_count":
7
,
"execution_count":
11
,
"metadata": {},
"outputs": [],
"source": [
"class MyNet(paddle.nn.Layer):\n",
" def __init__(self):\n",
" super(MyNet, self).__init__()\n",
" self.emb = paddle.nn.Embedding(
size=[vocab_size, emb_size],
)\n",
" self.fc = paddle.nn.Linear(in
put_dim=emb_size, output_dim
=2)\n",
" self.dropout = paddle.
fluid.dygraph
.Dropout(0.5)\n",
" self.emb = paddle.nn.Embedding(
vocab_size, emb_size
)\n",
" self.fc = paddle.nn.Linear(in
_features=emb_size, out_features
=2)\n",
" self.dropout = paddle.
nn
.Dropout(0.5)\n",
"\n",
" def forward(self, x):\n",
" x = self.emb(x)\n",
...
...
@@ -262,41 +264,32 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# 开始模型的训练\n",
"\n",
"- TODO: make this part shorter by using `fit`"
"# 开始模型的训练\n"
]
},
{
"cell_type": "code",
"execution_count":
9
,
"execution_count":
13
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch: 0, batch_id: 0, loss is: [0.69
32132
]\n",
"epoch: 0, batch_id: 500, loss is: [0.4
0177184
]\n",
"[validation] accuracy/loss: 0.8
407090306282043/0.3917059302330017
\n",
"epoch: 1, batch_id: 0, loss is: [0.
30510694
]\n",
"epoch: 1, batch_id: 500, loss is: [0.
3284214
]\n",
"[validation] accuracy/loss: 0.8
573943376541138/0.3381407558917999
\n"
"epoch: 0, batch_id: 0, loss is: [0.69
26701
]\n",
"epoch: 0, batch_id: 500, loss is: [0.4
1248566
]\n",
"[validation] accuracy/loss: 0.8
505121469497681/0.3615057170391083
\n",
"epoch: 1, batch_id: 0, loss is: [0.
29521096
]\n",
"epoch: 1, batch_id: 500, loss is: [0.
2916747
]\n",
"[validation] accuracy/loss: 0.8
6475670337677/0.3259459137916565
\n"
]
}
],
"source": [
"def train(model):\n",
" # 这个函数是把模型设置为训练模式的。\n",
" model.train()\n",
"\n",
"\n",
" opt = paddle.optimizer.Adam(learning_rate=0.001, \n",
" beta1=0.9, \n",
" beta2=0.999, \n",
" epsilon=1e-07,\n",
" parameter_list=model.parameters())\n",
"\n",
" opt = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())\n",
"\n",
" for epoch in range(epoch_num):\n",
" # shuffle data\n",
...
...
@@ -308,8 +301,8 @@
" x_data = train_sents_shuffled[(batch_id * batch_size):((batch_id+1)*batch_size)]\n",
" y_data = train_labels_shuffled[(batch_id * batch_size):((batch_id+1)*batch_size)]\n",
" \n",
" sent = paddle.
imperative.to_variable
(x_data)\n",
" label = paddle.
imperative.to_variable
(y_data)\n",
" sent = paddle.
to_tensor
(x_data)\n",
" label = paddle.
to_tensor
(y_data)\n",
" \n",
" logits = model(sent)\n",
" loss = paddle.nn.functional.softmax_with_cross_entropy(logits, label)\n",
...
...
@@ -329,14 +322,12 @@
" x_data = test_sents[(batch_id * batch_size):((batch_id+1)*batch_size)]\n",
" y_data = test_labels[(batch_id * batch_size):((batch_id+1)*batch_size)]\n",
" \n",
" sent = paddle.
imperative.to_variable
(x_data)\n",
" label = paddle.
imperative.to_variable
(y_data)\n",
" sent = paddle.
to_tensor
(x_data)\n",
" label = paddle.
to_tensor
(y_data)\n",
"\n",
" logits = model(sent)\n",
" pred = paddle.nn.functional.softmax(logits)\n",
"\n",
" loss = paddle.nn.functional.softmax_with_cross_entropy(logits, label)\n",
" acc = paddle.metric.accuracy(
pred
, label)\n",
" acc = paddle.metric.accuracy(
logits
, label)\n",
" \n",
" accuracies.append(acc.numpy())\n",
" losses.append(loss.numpy())\n",
...
...
@@ -378,18 +369,6 @@
"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,
...
...
paddle2.0_docs/n_gram_model/n_gram_model.ipynb
浏览文件 @
fcce28ed
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录