mnist_lenet_classification.ipynb 19.2 KB
Notebook
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MNIST数据集使用LeNet进行图像分类\n",
    "本示例教程演示如何在MNIST数据集上用LeNet进行图像分类。\n",
    "手写数字的MNIST数据集,包含60,000个用于训练的示例和10,000个用于测试的示例。这些数字已经过尺寸标准化并位于图像中心,图像是固定大小(28x28像素),其值为0到1。该数据集的官方地址为:http://yann.lecun.com/exdb/mnist/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
C
Chen Long 已提交
16
    "## 环境\n",
17 18 19 20 21
    "本教程基于paddle-develop编写,如果您的环境不是本版本,请先安装paddle-develop版本。"
   ]
  },
  {
   "cell_type": "code",
C
Chen Long 已提交
22
   "execution_count": 13,
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0.0\n"
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "print(paddle.__version__)\n",
    "paddle.disable_static()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
C
Chen Long 已提交
43
    "## 加载数据集\n",
44 45 46 47 48
    "我们使用飞桨自带的paddle.dataset完成mnist数据集的加载。"
   ]
  },
  {
   "cell_type": "code",
C
Chen Long 已提交
49
   "execution_count": 14,
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "download training data and load training data\n",
      "load finished\n"
     ]
    }
   ],
   "source": [
    "print('download training data and load training data')\n",
    "train_dataset = paddle.vision.datasets.MNIST(mode='train')\n",
    "test_dataset = paddle.vision.datasets.MNIST(mode='test')\n",
    "print('load finished')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "取训练集中的一条数据看一下。"
   ]
  },
  {
   "cell_type": "code",
C
Chen Long 已提交
77
   "execution_count": 15,
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train_data0 label is: [5]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAI4AAACOCAYAAADn/TAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAIY0lEQVR4nO3dXWhUZxoH8P/jaPxav7KREtNgiooQFvwg1l1cNOr6sQUN3ixR0VUK9cKPXTBYs17ohReLwl5ovCmuZMU1y+IaWpdC0GIuxCJJMLhJa6oWtSl+FVEXvdDK24s5nc5zapKTZ86cOTPz/4Hk/M8xc17w8Z13zpl5RpxzIBquEbkeAOUnFg6ZsHDIhIVDJiwcMmHhkElGhSMiq0WkT0RuisjesAZF8SfW6zgikgDwFYAVAPoBdABY75z7IrzhUVyNzOB33wVw0zn3NQCIyL8A1AEYsHDKyspcVVVVBqekqHV1dX3nnJvq359J4VQA+CYt9wNYONgvVFVVobOzM4NTUtRE5M6b9md9cSwiH4hIp4h0Pnr0KNuno4hkUjjfAqhMy297+xTn3EfOuRrnXM3UqT+b8ShPZVI4HQBmicg7IlICoB7AJ+EMi+LOvMZxzn0vIjsAtAFIADjhnOsNbWQUa5ksjuGc+xTApyGNhfIIrxyTCQuHTFg4ZMLCIRMWDpmwcMiEhUMmLBwyYeGQCQuHTFg4ZMLCIZOMbnIWk9evX6v89OnTwL/b1NSk8osXL1Tu6+tT+dixYyo3NDSo3NLSovKYMWNU3rv3p88N7N+/P/A4h4MzDpmwcMiEhUMmRbPGuXv3rsov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JC4dMWDhkwsIhkx8AyyZIbO5tLBIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 144x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "train_data0, train_label_0 = train_dataset[0][0],train_dataset[0][1]\n",
    "train_data0 = train_data0.reshape([28,28])\n",
    "plt.figure(figsize=(2,2))\n",
    "plt.imshow(train_data0, cmap=plt.cm.binary)\n",
    "print('train_data0 label is: ' + str(train_label_0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
C
Chen Long 已提交
114
    "## 组网\n",
C
Chen Long 已提交
115
    "用paddle.nn下的API,如`Conv2d`、`MaxPool2d`、`Linear`完成LeNet的构建。"
116 117 118 119
   ]
  },
  {
   "cell_type": "code",
C
Chen Long 已提交
120
   "execution_count": 18,
121 122 123 124 125 126 127 128 129
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle\n",
    "import paddle.nn.functional as F\n",
    "class LeNet(paddle.nn.Layer):\n",
    "    def __init__(self):\n",
    "        super(LeNet, self).__init__()\n",
    "        self.conv1 = paddle.nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, stride=1, padding=2)\n",
C
Chen Long 已提交
130
    "        self.max_pool1 = paddle.nn.MaxPool2d(kernel_size=2,  stride=2)\n",
131
    "        self.conv2 = paddle.nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5, stride=1)\n",
C
Chen Long 已提交
132
    "        self.max_pool2 = paddle.nn.MaxPool2d(kernel_size=2, stride=2)\n",
133 134 135 136 137 138 139 140 141 142 143
    "        self.linear1 = paddle.nn.Linear(in_features=16*5*5, out_features=120)\n",
    "        self.linear2 = paddle.nn.Linear(in_features=120, out_features=84)\n",
    "        self.linear3 = paddle.nn.Linear(in_features=84, out_features=10)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.conv1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.max_pool1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.conv2(x)\n",
    "        x = self.max_pool2(x)\n",
C
Chen Long 已提交
144
    "        x = paddle.flatten(x, start_axis=1,stop_axis=-1)\n",
145 146 147 148 149 150 151 152 153 154 155 156 157
    "        x = self.linear1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.linear2(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.linear3(x)\n",
    "        x = F.softmax(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
C
Chen Long 已提交
158
    "## 训练方式一\n",
C
Chen Long 已提交
159
    "组网后,开始对模型进行训练,先构建`train_loader`,加载训练数据,然后定义`train`函数,设置好损失函数后,按batch加载数据,完成模型的训练。"
160 161 162 163
   ]
  },
  {
   "cell_type": "code",
C
Chen Long 已提交
164
   "execution_count": 19,
C
Chen Long 已提交
165 166 167 168 169 170
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
C
Chen Long 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
      "epoch: 0, batch_id: 0, loss is: [2.3079572], acc is: [0.125]\n",
      "epoch: 0, batch_id: 100, loss is: [1.7078608], acc is: [0.828125]\n",
      "epoch: 0, batch_id: 200, loss is: [1.5642334], acc is: [0.90625]\n",
      "epoch: 0, batch_id: 300, loss is: [1.7024238], acc is: [0.78125]\n",
      "epoch: 0, batch_id: 400, loss is: [1.5536337], acc is: [0.921875]\n",
      "epoch: 0, batch_id: 500, loss is: [1.6908336], acc is: [0.828125]\n",
      "epoch: 0, batch_id: 600, loss is: [1.5622432], acc is: [0.921875]\n",
      "epoch: 0, batch_id: 700, loss is: [1.5251796], acc is: [0.953125]\n",
      "epoch: 0, batch_id: 800, loss is: [1.5698484], acc is: [0.890625]\n",
      "epoch: 0, batch_id: 900, loss is: [1.5524453], acc is: [0.9375]\n",
      "epoch: 1, batch_id: 0, loss is: [1.6443151], acc is: [0.84375]\n",
      "epoch: 1, batch_id: 100, loss is: [1.5547533], acc is: [0.90625]\n",
      "epoch: 1, batch_id: 200, loss is: [1.5019028], acc is: [1.]\n",
      "epoch: 1, batch_id: 300, loss is: [1.4820204], acc is: [1.]\n",
      "epoch: 1, batch_id: 400, loss is: [1.5215418], acc is: [0.984375]\n",
      "epoch: 1, batch_id: 500, loss is: [1.4972374], acc is: [1.]\n",
      "epoch: 1, batch_id: 600, loss is: [1.4930981], acc is: [0.984375]\n",
      "epoch: 1, batch_id: 700, loss is: [1.4971689], acc is: [0.984375]\n",
      "epoch: 1, batch_id: 800, loss is: [1.4611597], acc is: [1.]\n",
      "epoch: 1, batch_id: 900, loss is: [1.4903957], acc is: [0.984375]\n"
C
Chen Long 已提交
191 192 193 194 195
     ]
    }
   ],
   "source": [
    "import paddle\n",
C
Chen Long 已提交
196
    "train_loader = paddle.io.DataLoader(train_dataset, places=paddle.CPUPlace(), batch_size=64, shuffle=True)\n",
C
Chen Long 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    "# 加载训练集 batch_size 设为 64\n",
    "def train(model):\n",
    "    model.train()\n",
    "    epochs = 2\n",
    "    optim = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())\n",
    "    # 用Adam作为优化函数\n",
    "    for epoch in range(epochs):\n",
    "        for batch_id, data in enumerate(train_loader()):\n",
    "            x_data = data[0]\n",
    "            y_data = data[1]\n",
    "            predicts = model(x_data)\n",
    "            loss = paddle.nn.functional.cross_entropy(predicts, y_data)\n",
    "            # 计算损失\n",
    "            acc = paddle.metric.accuracy(predicts, y_data, k=2)\n",
    "            avg_loss = paddle.mean(loss)\n",
    "            avg_acc = paddle.mean(acc)\n",
    "            avg_loss.backward()\n",
    "            if batch_id % 100 == 0:\n",
    "                print(\"epoch: {}, batch_id: {}, loss is: {}, acc is: {}\".format(epoch, batch_id, avg_loss.numpy(), avg_acc.numpy()))\n",
    "            optim.minimize(avg_loss)\n",
    "            model.clear_gradients()\n",
    "model = LeNet()\n",
    "train(model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对模型进行验证\n",
    "训练完成后,需要验证模型的效果,此时,加载测试数据集,然后用训练好的模对测试集进行预测,计算损失与精度。"
   ]
  },
  {
   "cell_type": "code",
C
Chen Long 已提交
232
   "execution_count": 20,
C
Chen Long 已提交
233 234 235 236 237 238
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
C
Chen Long 已提交
239 240 241 242 243 244 245 246
      "batch_id: 0, loss is: [1.4767745], acc is: [1.]\n",
      "batch_id: 20, loss is: [1.4841802], acc is: [0.984375]\n",
      "batch_id: 40, loss is: [1.4997194], acc is: [1.]\n",
      "batch_id: 60, loss is: [1.4895413], acc is: [1.]\n",
      "batch_id: 80, loss is: [1.4668798], acc is: [1.]\n",
      "batch_id: 100, loss is: [1.4611752], acc is: [1.]\n",
      "batch_id: 120, loss is: [1.4613602], acc is: [1.]\n",
      "batch_id: 140, loss is: [1.4923686], acc is: [1.]\n"
C
Chen Long 已提交
247 248 249 250 251 252 253 254 255 256
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "test_loader = paddle.io.DataLoader(test_dataset, places=paddle.CPUPlace(), batch_size=64)\n",
    "# 加载测试数据集\n",
    "def test(model):\n",
    "    model.eval()\n",
    "    batch_size = 64\n",
C
Chen Long 已提交
257
    "    for batch_id, data in enumerate(test_loader()):\n",
C
Chen Long 已提交
258 259 260 261 262 263 264 265 266
    "        x_data = data[0]\n",
    "        y_data = data[1]\n",
    "        predicts = model(x_data)\n",
    "        # 获取预测结果\n",
    "        loss = paddle.nn.functional.cross_entropy(predicts, y_data)\n",
    "        acc = paddle.metric.accuracy(predicts, y_data, k=2)\n",
    "        avg_loss = paddle.mean(loss)\n",
    "        avg_acc = paddle.mean(acc)\n",
    "        avg_loss.backward()\n",
C
Chen Long 已提交
267
    "        if batch_id % 20 == 0:\n",
C
Chen Long 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    "            print(\"batch_id: {}, loss is: {}, acc is: {}\".format(batch_id, avg_loss.numpy(), avg_acc.numpy()))\n",
    "test(model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 训练方式一结束\n",
    "以上就是训练方式一,通过这种方式,可以清楚的看到训练和测试中的每一步过程。但是,这种方式句法比较复杂。因此,我们提供了训练方式二,能够更加快速、高效的完成模型的训练与测试。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.训练方式二\n",
    "通过paddle提供的`Model` 构建实例,使用封装好的训练与测试接口,快速完成模型训练与测试。"
   ]
  },
  {
   "cell_type": "code",
C
Chen Long 已提交
290
   "execution_count": 21,
291 292 293 294 295 296 297 298
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle\n",
    "from paddle.static import InputSpec\n",
    "from paddle.metric import Accuracy\n",
    "inputs = InputSpec([None, 784], 'float32', 'x')\n",
    "labels = InputSpec([None, 10], 'float32', 'x')\n",
C
Chen Long 已提交
299
    "model = paddle.Model(LeNet(), inputs, labels)\n",
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    "optim = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())\n",
    "\n",
    "model.prepare(\n",
    "    optim,\n",
    "    paddle.nn.loss.CrossEntropyLoss(),\n",
    "    Accuracy(topk=(1, 2))\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用model.fit来训练模型"
   ]
  },
  {
   "cell_type": "code",
C
Chen Long 已提交
318
   "execution_count": 22,
319 320 321 322 323 324 325
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/2\n",
C
Chen Long 已提交
326 327 328 329 330 331 332 333 334 335
      "step 100/938 - loss: 1.5644 - acc_top1: 0.6281 - acc_top2: 0.7145 - 14ms/step\n",
      "step 200/938 - loss: 1.6221 - acc_top1: 0.7634 - acc_top2: 0.8380 - 13ms/step\n",
      "step 300/938 - loss: 1.5123 - acc_top1: 0.8215 - acc_top2: 0.8835 - 13ms/step\n",
      "step 400/938 - loss: 1.4791 - acc_top1: 0.8530 - acc_top2: 0.9084 - 13ms/step\n",
      "step 500/938 - loss: 1.4904 - acc_top1: 0.8733 - acc_top2: 0.9235 - 13ms/step\n",
      "step 600/938 - loss: 1.5101 - acc_top1: 0.8875 - acc_top2: 0.9341 - 13ms/step\n",
      "step 700/938 - loss: 1.4642 - acc_top1: 0.8983 - acc_top2: 0.9417 - 13ms/step\n",
      "step 800/938 - loss: 1.4789 - acc_top1: 0.9069 - acc_top2: 0.9477 - 13ms/step\n",
      "step 900/938 - loss: 1.4773 - acc_top1: 0.9135 - acc_top2: 0.9523 - 13ms/step\n",
      "step 938/938 - loss: 1.4714 - acc_top1: 0.9157 - acc_top2: 0.9538 - 13ms/step\n",
336 337
      "save checkpoint at /Users/chenlong/online_repo/book/paddle2.0_docs/image_classification/mnist_checkpoint/0\n",
      "Epoch 2/2\n",
C
Chen Long 已提交
338 339 340 341 342 343 344 345 346 347
      "step 100/938 - loss: 1.4863 - acc_top1: 0.9695 - acc_top2: 0.9897 - 13ms/step\n",
      "step 200/938 - loss: 1.4883 - acc_top1: 0.9707 - acc_top2: 0.9912 - 13ms/step\n",
      "step 300/938 - loss: 1.4695 - acc_top1: 0.9720 - acc_top2: 0.9910 - 13ms/step\n",
      "step 400/938 - loss: 1.4628 - acc_top1: 0.9720 - acc_top2: 0.9915 - 13ms/step\n",
      "step 500/938 - loss: 1.5079 - acc_top1: 0.9727 - acc_top2: 0.9918 - 13ms/step\n",
      "step 600/938 - loss: 1.4803 - acc_top1: 0.9727 - acc_top2: 0.9919 - 13ms/step\n",
      "step 700/938 - loss: 1.4612 - acc_top1: 0.9732 - acc_top2: 0.9923 - 13ms/step\n",
      "step 800/938 - loss: 1.4755 - acc_top1: 0.9732 - acc_top2: 0.9923 - 13ms/step\n",
      "step 900/938 - loss: 1.4698 - acc_top1: 0.9732 - acc_top2: 0.9922 - 13ms/step\n",
      "step 938/938 - loss: 1.4764 - acc_top1: 0.9734 - acc_top2: 0.9923 - 13ms/step\n",
C
Chen Long 已提交
348
      "save checkpoint at /Users/chenlong/online_repo/book/paddle2.0_docs/image_classification/mnist_checkpoint/1\n",
349 350 351 352 353 354 355 356
      "save checkpoint at /Users/chenlong/online_repo/book/paddle2.0_docs/image_classification/mnist_checkpoint/final\n"
     ]
    }
   ],
   "source": [
    "model.fit(train_dataset,\n",
    "        epochs=2,\n",
    "        batch_size=64,\n",
C
Chen Long 已提交
357
    "        log_freq=100,\n",
358 359 360 361 362 363 364
    "        save_dir='mnist_checkpoint')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
C
Chen Long 已提交
365
    "### 使用model.evaluate来预测模型"
366 367 368 369
   ]
  },
  {
   "cell_type": "code",
C
Chen Long 已提交
370
   "execution_count": 23,
371 372 373 374 375 376
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
C
Chen Long 已提交
377
      "Eval begin...\n",
C
Chen Long 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
      "step  10/157 - loss: 1.5238 - acc_top1: 0.9750 - acc_top2: 0.9938 - 7ms/step\n",
      "step  20/157 - loss: 1.5143 - acc_top1: 0.9727 - acc_top2: 0.9922 - 7ms/step\n",
      "step  30/157 - loss: 1.5290 - acc_top1: 0.9698 - acc_top2: 0.9932 - 7ms/step\n",
      "step  40/157 - loss: 1.4624 - acc_top1: 0.9684 - acc_top2: 0.9930 - 7ms/step\n",
      "step  50/157 - loss: 1.4771 - acc_top1: 0.9697 - acc_top2: 0.9925 - 7ms/step\n",
      "step  60/157 - loss: 1.5066 - acc_top1: 0.9701 - acc_top2: 0.9922 - 6ms/step\n",
      "step  70/157 - loss: 1.4804 - acc_top1: 0.9699 - acc_top2: 0.9920 - 6ms/step\n",
      "step  80/157 - loss: 1.4718 - acc_top1: 0.9707 - acc_top2: 0.9930 - 6ms/step\n",
      "step  90/157 - loss: 1.4874 - acc_top1: 0.9726 - acc_top2: 0.9934 - 6ms/step\n",
      "step 100/157 - loss: 1.4612 - acc_top1: 0.9736 - acc_top2: 0.9936 - 6ms/step\n",
      "step 110/157 - loss: 1.4612 - acc_top1: 0.9746 - acc_top2: 0.9938 - 6ms/step\n",
      "step 120/157 - loss: 1.4763 - acc_top1: 0.9763 - acc_top2: 0.9941 - 6ms/step\n",
      "step 130/157 - loss: 1.4786 - acc_top1: 0.9764 - acc_top2: 0.9935 - 6ms/step\n",
      "step 140/157 - loss: 1.4612 - acc_top1: 0.9775 - acc_top2: 0.9939 - 6ms/step\n",
      "step 150/157 - loss: 1.4894 - acc_top1: 0.9785 - acc_top2: 0.9943 - 6ms/step\n",
      "step 157/157 - loss: 1.4612 - acc_top1: 0.9777 - acc_top2: 0.9941 - 6ms/step\n",
C
Chen Long 已提交
394
      "Eval samples: 10000\n"
395
     ]
C
Chen Long 已提交
396
    },
397
    {
C
Chen Long 已提交
398 399
     "data": {
      "text/plain": [
C
Chen Long 已提交
400
       "{'loss': [1.4611504], 'acc_top1': 0.9777, 'acc_top2': 0.9941}"
C
Chen Long 已提交
401 402
      ]
     },
C
Chen Long 已提交
403
     "execution_count": 23,
C
Chen Long 已提交
404 405
     "metadata": {},
     "output_type": "execute_result"
406 407 408
    }
   ],
   "source": [
C
Chen Long 已提交
409
    "model.evaluate(test_dataset, batch_size=64)"
410 411 412 413 414 415
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
C
Chen Long 已提交
416 417
    "### 训练方式二结束\n",
    "以上就是训练方式二,可以快速、高效的完成网络模型训练与预测。"
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 总结\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上就是用LeNet对手写数字数据及MNIST进行分类。本示例提供了两种训练模型的方式,一种可以快速完成模型的组建与预测,非常适合新手用户上手。另一种则需要多个步骤来完成模型的训练,适合进阶用户使用。"
   ]
  }
 ],
 "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}