{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "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"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "mnist = tf.keras.datasets.mnist\n",
    "(x_train, y_train),(x_test, y_test) = mnist.load_data(path=\"/data/data/mnist.npz\")   #加载mnist数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(60000, 28, 28)\n",
      "(60000,)\n",
      "(10000, 28, 28)\n",
      "(10000,)\n"
     ]
    }
   ],
   "source": [
    "#验证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)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12640 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#打印训练集中前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()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 0 4 1 9 2 1 3 1]\n"
     ]
    }
   ],
   "source": [
    "#打印相应的标签\n",
    "print(y_train[:9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#基操：将像素标准化一下\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",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "60000/60000 [==============================] - 4s 65us/sample - loss: 0.2971 - accuracy: 0.9140\n",
      "Epoch 2/10\n",
      "60000/60000 [==============================] - 4s 60us/sample - loss: 0.1436 - accuracy: 0.9574\n",
      "Epoch 3/10\n",
      "60000/60000 [==============================] - 4s 60us/sample - loss: 0.1094 - accuracy: 0.9670\n",
      "Epoch 4/10\n",
      "60000/60000 [==============================] - 4s 60us/sample - loss: 0.0869 - accuracy: 0.9731\n",
      "Epoch 5/10\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.0740 - accuracy: 0.9771\n",
      "Epoch 6/10\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.0640 - accuracy: 0.9800\n",
      "Epoch 7/10\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.0588 - accuracy: 0.9807\n",
      "Epoch 8/10\n",
      "60000/60000 [==============================] - 4s 60us/sample - loss: 0.0530 - accuracy: 0.9826\n",
      "Epoch 9/10\n",
      "60000/60000 [==============================] - 4s 60us/sample - loss: 0.0469 - accuracy: 0.9845\n",
      "Epoch 10/10\n",
      "60000/60000 [==============================] - 4s 61us/sample - loss: 0.0412 - accuracy: 0.9865\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x7fada36b6400>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#设置训练超参，优化器为Adam，损失函数为交叉熵，训练衡量指标为accuracy\n",
    "model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n",
    "\n",
    "#开始训练，训练10个epoch，一个epoch代表所有图像计算一遍。每一个epoch能观察到训练精度的提升\n",
    "model.fit(x_train, y_train, epochs=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/10000 [==============================] - 0s 39us/sample - loss: 0.0695 - accuracy: 0.9808\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.06950160280249547, 0.9808]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算训练了10个epoch的模型在测试集上的表现\n",
    "model.evaluate(x_test,  y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12696 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#直观看一下模型预测结果，打印测试集中的前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()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 2, 1, 0, 4, 1, 4, 9, 5])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#打印模型识别的数字，是否正确？\n",
    "np.argmax(model(x_test[:9]).numpy(), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#保存训练好的模型\n",
    "model.save(\"/data/output/model_epoch_5\")"
   ]
  },
  {
   "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
}
