Created
May 10, 2017 12:01
-
-
Save chck/f35186c86df02e56f3689d290f85f1ea 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 [==============================] - 12s - loss: 0.3322 - acc: 0.8997 - val_loss: 0.0818 - val_acc: 0.9753\n", | |
"Epoch 2/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.1196 - acc: 0.9651 - val_loss: 0.0546 - val_acc: 0.9828\n", | |
"Epoch 3/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0867 - acc: 0.9749 - val_loss: 0.0420 - val_acc: 0.9856\n", | |
"Epoch 4/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0732 - acc: 0.9782 - val_loss: 0.0428 - val_acc: 0.9857\n", | |
"Epoch 5/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0637 - acc: 0.9808 - val_loss: 0.0366 - val_acc: 0.9870\n", | |
"Epoch 6/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0561 - acc: 0.9832 - val_loss: 0.0366 - val_acc: 0.9882\n", | |
"Epoch 7/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0525 - acc: 0.9848 - val_loss: 0.0344 - val_acc: 0.9874\n", | |
"Epoch 8/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0473 - acc: 0.9862 - val_loss: 0.0321 - val_acc: 0.9881\n", | |
"Epoch 9/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0444 - acc: 0.9871 - val_loss: 0.0303 - val_acc: 0.9896\n", | |
"Epoch 10/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0426 - acc: 0.9873 - val_loss: 0.0297 - val_acc: 0.9894\n", | |
"Epoch 11/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0383 - acc: 0.9884 - val_loss: 0.0284 - val_acc: 0.9896\n", | |
"Epoch 12/12\n", | |
"60000/60000 [==============================] - 11s - loss: 0.0361 - acc: 0.9889 - val_loss: 0.0272 - val_acc: 0.9912\n", | |
"Test loss: 0.0272085557612\n", | |
"Test accuracy: 0.9912\n", | |
"CPU times: user 1min 40s, sys: 34.3 s, total: 2min 14s\n", | |
"Wall time: 2min 21s\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