Last active
December 24, 2018 01:07
-
-
Save lenhhoxung86/2b977b1c9c9c0593acaa1dd813fc02f4 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": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"'''Fully connected neural networks with two Dense layers.\n", | |
"'''\n", | |
"# step 1: Importing packages\n", | |
"import keras\n", | |
"from keras.datasets import mnist\n", | |
"from keras.models import Sequential\n", | |
"from keras.layers import Dense, Dropout\n", | |
"from keras.optimizers import Adam\n", | |
"import matplotlib.pyplot as plt\n", | |
"import matplotlib.gridspec as gridspec\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# step 2: Loading data and visualizing some examples\n", | |
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", | |
"\n", | |
"# training parameters\n", | |
"batch_size = 128\n", | |
"num_classes = 10\n", | |
"epochs = 5\n", | |
"\n", | |
"# visualization of some examples\n", | |
"def show_images(images, rows = 6):\n", | |
" \"\"\"Display a list of images in a single figure with matplotlib.\n", | |
" \n", | |
" Args:\n", | |
" images: List of np.arrays compatible with plt.imshow.\n", | |
" rows (Default = 1): Number of columns in figure (number of rows is \n", | |
" set to np.ceil(n_images/float(rows))).\n", | |
" \"\"\"\n", | |
" n_images = len(images)\n", | |
" cols = int(n_images/rows)\n", | |
" plt.figure(figsize = (rows, cols))\n", | |
" gs1 = gridspec.GridSpec(rows, cols)\n", | |
" gs1.update(wspace=0.025, hspace=0.025) # set the spacing between axes. \n", | |
"\n", | |
" for i in range(n_images):\n", | |
" ax1 = plt.subplot(gs1[i])\n", | |
" plt.axis('on')\n", | |
" ax1.set_xticklabels([])\n", | |
" ax1.set_yticklabels([])\n", | |
" ax1.set_aspect('equal')\n", | |
" plt.gray()\n", | |
" plt.imshow(images[i])\n", | |
"\n", | |
" plt.show()\n", | |
"\n", | |
"show_images(x_train[:48])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# step 3: Reshaping to 2D arrays and normalizing data\n", | |
"x_train = x_train.reshape(-1, 784).astype('float32')\n", | |
"x_test = x_test.reshape(-1, 784).astype('float32')\n", | |
"\n", | |
"x_train /= 255\n", | |
"x_test /= 255\n", | |
"print('number of training examples: ',x_train.shape[0])\n", | |
"print('number of testing examples: ',x_test.shape[0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# step 4: Making categorical labels (one-hot labels)\n", | |
"y_train = keras.utils.to_categorical(y_train, num_classes)\n", | |
"y_test = keras.utils.to_categorical(y_test, num_classes)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# step 5: Building model\n", | |
"model = Sequential()\n", | |
"model.add(Dense(512, activation='relu', input_shape=(784,)))\n", | |
"model.add(Dropout(0.2))\n", | |
"model.add(Dense(512, activation='relu'))\n", | |
"model.add(Dropout(0.2))\n", | |
"model.add(Dense(num_classes, activation='softmax'))\n", | |
"\n", | |
"# summary of model's parameters\n", | |
"model.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# step 6: Compiling model\n", | |
"model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# step 7: Training\n", | |
"model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# step 8: Evaluating\n", | |
"score = model.evaluate(x_test, y_test, verbose=0)\n", | |
"print('test_loss={:.4f} test_accuracy={:.4f}'.format(score[0], score[1]))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment