未验证 提交 d0363589 编写于 作者: J jzhang533 提交者: GitHub

update several notebooks (#890)

* change from Pool2D to AdaptiveAvgPool2d in image_search

* hello paddle updated

* convnet_image_classification updated

* dynamic_graph updated

* convnet_image_classification updated

* imdb bow classification updated
上级 3fa53567
......@@ -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.72117436]\n",
" [ 0.8870686 -1.1389219 ]\n",
" [ 1.1233491 0.34348443]]\n",
"[[-0.49341336 -0.8112665 ]\n",
" [ 0.8929015 0.24661176]\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": {},
......
......@@ -37,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 24,
"metadata": {},
"outputs": [
{
......@@ -54,8 +54,8 @@
}
],
"source": [
"def calculate_fee(minutes_dialed):\n",
" return 10 + 2 * minutes_dialed\n",
"def calculate_fee(distance_travelled):\n",
" return 10 + 2 * distance_travelled\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": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"paddle version 2.0.0-alpha0\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.SGDOptimizer`的参数`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.SGDOptimizer(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.018333911895752\n",
"b after optimize: 9.765570640563965\n"
"w after optimize: 2.017843246459961\n",
"b after optimize: 9.771851539611816\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": [
{
......
......@@ -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",
"paddle.enable_imperative()"
"print(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(input_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.6932132]\n",
"epoch: 0, batch_id: 500, loss is: [0.40177184]\n",
"[validation] accuracy/loss: 0.8407090306282043/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.8573943376541138/0.3381407558917999\n"
"epoch: 0, batch_id: 0, loss is: [0.6926701]\n",
"epoch: 0, batch_id: 500, loss is: [0.41248566]\n",
"[validation] accuracy/loss: 0.8505121469497681/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.86475670337677/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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册