Created
October 16, 2018 13:32
-
-
Save yiidtw/76e0c7e234e4f0617fe956ee83a15ca2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "keras_cnn_mnist.ipynb", | |
"version": "0.3.2", | |
"provenance": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"metadata": { | |
"id": "-eIxxGyvQAbP", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Import Library and Use Tensorflow as Keras Backend" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "bsGpTlf1NMOB", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "e00bc87d-dcfa-490f-a405-b6ad48e14b79" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from __future__ import print_function\n", | |
"import keras\n", | |
"from keras.datasets import mnist\n", | |
"from keras.models import Sequential\n", | |
"from keras.layers import Dense, Dropout, Flatten\n", | |
"from keras.layers import Conv2D, MaxPooling2D\n", | |
"from keras import backend as K" | |
], | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Using TensorFlow backend.\n" | |
], | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "hfvp3JVuP8dR", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Load Data" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "gv9kobR6PO2N", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 51 | |
}, | |
"outputId": "26156e1b-40c5-43c4-87c7-f7f3ad93e515" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# the data, split between train and test sets\n", | |
"(x_train, y_train), (x_test, y_test) = mnist.load_data()" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz\n", | |
"11493376/11490434 [==============================] - 1s 0us/step\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "GEJetJ29QH5W", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Setup Hyper-parameter" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "OYc55xjGP2rA", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 68 | |
}, | |
"outputId": "0ea7ecf0-ca2d-4c82-c1de-785b5cf7b8e7" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"batch_size = 128\n", | |
"num_classes = 10\n", | |
"epochs = 12\n", | |
"\n", | |
"# input image dimensions\n", | |
"img_rows, img_cols = 28, 28\n", | |
"\n", | |
"if K.image_data_format() == 'channels_first':\n", | |
" x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)\n", | |
" x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)\n", | |
" input_shape = (1, img_rows, img_cols)\n", | |
"else:\n", | |
" x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\n", | |
" x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\n", | |
" input_shape = (img_rows, img_cols, 1)\n", | |
"\n", | |
"x_train = x_train.astype('float32')\n", | |
"x_test = x_test.astype('float32')\n", | |
"x_train /= 255\n", | |
"x_test /= 255\n", | |
"print('x_train shape:', x_train.shape)\n", | |
"print(x_train.shape[0], 'train samples')\n", | |
"print(x_test.shape[0], 'test samples')\n", | |
"\n", | |
"# convert class vectors to binary class matrices\n", | |
"y_train = keras.utils.to_categorical(y_train, num_classes)\n", | |
"y_test = keras.utils.to_categorical(y_test, num_classes)" | |
], | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"x_train shape: (60000, 28, 28, 1)\n", | |
"60000 train samples\n", | |
"10000 test samples\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "tS7PZjX5QC5X", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Build Model" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "Scdyj8g0Nct1", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"\n", | |
"model = Sequential()\n", | |
"model.add(Conv2D(32, kernel_size=(3, 3),\n", | |
" activation='relu',\n", | |
" input_shape=input_shape))\n", | |
"model.add(Conv2D(64, (3, 3), activation='relu'))\n", | |
"model.add(MaxPooling2D(pool_size=(2, 2)))\n", | |
"model.add(Dropout(0.25))\n", | |
"model.add(Flatten())\n", | |
"model.add(Dense(128, activation='relu'))\n", | |
"model.add(Dropout(0.5))\n", | |
"model.add(Dense(num_classes, activation='softmax'))\n", | |
"\n", | |
"model.compile(loss=keras.losses.categorical_crossentropy,\n", | |
" optimizer=keras.optimizers.Adadelta(),\n", | |
" metrics=['accuracy'])" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "L5oZWDnqQz9V", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Train Model" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "GWyjsuFoQ2Zh", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 459 | |
}, | |
"outputId": "2bdc2b0e-1a45-4569-cf4e-f69561728235" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"model.fit(x_train, y_train,\n", | |
" batch_size=batch_size,\n", | |
" epochs=epochs,\n", | |
" verbose=1,\n", | |
" validation_data=(x_test, y_test))" | |
], | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Train on 60000 samples, validate on 10000 samples\n", | |
"Epoch 1/12\n", | |
"60000/60000 [==============================] - 11s 188us/step - loss: 0.2642 - acc: 0.9186 - val_loss: 0.0615 - val_acc: 0.9793\n", | |
"Epoch 2/12\n", | |
"60000/60000 [==============================] - 10s 160us/step - loss: 0.0866 - acc: 0.9737 - val_loss: 0.0417 - val_acc: 0.9859\n", | |
"Epoch 3/12\n", | |
"60000/60000 [==============================] - 9s 155us/step - loss: 0.0654 - acc: 0.9808 - val_loss: 0.0376 - val_acc: 0.9864\n", | |
"Epoch 4/12\n", | |
"60000/60000 [==============================] - 9s 156us/step - loss: 0.0543 - acc: 0.9840 - val_loss: 0.0456 - val_acc: 0.9852\n", | |
"Epoch 5/12\n", | |
"60000/60000 [==============================] - 9s 155us/step - loss: 0.0472 - acc: 0.9853 - val_loss: 0.0337 - val_acc: 0.9892\n", | |
"Epoch 6/12\n", | |
"60000/60000 [==============================] - 9s 155us/step - loss: 0.0401 - acc: 0.9875 - val_loss: 0.0293 - val_acc: 0.9906\n", | |
"Epoch 7/12\n", | |
"60000/60000 [==============================] - 9s 154us/step - loss: 0.0370 - acc: 0.9890 - val_loss: 0.0256 - val_acc: 0.9915\n", | |
"Epoch 8/12\n", | |
"60000/60000 [==============================] - 9s 155us/step - loss: 0.0351 - acc: 0.9895 - val_loss: 0.0275 - val_acc: 0.9908\n", | |
"Epoch 9/12\n", | |
"60000/60000 [==============================] - 9s 155us/step - loss: 0.0302 - acc: 0.9911 - val_loss: 0.0248 - val_acc: 0.9922\n", | |
"Epoch 10/12\n", | |
"60000/60000 [==============================] - 9s 157us/step - loss: 0.0291 - acc: 0.9914 - val_loss: 0.0287 - val_acc: 0.9908\n", | |
"Epoch 11/12\n", | |
"60000/60000 [==============================] - 9s 155us/step - loss: 0.0269 - acc: 0.9918 - val_loss: 0.0274 - val_acc: 0.9915\n", | |
"Epoch 12/12\n", | |
"60000/60000 [==============================] - 9s 155us/step - loss: 0.0259 - acc: 0.9920 - val_loss: 0.0283 - val_acc: 0.9909\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7f670edfd048>" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 6 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "tvIByE2hQpUk", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Validate Model" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "PfcHeD7LQfvT", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 51 | |
}, | |
"outputId": "53d5b3a9-3ab4-4258-d14c-a624835c27e2" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"score = model.evaluate(x_test, y_test, verbose=0)\n", | |
"print('Test loss:', score[0])\n", | |
"print('Test accuracy:', score[1])\n" | |
], | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Test loss: 0.028276310487868248\n", | |
"Test accuracy: 0.9909\n" | |
], | |
"name": "stdout" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment