diff --git a/paddle2.0_docs/getting_started/getting_started.ipynb b/paddle2.0_docs/getting_started/getting_started.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..f2a27f4d2d297a037ab338093856eec453590370 --- /dev/null +++ b/paddle2.0_docs/getting_started/getting_started.ipynb @@ -0,0 +1,219 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 快速上手飞桨(PaddlePaddle)\n", + "\n", + "本示例通过一个基础案例带您从一个飞桨新手快速掌握如何使用。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1. 安装飞桨\n", + "\n", + "如果您已经安装好飞桨那么可以跳过此步骤。我们针对用户提供了一个方便易用的安装引导页面,您可以通过选择自己的系统和软件版本来获取对应的安装命令,具体可以点击[快速安装](https://www.paddlepaddle.org.cn/install/quick)查看。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 2. 导入飞桨\n", + "\n", + "这个示例我们采用了Notebook的形式来进行编写,您可以直接通过AIStudio或Jupyter等平台工具来运行这个案例,Notebook的好处是可以通过浏览器来运行Python程序,边看教程边运行结果,可以对比学习,并且可以做到单步运行调试。\n", + "\n", + "安装好飞桨后我们就可以在Python程序中进行飞桨的导入。" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'0.0.0'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import paddle\n", + "\n", + "paddle.__version__" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3. 实践一个手写数字识别任务\n", + "\n", + "对于深度学习任务如果简单来看,其实分为几个核心步骤:1. 数据集的准备和加载;2. 模型的构建;3.模型训练;4.模型评估。那么接下来我们就一步一步带您通过飞桨的少量API快速实现。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3.1 数据加载\n", + "\n", + "加载我们框架为您准备好的一个手写数字识别数据集。这里我们使用两个数据集,一个用来做模型的训练,一个用来做模型的评估。" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "train_dataset = paddle.vision.datasets.MNIST(mode='train', chw_format=False)\n", + "val_dataset = paddle.vision.datasets.MNIST(mode='test', chw_format=False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3.2 模型搭建\n", + "\n", + "通过Sequential将一层一层的网络结构组建起来。通过数据集加载接口的chw_format参数我们已经将[1, 28, 28]形状的图片数据改变形状为[1, 784],那么在组网过程中不在需要先进行Flatten操作。" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "mnist = paddle.nn.Sequential(\n", + " paddle.nn.Linear(784, 512),\n", + " paddle.nn.ReLU(),\n", + " paddle.nn.Dropout(0.2),\n", + " paddle.nn.Linear(512, 10)\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3.3 模型训练\n", + "\n", + "配置好我们模型训练需要的损失计算方法和优化方法后就可以使用fit接口来开启我们的模型训练过程。" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/5\n", + "step 1875/1875 [==============================] - loss: 0.2571 - acc: 0.9037 - 10ms/step \n", + "Epoch 2/5\n", + "step 1875/1875 [==============================] - loss: 0.1880 - acc: 0.9458 - 14ms/step \n", + "Epoch 3/5\n", + "step 1875/1875 [==============================] - loss: 0.0279 - acc: 0.9549 - 11ms/step \n", + "Epoch 4/5\n", + "step 1875/1875 [==============================] - loss: 0.0505 - acc: 0.9608 - 13ms/step \n", + "Epoch 5/5\n", + "step 1875/1875 [==============================] - loss: 0.2253 - acc: 0.9646 - 12ms/step \n" + ] + } + ], + "source": [ + "# 开启动态图模式\n", + "paddle.disable_static() \n", + "\n", + "# 预计模型结构生成模型实例,便于进行后续的配置、训练和验证\n", + "model = paddle.Model(mnist) \n", + "\n", + "# 模型训练相关配置,准备损失计算方法,优化器和精度计算方法\n", + "model.prepare(paddle.optimizer.Adam(parameters=mnist.parameters()),\n", + " paddle.nn.CrossEntropyLoss(),\n", + " paddle.metric.Accuracy())\n", + "\n", + "# 开始模型训练\n", + "model.fit(train_dataset,\n", + " epochs=5, \n", + " batch_size=32,\n", + " verbose=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3.4 模型评估\n", + "\n", + "使用我们刚才训练得到的模型参数进行模型的评估操作,看看我们的模型精度如何。" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'loss': [3.576278e-07], 'acc': 0.9666}" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model.evaluate(val_dataset, verbose=0)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "那么初步训练得到的模型效果在97%附近,我们可以进一步通过调整其中的训练参数来提升我们的模型精度。\n", + "\n", + "至此我们可以知道如何通过飞桨的几个简单API来快速完成一个深度学习任务,大家可以针对自己的需求来更换其中的代码,如果需要使用自己的数据集,那么可以更换数据集加载部分程序,如果需要替换模型,那么可以更改模型代码实现等等。我们也为大家提供了很多其他场景的示例代码来教大家如何使用我们的飞桨API,大家可以查看下面的链接或通过页面导航来查看自己感兴趣的部分。\n", + "\n", + "TODO:补充其他示例教程的快速链接。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.7.4 64-bit", + "language": "python", + "name": "python37464bitc4da1ac836094043840bff631bedbf7f" + }, + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}