Skip to content

Instantly share code, notes, and snippets.

@lenhhoxung86
Last active December 22, 2018 23:25
Show Gist options
  • Save lenhhoxung86/1d0d213370af75566de0b9982985af08 to your computer and use it in GitHub Desktop.
Save lenhhoxung86/1d0d213370af75566de0b9982985af08 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"'''A deep convolutional neural networks with two convolution layers, one max pooling and 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, Flatten\n",
"from keras.layers import Conv2D, MaxPooling2D\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.gridspec as gridspec"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"# step 2: Loading data and visualizing some examples\n",
"\n",
"# training parameters\n",
"batch_size = 128\n",
"num_classes = 10\n",
"epochs = 5\n",
"\n",
"# data loading\n",
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
"\n",
"# width and height of images\n",
"img_rows, img_cols = x_train.shape[1], x_train.shape[2]\n",
"\n",
"# visualization of some examples\n",
"def show_images(images, rows = 6, titles = None):\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",
" titles: List of titles corresponding to each image. Must have\n",
" the same length as titles.\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 4D arrays and normalizing data\n",
"x_train = x_train.reshape(-1, img_rows, img_cols, 1).astype('float32')\n",
"x_test = x_test.reshape(-1, img_rows, img_cols, 1).astype('float32')\n",
"input_shape = (img_rows, img_cols, 1)\n",
"\n",
"x_train /= 255\n",
"x_test /= 255\n",
"print('x_train shape:', x_train.shape)\n",
"print('number of training examples: ',x_train.shape[0])\n",
"print('number of testing examples: ',x_test.shape[0])\n"
]
},
{
"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(Conv2D(32, kernel_size=(5, 5),\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.25))\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=keras.losses.categorical_crossentropy,\n",
" optimizer=keras.optimizers.Adam(lr=0.001),\n",
" 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