{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "mnist = tf.keras.datasets.mnist\n",
    "(x_train, y_train),(x_test, y_test) = mnist.load_data(path=\"/data/data/mnist.npz\")   #加载mnist数据集\n",
    "\n",
    "#验证mnist数据集大小。x为数据，y为标签。mnist每张图的像素为28*28\n",
    "print(x_train.shape)\n",
    "print(y_train.shape)\n",
    "print(x_test.shape)\n",
    "print(y_test.shape)\n",
    "\n",
    "#打印训练集中前9张，看看是什么数字\n",
    "for i in range(9):  \n",
    "    plt.subplot(3,3,1+i)\n",
    "    plt.imshow(x_train[i], cmap='gray')\n",
    "plt.show()\n",
    "\n",
    "#打印相应的标签\n",
    "print(y_train[:9])\n",
    "\n",
    "#基操：将像素标准化一下\n",
    "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
    "\n",
    "#搭建一个两层神经网络\n",
    "model = tf.keras.models.Sequential([\n",
    "  tf.keras.layers.Flatten(input_shape=(28, 28)),    #拉伸图像成一维向量\n",
    "  tf.keras.layers.Dense(128, activation='relu'),    #第一层全连接+ReLU激活\n",
    "  tf.keras.layers.Dropout(0.2),                     #dropout层\n",
    "  tf.keras.layers.Dense(10, activation='softmax')   #第二层全连接+softmax激活，输出预测标签\n",
    "])\n",
    "\n",
    "#设置训练超参，优化器为sgd，损失函数为交叉熵，训练衡量指标为accuracy\n",
    "model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n",
    "\n",
    "#开始训练，训练5个epoch，一个epoch代表所有图像计算一遍。每一个epoch能观察到训练精度的提升\n",
    "model.fit(x_train, y_train, epochs=6)\n",
    "\n",
    "#计算训练了5个epoch的模型在测试集上的表现\n",
    "model.evaluate(x_test,  y_test)\n",
    "\n",
    "\n",
    "#直观看一下模型预测结果，打印测试集中的前9张图像\n",
    "for i in range(9):  \n",
    "    plt.subplot(3,3,1+i)\n",
    "    plt.imshow(x_test[i], cmap='gray')\n",
    "plt.show()\n",
    "\n",
    "#打印模型识别的数字，是否正确？\n",
    "np.argmax(model(x_test[:9]).numpy(), axis=1)\n",
    "\n",
    "#保存训练好的模型\n",
    "model.save(\"/data/output/model_epoch_6\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.evaluate(x_test,  y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/10000 [==============================] - 0s 30us/sample - loss: 0.4680 - accuracy: 0.8846\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.46795412821769716, 0.8846]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(x_test,  y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'model' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-e80b0bf1617b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mevaluate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_test\u001b[0m\u001b[0;34m,\u001b[0m  \u001b[0my_test\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'model' is not defined"
     ]
    }
   ],
   "source": [
    "model.evaluate(x_test,  y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(60000, 28, 28)\n",
      "(60000,)\n",
      "(10000, 28, 28)\n",
      "(10000,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 0 4 1 9 2 1 3 1]\n",
      "Epoch 1/6\n",
      "60000/60000 [==============================] - 3s 58us/sample - loss: 1.6360 - accuracy: 0.5506\n",
      "Epoch 2/6\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.9633 - accuracy: 0.7638\n",
      "Epoch 3/6\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.7347 - accuracy: 0.8098\n",
      "Epoch 4/6\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.6252 - accuracy: 0.8334\n",
      "Epoch 5/6\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.5630 - accuracy: 0.8467\n",
      "Epoch 6/6\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.5185 - accuracy: 0.8568\n",
      "10000/10000 [==============================] - 0s 37us/sample - loss: 0.4304 - accuracy: 0.8923\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import os\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "mnist = tf.keras.datasets.mnist\n",
    "(x_train, y_train),(x_test, y_test) = mnist.load_data(path=\"/data/data/mnist.npz\")   #加载mnist数据集\n",
    "\n",
    "#验证mnist数据集大小。x为数据，y为标签。mnist每张图的像素为28*28\n",
    "print(x_train.shape)\n",
    "print(y_train.shape)\n",
    "print(x_test.shape)\n",
    "print(y_test.shape)\n",
    "\n",
    "#打印训练集中前9张，看看是什么数字\n",
    "for i in range(9):  \n",
    "    plt.subplot(3,3,1+i)\n",
    "    plt.imshow(x_train[i], cmap='gray')\n",
    "plt.show()\n",
    "\n",
    "#打印相应的标签\n",
    "print(y_train[:9])\n",
    "\n",
    "#基操：将像素标准化一下\n",
    "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
    "\n",
    "#搭建一个两层神经网络\n",
    "model = tf.keras.models.Sequential([\n",
    "  tf.keras.layers.Flatten(input_shape=(28, 28)),    #拉伸图像成一维向量\n",
    "  tf.keras.layers.Dense(128, activation='relu'),    #第一层全连接+ReLU激活\n",
    "  tf.keras.layers.Dropout(0.2),                     #dropout层\n",
    "  tf.keras.layers.Dense(10, activation='softmax')   #第二层全连接+softmax激活，输出预测标签\n",
    "])\n",
    "\n",
    "#设置训练超参，优化器为sgd，损失函数为交叉熵，训练衡量指标为accuracy\n",
    "model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n",
    "\n",
    "#开始训练，训练5个epoch，一个epoch代表所有图像计算一遍。每一个epoch能观察到训练精度的提升\n",
    "model.fit(x_train, y_train, epochs=6)\n",
    "\n",
    "#计算训练了5个epoch的模型在测试集上的表现\n",
    "model.evaluate(x_test,  y_test)\n",
    "\n",
    "\n",
    "#直观看一下模型预测结果，打印测试集中的前9张图像\n",
    "for i in range(9):  \n",
    "    plt.subplot(3,3,1+i)\n",
    "    plt.imshow(x_test[i], cmap='gray')\n",
    "plt.show()\n",
    "\n",
    "#打印模型识别的数字，是否正确？\n",
    "np.argmax(model(x_test[:9]).numpy(), axis=1)\n",
    "\n",
    "#保存训练好的模型\n",
    "model.save(\"/data/output/model_epoch_6\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/10000 [==============================] - 0s 30us/sample - loss: 0.4304 - accuracy: 0.8923\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.43036172440052034, 0.8923]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(x_test,  y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(60000, 28, 28)\n",
      "(60000,)\n",
      "(10000, 28, 28)\n",
      "(10000,)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12640 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 0 4 1 9 2 1 3 1]\n",
      "Epoch 1/9\n",
      "60000/60000 [==============================] - 3s 57us/sample - loss: 1.7841 - accuracy: 0.4954\n",
      "Epoch 2/9\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 1.0707 - accuracy: 0.7433\n",
      "Epoch 3/9\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.7987 - accuracy: 0.7936\n",
      "Epoch 4/9\n",
      "60000/60000 [==============================] - 3s 56us/sample - loss: 0.6696 - accuracy: 0.8208\n",
      "Epoch 5/9\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.5945 - accuracy: 0.8386\n",
      "Epoch 6/9\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.5450 - accuracy: 0.8495\n",
      "Epoch 7/9\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.5106 - accuracy: 0.8577\n",
      "Epoch 8/9\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.4841 - accuracy: 0.8640\n",
      "Epoch 9/9\n",
      "60000/60000 [==============================] - 3s 55us/sample - loss: 0.4627 - accuracy: 0.8687\n",
      "10000/10000 [==============================] - 0s 38us/sample - loss: 0.3818 - accuracy: 0.9001\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12696 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import os\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "mnist = tf.keras.datasets.mnist\n",
    "(x_train, y_train),(x_test, y_test) = mnist.load_data(path=\"/data/data/mnist.npz\")   #加载mnist数据集\n",
    "\n",
    "#验证mnist数据集大小。x为数据，y为标签。mnist每张图的像素为28*28\n",
    "print(x_train.shape)\n",
    "print(y_train.shape)\n",
    "print(x_test.shape)\n",
    "print(y_test.shape)\n",
    "\n",
    "#打印训练集中前9张，看看是什么数字\n",
    "for i in range(9):  \n",
    "    plt.subplot(3,3,1+i)\n",
    "    plt.imshow(x_train[i], cmap='gray')\n",
    "plt.show()\n",
    "\n",
    "#打印相应的标签\n",
    "print(y_train[:9])\n",
    "\n",
    "#基操：将像素标准化一下\n",
    "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
    "\n",
    "#搭建一个两层神经网络\n",
    "model = tf.keras.models.Sequential([\n",
    "  tf.keras.layers.Flatten(input_shape=(28, 28)),    #拉伸图像成一维向量\n",
    "  tf.keras.layers.Dense(128, activation='relu'),    #第一层全连接+ReLU激活\n",
    "  tf.keras.layers.Dropout(0.2),                     #dropout层\n",
    "  tf.keras.layers.Dense(10, activation='softmax')   #第二层全连接+softmax激活，输出预测标签\n",
    "])\n",
    "\n",
    "#设置训练超参，优化器为sgd，损失函数为交叉熵，训练衡量指标为accuracy\n",
    "model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n",
    "\n",
    "#开始训练，训练5个epoch，一个epoch代表所有图像计算一遍。每一个epoch能观察到训练精度的提升\n",
    "model.fit(x_train, y_train, epochs=9)\n",
    "\n",
    "#计算训练了5个epoch的模型在测试集上的表现\n",
    "model.evaluate(x_test,  y_test)\n",
    "\n",
    "\n",
    "#直观看一下模型预测结果，打印测试集中的前9张图像\n",
    "for i in range(9):  \n",
    "    plt.subplot(3,3,1+i)\n",
    "    plt.imshow(x_test[i], cmap='gray')\n",
    "plt.show()\n",
    "\n",
    "#打印模型识别的数字，是否正确？\n",
    "np.argmax(model(x_test[:9]).numpy(), axis=1)\n",
    "\n",
    "#保存训练好的模型\n",
    "model.save(\"/data/output/model_epoch_9\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/10000 [==============================] - 0s 30us/sample - loss: 0.3818 - accuracy: 0.9001\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.381765931224823, 0.9001]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(x_test,  y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(60000, 28, 28)\n",
      "(60000,)\n",
      "(10000, 28, 28)\n",
      "(10000,)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12640 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 0 4 1 9 2 1 3 1]\n",
      "Epoch 1/15\n",
      "60000/60000 [==============================] - 4s 64us/sample - loss: 0.2953 - accuracy: 0.9138\n",
      "Epoch 2/15\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.1440 - accuracy: 0.9567\n",
      "Epoch 3/15\n",
      "60000/60000 [==============================] - 4s 63us/sample - loss: 0.1086 - accuracy: 0.9677\n",
      "Epoch 4/15\n",
      "60000/60000 [==============================] - 4s 62us/sample - loss: 0.0888 - accuracy: 0.9721\n",
      "Epoch 5/15\n",
      "60000/60000 [==============================] - 4s 62us/sample - loss: 0.0763 - accuracy: 0.9758\n",
      "Epoch 6/15\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.0658 - accuracy: 0.9789\n",
      "Epoch 7/15\n",
      "60000/60000 [==============================] - 4s 60us/sample - loss: 0.0572 - accuracy: 0.9813\n",
      "Epoch 8/15\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.0519 - accuracy: 0.9832\n",
      "Epoch 9/15\n",
      "60000/60000 [==============================] - 4s 62us/sample - loss: 0.0503 - accuracy: 0.9830\n",
      "Epoch 10/15\n",
      "60000/60000 [==============================] - 4s 62us/sample - loss: 0.0430 - accuracy: 0.9857\n",
      "Epoch 11/15\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.0414 - accuracy: 0.9861\n",
      "Epoch 12/15\n",
      "60000/60000 [==============================] - 4s 62us/sample - loss: 0.0383 - accuracy: 0.9877\n",
      "Epoch 13/15\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.0362 - accuracy: 0.9876\n",
      "Epoch 14/15\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.0342 - accuracy: 0.9881\n",
      "Epoch 15/15\n",
      "60000/60000 [==============================] - ETA: 0s - loss: 0.0336 - accuracy: 0.98 - 4s 61us/sample - loss: 0.0336 - accuracy: 0.9884\n",
      "10000/10000 [==============================] - 0s 37us/sample - loss: 0.0869 - accuracy: 0.9791\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12696 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import os\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "mnist = tf.keras.datasets.mnist\n",
    "(x_train, y_train),(x_test, y_test) = mnist.load_data(path=\"/data/data/mnist.npz\")   #加载mnist数据集\n",
    "\n",
    "#验证mnist数据集大小。x为数据，y为标签。mnist每张图的像素为28*28\n",
    "print(x_train.shape)\n",
    "print(y_train.shape)\n",
    "print(x_test.shape)\n",
    "print(y_test.shape)\n",
    "\n",
    "#打印训练集中前9张，看看是什么数字\n",
    "for i in range(9):  \n",
    "    plt.subplot(3,3,1+i)\n",
    "    plt.imshow(x_train[i], cmap='gray')\n",
    "plt.show()\n",
    "\n",
    "#打印相应的标签\n",
    "print(y_train[:9])\n",
    "\n",
    "#基操：将像素标准化一下\n",
    "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
    "\n",
    "#搭建一个两层神经网络\n",
    "model = tf.keras.models.Sequential([\n",
    "  tf.keras.layers.Flatten(input_shape=(28, 28)),    #拉伸图像成一维向量\n",
    "  tf.keras.layers.Dense(128, activation='relu'),    #第一层全连接+ReLU激活\n",
    "  tf.keras.layers.Dropout(0.2),                     #dropout层\n",
    "  tf.keras.layers.Dense(10, activation='softmax')   #第二层全连接+softmax激活，输出预测标签\n",
    "])\n",
    "\n",
    "#设置训练超参，优化器为sgd，损失函数为交叉熵，训练衡量指标为accuracy\n",
    "model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n",
    "\n",
    "#开始训练，训练5个epoch，一个epoch代表所有图像计算一遍。每一个epoch能观察到训练精度的提升\n",
    "model.fit(x_train, y_train, epochs=15)\n",
    "\n",
    "#计算训练了5个epoch的模型在测试集上的表现\n",
    "model.evaluate(x_test,  y_test)\n",
    "\n",
    "\n",
    "#直观看一下模型预测结果，打印测试集中的前9张图像\n",
    "for i in range(9):  \n",
    "    plt.subplot(3,3,1+i)\n",
    "    plt.imshow(x_test[i], cmap='gray')\n",
    "plt.show()\n",
    "\n",
    "#打印模型识别的数字，是否正确？\n",
    "np.argmax(model(x_test[:9]).numpy(), axis=1)\n",
    "\n",
    "#保存训练好的模型\n",
    "model.save(\"/data/output/model_epoch_15\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/10000 [==============================] - 0s 30us/sample - loss: 0.0869 - accuracy: 0.9791\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.08690820527771356, 0.9791]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(x_test,  y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tensorflow-2.0",
   "language": "python",
   "name": "tensorflow-2.0"
  },
  "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
