Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 95 additions & 23 deletions docs/guides/02_paddle2.0_develop/05_train_eval_predict_cn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@
"\n",
"模型训练时,需要用到 CPU、 GPU 等计算处理器资源,由于飞桨框架的安装包是区分处理器类型的,默认情况下飞桨框架会根据所安装的版本自动选择对应硬件,比如安装的 GPU 版本的飞桨,则自动使用 GPU 训练模型,无需手动指定。因此一般情况下,无需执行此步骤。\n",
"\n",
"但是如果安装的 GPU 版本的飞桨框架,想切换到 CPU 上训练,则可通过 [paddle.device.set_device](../../api/paddle/device/set_device_cn.html#set-device) 修改,如果本机有多个 GPU 卡,也可以通过该 API 选择指定的卡进行训练。"
"但是如果安装的 GPU 版本的飞桨框架,想切换到 CPU 上训练,则可通过 [paddle.device.set_device](../../api/paddle/device/set_device_cn.html#set-device) 修改,如果本机有多个 GPU 卡,也可以通过该 API 选择指定的卡进行训练。多卡情况下如果不手动指定GPU,默认使用GPU:0。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "0da73e3c-adb6-49bb-9da2-637ab920f558",
"metadata": {
"execution": {
"iopub.execute_input": "2022-02-15T05:48:36.733300Z",
"iopub.status.busy": "2022-02-15T05:48:36.732654Z",
"iopub.status.idle": "2022-02-15T05:48:38.199626Z",
"shell.execute_reply": "2022-02-15T05:48:38.199024Z",
"shell.execute_reply.started": "2022-02-15T05:48:36.733265Z"
"iopub.execute_input": "2022-02-23T03:53:25.061855Z",
"iopub.status.busy": "2022-02-23T03:53:25.061069Z",
"iopub.status.idle": "2022-02-23T03:53:26.486095Z",
"shell.execute_reply": "2022-02-23T03:53:26.485309Z",
"shell.execute_reply.started": "2022-02-23T03:53:25.061819Z"
},
"scrolled": true
},
Expand All @@ -86,7 +86,7 @@
"CPUPlace"
]
},
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -126,15 +126,15 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"id": "c20487eb-ae05-4a35-a459-a00484f02f1b",
"metadata": {
"execution": {
"iopub.execute_input": "2022-02-15T05:48:38.201208Z",
"iopub.status.busy": "2022-02-15T05:48:38.200733Z",
"iopub.status.idle": "2022-02-15T05:48:42.416167Z",
"shell.execute_reply": "2022-02-15T05:48:42.415347Z",
"shell.execute_reply.started": "2022-02-15T05:48:38.201177Z"
"iopub.execute_input": "2022-02-23T04:04:26.920997Z",
"iopub.status.busy": "2022-02-23T04:04:26.920406Z",
"iopub.status.idle": "2022-02-23T04:04:30.892862Z",
"shell.execute_reply": "2022-02-23T04:04:30.892026Z",
"shell.execute_reply.started": "2022-02-23T04:04:26.920963Z"
},
"scrolled": true
},
Expand Down Expand Up @@ -282,14 +282,7 @@
"text": [
"The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n",
"Epoch 1/5\n",
"step 10/938 [..............................] - loss: 0.9679 - acc: 0.4109 - ETA: 13s - 14ms/step"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"step 938/938 [==============================] - loss: 0.1158 - acc: 0.9020 - 10ms/step \n",
"step 10/938 [..............................] - loss: 0.9679 - acc: 0.4109 - ETA: 13s - 14ms/stepstep 938/938 [==============================] - loss: 0.1158 - acc: 0.9020 - 10ms/step \n",
"Epoch 2/5\n",
"step 938/938 [==============================] - loss: 0.0981 - acc: 0.9504 - 10ms/step \n",
"Epoch 3/5\n",
Expand Down Expand Up @@ -744,9 +737,88 @@
"source": [
"## 四、总结\n",
"\n",
"本节中介绍了在飞桨框架中使用高层 API 进行模型训练、评估和推理的方法,并拆解出对应的基础 API 实现方法。实际应用中,飞桨的高层 API 和基础 API 可以组合使用,并不是完全割裂开的,这样有助于开发者更便捷地完成算法迭代。\n",
"本节中介绍了在飞桨框架中使用高层 API 进行模型训练、评估和推理的方法,并拆解出对应的基础 API 实现方法。需要注意的是,这里的推理仅用于实验阶段的模型效果验证,实际生产应用中,部署上线时会使用到部署工具。同时,实际应用中,飞桨的高层 API 和基础 API 可以组合使用,并不是完全割裂开的,这样有助于开发者更便捷地完成算法迭代。例如:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "50b8953f-9df0-4fa3-b420-f5987ad0cf3b",
"metadata": {
"execution": {
"iopub.execute_input": "2022-02-23T04:10:14.866170Z",
"iopub.status.busy": "2022-02-23T04:10:14.865665Z",
"iopub.status.idle": "2022-02-23T04:11:59.489596Z",
"shell.execute_reply": "2022-02-23T04:11:59.488958Z",
"shell.execute_reply.started": "2022-02-23T04:10:14.866136Z"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n",
"Epoch 1/5\n",
"step 938/938 [==============================] - loss: 0.0863 - acc: 0.9369 - 22ms/step \n",
"Eval begin...\n",
"step 157/157 [==============================] - loss: 0.1034 - acc: 0.9741 - 8ms/step \n",
"Eval samples: 10000\n",
"Epoch 2/5\n",
"step 938/938 [==============================] - loss: 0.0366 - acc: 0.9769 - 21ms/step \n",
"Eval begin...\n",
"step 157/157 [==============================] - loss: 0.0047 - acc: 0.9835 - 8ms/step \n",
"Eval samples: 10000\n",
"Epoch 3/5\n",
"step 938/938 [==============================] - loss: 0.0073 - acc: 0.9813 - 21ms/step \n",
"Eval begin...\n",
"step 157/157 [==============================] - loss: 0.0345 - acc: 0.9818 - 9ms/step \n",
"Eval samples: 10000\n",
"Epoch 4/5\n",
"step 938/938 [==============================] - loss: 0.0025 - acc: 0.9842 - 21ms/step \n",
"Eval begin...\n",
"step 157/157 [==============================] - loss: 0.0033 - acc: 0.9834 - 8ms/step \n",
"Eval samples: 10000\n",
"Epoch 5/5\n",
"step 938/938 [==============================] - loss: 0.0294 - acc: 0.9859 - 20ms/step \n",
"Eval begin...\n",
"step 157/157 [==============================] - loss: 0.0039 - acc: 0.9834 - 7ms/step \n",
"Eval samples: 10000\n"
]
}
],
"source": [
"from paddle.vision.models import LeNet\n",
"\n",
"class FaceNet(paddle.nn.Layer):\n",
" def __init__(self):\n",
" super().__init__()\n",
" # 使用高层API组网\n",
" self.backbone = LeNet()\n",
" # 使用基础API组网\n",
" self.outLayer1 = paddle.nn.Sequential(\n",
" paddle.nn.Linear(10, 512),\n",
" paddle.nn.ReLU(),\n",
" paddle.nn.Dropout(0.2)\n",
" )\n",
" self.outLayer2 = paddle.nn.Linear(512, 10)\n",
" \n",
" def forward(self, inputs):\n",
" out = self.backbone(inputs)\n",
" out = self.outLayer1(out)\n",
" out = self.outLayer2(out)\n",
" return out\n",
"# 使用高层API封装网络\n",
"model = paddle.Model(FaceNet())\n",
"# 使用基础API定义优化器\n",
"optim = paddle.optimizer.Adam(learning_rate=1e-3, parameters=model.parameters())\n",
"# 使用高层API封装优化器和损失函数\n",
"model.prepare(optim, paddle.nn.CrossEntropyLoss(), metrics=paddle.metric.Accuracy())\n",
"# 使用高层API训练和评估网络\n",
"model.fit(train_dataset, test_dataset, epochs=5, batch_size=64, verbose=1)"
]
}
],
"metadata": {
Expand Down