{ "nbformat": 4, "nbformat_minor": 0, "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.6.5" }, "colab": { "name": "06-keras-intro.ipynb", "version": "0.3.2", "provenance": [] } }, "cells": [ { "cell_type": "code", "metadata": { "id": "JqTr2EniyDlT", "colab_type": "code", "colab": {} }, "source": [ "# windows only hack for graphviz path \n", "import os\n", "for path in os.environ['PATH'].split(os.pathsep):\n", " if path.endswith(\"Library\\\\bin\"):\n", " os.environ['PATH']+=os.pathsep+os.path.join(path, 'graphviz')" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "fwixO1QKyDlb", "colab_type": "code", "colab": {}, "outputId": "2ad761a2-ab03-41ab-a6e5-18c796a51b05" }, "source": [ "import keras\n", "from keras.models import Sequential\n", "from PIL import Image\n", "import numpy as np" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ], "name": "stderr" } ] }, { "cell_type": "code", "metadata": { "id": "1cVK1EzUyDli", "colab_type": "code", "colab": {} }, "source": [ "import lzma\n", "import pickle\n", "with lzma.open(\"mnist.pkl.xz\", 'rb') as f:\n", " train_set, validation_set, test_set = pickle.load(f, encoding='latin1')\n", "train_X, train_y = train_set\n", "validation_X, validation_y = validation_set\n", "test_X, test_y = test_set\n", "\n", "\n", "train_Y = np.eye(10)[train_y]\n", "test_Y = np.eye(10)[test_y]\n", "validation_Y = np.eye(10)[validation_y]\n", "\n", "# or\n", "# from keras.datasets import mnist\n", "# from keras.utils import np_utils\n", "# (train_X, train_y), (test_X, test_y) = mnist.load_data()\n", "# train_Y = np_utils.to_categorical(train_y, 10)\n", "# test_Y = np_utils.to_categorical(test_y, 10)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "YAmfG2kKyDlo", "colab_type": "text" }, "source": [ "### logistic regression" ] }, { "cell_type": "code", "metadata": { "id": "RgqfH4FGyDlq", "colab_type": "code", "colab": {} }, "source": [ "from keras.layers import Dense, Activation\n", "model = Sequential()\n", "model.add(Dense(units=10, input_dim=784))\n", "model.add(Activation('softmax'))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "zhpccySVyDlu", "colab_type": "code", "colab": {} }, "source": [ "model.compile(loss='categorical_crossentropy',\n", " optimizer='sgd',\n", " metrics=['accuracy'])" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "nD3poRD4yDlz", "colab_type": "code", "colab": {}, "outputId": "4ad2f71b-b7e8-44ab-c8b3-814c152b82a9" }, "source": [ "from IPython.display import SVG, display\n", "from keras.utils.vis_utils import model_to_dot\n", "\n", "SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "image/svg+xml": "\n\nG\n\n\n1902677000824\n\ndense_1_input: InputLayer\n\ninput:\n\noutput:\n\n(None, 784)\n\n(None, 784)\n\n\n1902676938424\n\ndense_1: Dense\n\ninput:\n\noutput:\n\n(None, 784)\n\n(None, 10)\n\n\n1902677000824->1902676938424\n\n\n\n\n1902676938704\n\nactivation_1: Activation\n\ninput:\n\noutput:\n\n(None, 10)\n\n(None, 10)\n\n\n1902676938424->1902676938704\n\n\n\n\n", "text/plain": [ "" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "code", "metadata": { "id": "vn626EQmyDl4", "colab_type": "code", "colab": {}, "outputId": "2432a829-7b0f-4caf-d42d-d3b6346b65bd" }, "source": [ "model.fit(train_X, train_Y, validation_data=(validation_X, validation_Y), batch_size=128, epochs=15)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Train on 50000 samples, validate on 10000 samples\n", "Epoch 1/15\n", "50000/50000 [==============================] - 0s - loss: 1.3414 - acc: 0.6857 - val_loss: 0.8683 - val_acc: 0.8318\n", "Epoch 2/15\n", "50000/50000 [==============================] - 0s - loss: 0.7797 - acc: 0.8291 - val_loss: 0.6367 - val_acc: 0.8615\n", "Epoch 3/15\n", "50000/50000 [==============================] - 0s - loss: 0.6348 - acc: 0.8509 - val_loss: 0.5439 - val_acc: 0.8743\n", "Epoch 4/15\n", "50000/50000 [==============================] - 0s - loss: 0.5644 - acc: 0.8614 - val_loss: 0.4922 - val_acc: 0.8825\n", "Epoch 5/15\n", "50000/50000 [==============================] - 0s - loss: 0.5214 - acc: 0.8681 - val_loss: 0.4590 - val_acc: 0.8887\n", "Epoch 6/15\n", "50000/50000 [==============================] - 0s - loss: 0.4919 - acc: 0.8736 - val_loss: 0.4358 - val_acc: 0.8909\n", "Epoch 7/15\n", "50000/50000 [==============================] - 0s - loss: 0.4700 - acc: 0.8773 - val_loss: 0.4182 - val_acc: 0.8938\n", "Epoch 8/15\n", "50000/50000 [==============================] - 0s - loss: 0.4530 - acc: 0.8801 - val_loss: 0.4044 - val_acc: 0.8959\n", "Epoch 9/15\n", "50000/50000 [==============================] - 0s - loss: 0.4393 - acc: 0.8828 - val_loss: 0.3932 - val_acc: 0.8982\n", "Epoch 10/15\n", "50000/50000 [==============================] - 0s - loss: 0.4280 - acc: 0.8853 - val_loss: 0.3840 - val_acc: 0.8997\n", "Epoch 11/15\n", "50000/50000 [==============================] - 0s - loss: 0.4183 - acc: 0.8867 - val_loss: 0.3762 - val_acc: 0.9005\n", "Epoch 12/15\n", "50000/50000 [==============================] - 0s - loss: 0.4101 - acc: 0.8890 - val_loss: 0.3693 - val_acc: 0.9017\n", "Epoch 13/15\n", "50000/50000 [==============================] - 0s - loss: 0.4029 - acc: 0.8902 - val_loss: 0.3637 - val_acc: 0.9029\n", "Epoch 14/15\n", "50000/50000 [==============================] - 0s - loss: 0.3965 - acc: 0.8918 - val_loss: 0.3585 - val_acc: 0.9036\n", "Epoch 15/15\n", "50000/50000 [==============================] - 0s - loss: 0.3908 - acc: 0.8931 - val_loss: 0.3540 - val_acc: 0.9038\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": { "tags": [] }, "execution_count": 7 } ] }, { "cell_type": "code", "metadata": { "id": "CPpkuL-MyDl9", "colab_type": "code", "colab": {}, "outputId": "d725a9a4-5d15-4e7f-a19a-86a383bd172a" }, "source": [ "# 預測看看 test_X 前 20 筆\n", "model.predict_classes(test_X[:20])" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "20/20 [==============================] - 0s\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "array([7, 2, 1, 0, 4, 1, 4, 9, 6, 9, 0, 6, 9, 0, 1, 5, 9, 7, 3, 4], dtype=int64)" ] }, "metadata": { "tags": [] }, "execution_count": 8 } ] }, { "cell_type": "code", "metadata": { "id": "37826yStyDmA", "colab_type": "code", "colab": {}, "outputId": "f193b695-97c2-4896-94c5-49b772f3d7fc" }, "source": [ "# 對答案\n", "test_y[:20]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([7, 2, 1, 0, 4, 1, 4, 9, 5, 9, 0, 6, 9, 0, 1, 5, 9, 7, 3, 4], dtype=int64)" ] }, "metadata": { "tags": [] }, "execution_count": 9 } ] }, { "cell_type": "code", "metadata": { "id": "2yIS2tAbyDmE", "colab_type": "code", "colab": {}, "outputId": "659b0f92-1d4f-4930-aeda-e861d12c952e" }, "source": [ "# 看看 test accuracy\n", "model.evaluate(test_X, test_Y)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ " 8032/10000 [=======================>......] - ETA: 0s" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "[0.36359533268213273, 0.90269999999999995]" ] }, "metadata": { "tags": [] }, "execution_count": 10 } ] }, { "cell_type": "markdown", "metadata": { "id": "p9IG_ZfzyDmJ", "colab_type": "text" }, "source": [ "## Q \n", "* 將 `optimizer` 換成 `\"adam\"`\n", "* 將 `optimizer` 換成 `keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True)`" ] }, { "cell_type": "markdown", "metadata": { "id": "SqgefFRpyDmK", "colab_type": "text" }, "source": [ "### 建立 convolutional model\n", "我們之前的網路架構\n", "* convolution 2d kernel=(3,3), filters=32\n", "* relu\n", "* max pool\n", "* convolution 2d kernel=(3,3), filters=64\n", "* relu\n", "* max pool\n", "* dense units=1024\n", "* relu\n", "* dropout (rate=0.8) # 先省略這一層\n", "* dense units = 10\n", "* softmax\n", "\n", "試著架出這樣的網路\n", "\n", "然後訓練看看\n", "\n", "開頭幾行可以這樣寫\n", "```python\n", "from keras.layers import Dense, Activation, Conv2D, MaxPool2D, Reshape\n", "model = Sequential()\n", "model.add(Reshape((28, 28, 1), input_shape=(784,) ))\n", "model.add(Conv2D(filters=32, kernel_size=(3,3), padding='same', activation=\"relu\"))\n", "```" ] }, { "cell_type": "code", "metadata": { "id": "wsRrdIP1yDmM", "colab_type": "code", "colab": {} }, "source": [ "# 參考答案\n", "#%load q_keras_cnn.py" ], "execution_count": 0, "outputs": [] } ] }