Created
May 10, 2017 11:53
-
-
Save chck/0ec1154afe973addfbce4a26a62b3768 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using TensorFlow backend.\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz\n" | |
] | |
} | |
], | |
"source": [ | |
"'''Trains a simple convnet on the MNIST dataset.\n", | |
"\n", | |
"Gets to 99.25% test accuracy after 12 epochs\n", | |
"(there is still a lot of margin for parameter tuning).\n", | |
"16 seconds per epoch on a GRID K520 GPU.\n", | |
"'''\n", | |
"\n", | |
"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\n", | |
"\n", | |
"# the data, shuffled and split between train and test sets\n", | |
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", | |
"\n", | |
"def mnist_cnn(x_train, y_train, x_test, y_test):\n", | |
" 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)\n", | |
"\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'])\n", | |
"\n", | |
" model.fit(x_train, y_train,\n", | |
" batch_size=batch_size,\n", | |
" epochs=epochs,\n", | |
" verbose=1,\n", | |
" validation_data=(x_test, y_test))\n", | |
" score = model.evaluate(x_test, y_test, verbose=0)\n", | |
" print('Test loss:', score[0])\n", | |
" print('Test accuracy:', score[1])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"x_train shape: (60000, 28, 28, 1)\n", | |
"60000 train samples\n", | |
"10000 test samples\n", | |
"Train on 60000 samples, validate on 10000 samples\n", | |
"Epoch 1/12\n", | |
"60000/60000 [==============================] - 315s - loss: 0.3346 - acc: 0.8984 - val_loss: 0.0717 - val_acc: 0.9776\n", | |
"Epoch 2/12\n", | |
"60000/60000 [==============================] - 314s - loss: 0.1089 - acc: 0.9674 - val_loss: 0.0515 - val_acc: 0.9846\n", | |
"Epoch 3/12\n", | |
"60000/60000 [==============================] - 313s - loss: 0.0833 - acc: 0.9750 - val_loss: 0.0421 - val_acc: 0.9860\n", | |
"Epoch 4/12\n", | |
"60000/60000 [==============================] - 312s - loss: 0.0692 - acc: 0.9793 - val_loss: 0.0375 - val_acc: 0.9877\n", | |
"Epoch 5/12\n", | |
"60000/60000 [==============================] - 312s - loss: 0.0597 - acc: 0.9824 - val_loss: 0.0335 - val_acc: 0.9889\n", | |
"Epoch 6/12\n", | |
"60000/60000 [==============================] - 311s - loss: 0.0553 - acc: 0.9837 - val_loss: 0.0380 - val_acc: 0.9869\n", | |
"Epoch 7/12\n", | |
"60000/60000 [==============================] - 310s - loss: 0.0506 - acc: 0.9854 - val_loss: 0.0314 - val_acc: 0.9892\n", | |
"Epoch 8/12\n", | |
"60000/60000 [==============================] - 311s - loss: 0.0463 - acc: 0.9857 - val_loss: 0.0299 - val_acc: 0.9896\n", | |
"Epoch 9/12\n", | |
"60000/60000 [==============================] - 312s - loss: 0.0422 - acc: 0.9873 - val_loss: 0.0298 - val_acc: 0.9897\n", | |
"Epoch 10/12\n", | |
"60000/60000 [==============================] - 312s - loss: 0.0410 - acc: 0.9880 - val_loss: 0.0287 - val_acc: 0.9900\n", | |
"Epoch 11/12\n", | |
"60000/60000 [==============================] - 313s - loss: 0.0385 - acc: 0.9885 - val_loss: 0.0271 - val_acc: 0.9919\n", | |
"Epoch 12/12\n", | |
"60000/60000 [==============================] - 312s - loss: 0.0368 - acc: 0.9892 - val_loss: 0.0268 - val_acc: 0.9913\n", | |
"Test loss: 0.0267528861207\n", | |
"Test accuracy: 0.9913\n", | |
"CPU times: user 1h 57min 37s, sys: 6min 22s, total: 2h 3min 59s\n", | |
"Wall time: 1h 2min 48s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"mnist_cnn(x_train, y_train, x_test, y_test)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment