Skip to content

Instantly share code, notes, and snippets.

@georgehc
Last active February 26, 2018 14:43
Show Gist options
  • Save georgehc/147a29b86d6d306078e1515415795d91 to your computer and use it in GitHub Desktop.
Save georgehc/147a29b86d6d306078e1515415795d91 to your computer and use it in GitHub Desktop.
95-865 Convnet continuation of deep learning handwritten digit demo
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n",
"/Users/georgehc/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6\n",
" return f(*args, **kwds)\n"
]
}
],
"source": [
"# This demo draws heavily from the handwritten digit example in\n",
"# Chapter 2 of Francois Chollet's \"Deep Learning with Python\" book.\n",
"# I've added a simpler single-layer example first before moving to\n",
"# the 2-layer example. -George Chen (CMU Fall 2017)\n",
"# Addendum: several convnet examples at the end -George Chen (CMU Fall 2017) \n",
"\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"from keras.datasets import mnist\n",
"from keras import models\n",
"from keras import layers\n",
"\n",
"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(60000, 28, 28)\n"
]
}
],
"source": [
"print(train_images.shape)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(10000, 28, 28)\n"
]
}
],
"source": [
"print(test_images.shape)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"flattened_train_images = train_images.reshape(len(train_images), -1) # flattens out each training image\n",
"flattened_test_images = test_images.reshape(len(test_images), -1) # flattens out each test image\n",
"flattened_train_images = flattened_train_images.astype(np.float32) / 255 # rescale to be between 0 and 1\n",
"flattened_test_images = flattened_test_images.astype(np.float32) / 255 # rescale to be between 0 and 1\n",
"\n",
"from keras.utils import to_categorical\n",
"train_labels_categorical = to_categorical(train_labels)\n",
"test_labels_categorical = to_categorical(test_labels)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_labels[6]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_labels_categorical[6]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(60000, 784)\n"
]
}
],
"source": [
"print(flattened_train_images.shape)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(-0.5, 27.5, 27.5, -0.5)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAABjRJREFUeJzt3S9oVX0cx/Gdx8kUFBWDUYtgM2nS\nIgjiHwTRosgEkyIahDXbQJgywSIGwWJRUItFsA0xuCKIFoOijJWhpuFg50kPPOV+t9273bl9Xq/6\n2Tn3BN+c8PNuTdu2A0Cef1b7AYDVIX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4INdjPD2uaxn8nhBXW\ntm2zmJ/z5odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ\n4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQ4odQff0T3ayMa9euddxu3bpVXjs4\nWP8TGB0dLfd79+6VO38vb34IJX4IJX4IJX4IJX4IJX4IJX4I1bRt278Pa5r+fdg6cvPmzXIfGxvr\nuM3NzZXXDg0NdfVM/zlz5ky5v3jxoqf7s3Rt2zaL+TlvfgglfgglfgglfgglfgglfgglfgjlnH8N\neP36dbnPzs523C5fvlxe++TJk3I/evRouU9MTJT74cOHy53l55wfKIkfQokfQokfQokfQokfQvnV\n3WvA8PBwuU9PT3fc5ufny2u/fPlS7gsd9bF2efNDKPFDKPFDKPFDKPFDKPFDKPFDKOf8a8DU1NRq\nPwLrkDc/hBI/hBI/hBI/hBI/hBI/hBI/hHLOv85t2bKl3C9cuNDT/ScnJ3u6ntXjzQ+hxA+hxA+h\nxA+hxA+hxA+hxA+hnPOvc48ePSr3rVu3lvtCf8L96dOnS34m/g7e/BBK/BBK/BBK/BBK/BBK/BBK\n/BCqWegcd1k/rGn692FBrl+/3nEbHx8vr92wYUNPnz07O1vujx8/7rjdvn27vPbbt2/dPFK8tm2b\nxfycNz+EEj+EEj+EEj+EEj+EEj+EctS3BuzZs6fcP3361HHbtGnTMj/N8vn582e5nzp1qtwnJiaW\n83HWDUd9QEn8EEr8EEr8EEr8EEr8EEr8EMqv7l4DLl68WO7VWf7MzEx57dWrV8v948eP5b5///5y\nr351+Pbt28trnz9/Xu579+4t91+/fpV7Om9+CCV+CCV+CCV+CCV+CCV+CCV+COX7/GvAjh07yv3Y\nsWMdt7dv35bXfv36tatnWqyDBw923N69e1de2zT119JPnjxZ7q9evSr39cr3+YGS+CGU+CGU+CGU\n+CGU+CGU+CGUc35WzbNnz8r97Nmz5X7nzp1yHxkZWfIzrQfO+YGS+CGU+CGU+CGU+CGU+CGU+CGU\n39vPiqq+k79x48ae7r3Q7+2n5s0PocQPocQPocQPocQPocQPoRz1saK2bdvWcTt9+nRP9/7+/XtP\n16fz5odQ4odQ4odQ4odQ4odQ4odQ4odQzvlZUZs3b+762vn5+XJ/+fJl1/fGmx9iiR9CiR9CiR9C\niR9CiR9CiR9COefvg3PnzpX7iRMnyv3SpUvL+DTLa6Fnv3v3btf3Hh0dLfc3b950fW+8+SGW+CGU\n+CGU+CGU+CGU+CGU+CFU07Zt/z6safr3YX20c+fOcp+amupp371795KfabGGhobK/cGDB+V+/vz5\nru8/OTlZXnvkyJFy//37d7mnatu2899F/x9vfgglfgglfgglfgglfgglfgjlK73L4M+fP+U+PT1d\n7rt27Sr3kZGRcv/x40e5V27cuFHuBw4c6PreAwMDAx8+fOi4HT9+vLzWUd7K8uaHUOKHUOKHUOKH\nUOKHUOKHUOKHUL7S2wdXrlwp9/v375f74ODq/XeMmZmZch8fHy/3sbGxjtvc3FxXz0TNV3qBkvgh\nlPghlPghlPghlPghlPghlHP+v8ChQ4fKfXh4uNz37dvXcfv8+XNXz/Sfhw8flvv79+97uj/Lzzk/\nUBI/hBI/hBI/hBI/hBI/hBI/hHLOD+uMc36gJH4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4I\nJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4IJX4I\n1dc/0Q38Pbz5IZT4IZT4IZT4IZT4IZT4IZT4IZT4IZT4IZT4IZT4IZT4IZT4IZT4IZT4IZT4IZT4\nIZT4IZT4IZT4IZT4IZT4IZT4IdS/LwkD0UEZ94EAAAAASUVORK5CYII=\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x181f54beb8>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# this plots out one of the training images\n",
"idx = np.random.randint(len(train_images)) # random training image index\n",
"plt.imshow(train_images[idx], cmap='gray')\n",
"plt.axis('off')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(-0.5, 9.5, 783.5, -0.5)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAADEAAAD8CAYAAADe6kx2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAAoBJREFUeJztnSGu4mAURm8nk2AgwRCCYiUotsEG\n2AOSXWAQLACNJawBjcThwHVcB9veSd6Zm3NUzZf08Kffbfn7Hk3btvG/8+unT+BfoAQFJSgoQUEJ\nCr9/+gQiIpqm6W4b2rZt+uZLrIQSFJSgoAQFJSgoQUEJCkpQUIKCEhSUoKAEBSUoKEFBCQpKUFCC\nAmKn6Pl8pvIN4QUVt7tCCQ5KUCghgZgTn88nlXdOUFCCghIUEBV7uVxS+RIr4ZygoAQFJSgg5sRi\nsUjlERUbEd8n0btiESvRNH/Pe8iHWuKaUIKCEhRKSCAq9nq9pvKIYefzRCjBQQkKiIodj8epPEJi\nv9+n8s4JCkpQUIIComKzDYmQ8CubUIKDEhRKSCAqtsScWC6X3fHj8eid93mCghIUSkgg2un9fqfy\niHaKClvAq9WqOx6yV4FYCedEKMGhhASinUrcim82m+74dDr1ziMqdjqddifxer16VyxCwjkRSnAo\nIYGo2O9vO4aAkJjP56k8QmI2m6XyzgkKSlBQgoISFJSgoAQFJSggbsXP53Mq7604BSUoKEGhhARi\nTpTYZPG92FCCgxIUSkggKna9XqfyPk9QUIIC4sI+Ho+pfIkLG7ES9/s9lS+xEiUubCUoKEEBUbGj\n0SiVR0jcbrdU3jlBQQkKiAt7Mpmk8giJ7XabyiPaKSr8ddf3K6VDfikHsRLOiVCCQwkJRDuV2Hjc\n7Xbd8ZB/4mbFUlCCghIUEBV7OBxSeSuWghIUlKCgBAUlKChBQQkKSlBQgoISFJSgoAQFJSgoQUEJ\nCkpQUIJCCQnEdleWEiuhBAUlKChBQQkKSlBQgoISFJSgoAQFJSgoQeEPPWHZgogv7NEAAAAASUVO\nRK5CYII=\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x181f54b3c8>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# this is just to visualize what the flattened version of the image above looks like;\n",
"# after flattening the image I replicate it horizontally (by 10 pixels) just for\n",
"# visualization purposes since otherwise the image is so thin (horizontally) that\n",
"# it's not easy to see anything\n",
"plt.imshow(np.hstack([train_images[idx].flatten().reshape((784,1))]*10), cmap='gray')\n",
"plt.axis('off')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"dense_1 (Dense) (None, 10) 7850 \n",
"=================================================================\n",
"Total params: 7,850\n",
"Trainable params: 7,850\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"# extremely shallow single-layer model\n",
"shallow_single_layer_model = models.Sequential() # this is Keras's way of specifying a model that is a single sequence of layers\n",
"shallow_single_layer_model.add(layers.Dense(10, activation='softmax', input_shape=(784,)))\n",
"shallow_single_layer_model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"shallow_single_layer_model.compile(optimizer='rmsprop',\n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/5\n",
"60000/60000 [==============================] - 1s 21us/step - loss: 0.5919 - acc: 0.8562\n",
"Epoch 2/5\n",
"60000/60000 [==============================] - 1s 19us/step - loss: 0.3305 - acc: 0.9086\n",
"Epoch 3/5\n",
"60000/60000 [==============================] - 1s 18us/step - loss: 0.3013 - acc: 0.9155\n",
"Epoch 4/5\n",
"60000/60000 [==============================] - 1s 17us/step - loss: 0.2880 - acc: 0.9200\n",
"Epoch 5/5\n",
"60000/60000 [==============================] - 1s 19us/step - loss: 0.2801 - acc: 0.9221\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x181f54bcf8>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"shallow_single_layer_model.fit(flattened_train_images, train_labels_categorical, epochs=5, batch_size=128)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 0s 33us/step\n",
"Test accuracy: 0.9232\n"
]
}
],
"source": [
"test_loss, test_acc = shallow_single_layer_model.evaluate(flattened_test_images, test_labels_categorical)\n",
"print('Test accuracy:', test_acc)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"dense_2 (Dense) (None, 512) 401920 \n",
"_________________________________________________________________\n",
"dense_3 (Dense) (None, 10) 5130 \n",
"=================================================================\n",
"Total params: 407,050\n",
"Trainable params: 407,050\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"# two-layer model\n",
"two_layer_model = models.Sequential() # this is Keras's way of specifying a model that is a single sequence of layers\n",
"two_layer_model.add(layers.Dense(512, activation='relu', input_shape=(784,)))\n",
"two_layer_model.add(layers.Dense(10, activation='softmax'))\n",
"two_layer_model.compile(optimizer='rmsprop',\n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy'])\n",
"two_layer_model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/5\n",
"60000/60000 [==============================] - 6s 100us/step - loss: 0.2526 - acc: 0.9278\n",
"Epoch 2/5\n",
"60000/60000 [==============================] - 6s 100us/step - loss: 0.1022 - acc: 0.9697\n",
"Epoch 3/5\n",
"60000/60000 [==============================] - 6s 96us/step - loss: 0.0683 - acc: 0.9793\n",
"Epoch 4/5\n",
"60000/60000 [==============================] - 6s 96us/step - loss: 0.0498 - acc: 0.9856\n",
"Epoch 5/5\n",
"60000/60000 [==============================] - 6s 102us/step - loss: 0.0363 - acc: 0.9894\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x1838c1bac8>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"two_layer_model.fit(flattened_train_images, train_labels_categorical, epochs=5, batch_size=128)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 1s 65us/step\n",
"Test accuracy: 0.9802\n"
]
}
],
"source": [
"test_loss, test_acc = two_layer_model.evaluate(flattened_test_images, test_labels_categorical)\n",
"print('Test accuracy:', test_acc)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"scaled_train_images = train_images.reshape(len(train_images), train_images.shape[1], train_images.shape[2], -1) # flattens out each training image\n",
"scaled_test_images = test_images.reshape(len(test_images), test_images.shape[1], test_images.shape[2], -1) # flattens out each test image\n",
"scaled_train_images = scaled_train_images.astype(np.float32) / 255 # rescale to be between 0 and 1\n",
"scaled_test_images = scaled_test_images.astype(np.float32) / 255 # rescale to be between 0 and 1"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(60000, 28, 28, 1)\n"
]
}
],
"source": [
"print(scaled_train_images.shape)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d_5 (Conv2D) (None, 26, 26, 32) 320 \n",
"_________________________________________________________________\n",
"max_pooling2d_5 (MaxPooling2 (None, 13, 13, 32) 0 \n",
"_________________________________________________________________\n",
"flatten_3 (Flatten) (None, 5408) 0 \n",
"_________________________________________________________________\n",
"dense_10 (Dense) (None, 512) 2769408 \n",
"_________________________________________________________________\n",
"dense_11 (Dense) (None, 10) 5130 \n",
"=================================================================\n",
"Total params: 2,774,858\n",
"Trainable params: 2,774,858\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"simple_convnet_model = models.Sequential()\n",
"simple_convnet_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\n",
"simple_convnet_model.add(layers.MaxPooling2D((2, 2)))\n",
"simple_convnet_model.add(layers.Flatten())\n",
"simple_convnet_model.add(layers.Dense(512, activation='relu'))\n",
"simple_convnet_model.add(layers.Dense(10, activation='softmax'))\n",
"simple_convnet_model.summary()\n",
"\n",
"simple_convnet_model.compile(optimizer='rmsprop',\n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/5\n",
"60000/60000 [==============================] - 63s 1ms/step - loss: 0.1826 - acc: 0.9428\n",
"Epoch 2/5\n",
"60000/60000 [==============================] - 59s 989us/step - loss: 0.0500 - acc: 0.9846\n",
"Epoch 3/5\n",
"60000/60000 [==============================] - 61s 1ms/step - loss: 0.0300 - acc: 0.9906\n",
"Epoch 4/5\n",
"60000/60000 [==============================] - 63s 1ms/step - loss: 0.0188 - acc: 0.9942\n",
"Epoch 5/5\n",
"60000/60000 [==============================] - 59s 991us/step - loss: 0.0121 - acc: 0.9965\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x18390da5c0>"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"simple_convnet_model.fit(scaled_train_images, train_labels_categorical, epochs=5, batch_size=128)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 3s 338us/step\n",
"Test accuracy: 0.9886\n"
]
}
],
"source": [
"test_loss, test_acc = simple_convnet_model.evaluate(scaled_test_images, test_labels_categorical)\n",
"print('Test accuracy:', test_acc)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d_7 (Conv2D) (None, 26, 26, 32) 320 \n",
"_________________________________________________________________\n",
"max_pooling2d_7 (MaxPooling2 (None, 13, 13, 32) 0 \n",
"_________________________________________________________________\n",
"flatten_5 (Flatten) (None, 5408) 0 \n",
"_________________________________________________________________\n",
"dense_14 (Dense) (None, 64) 346176 \n",
"_________________________________________________________________\n",
"dense_15 (Dense) (None, 10) 650 \n",
"=================================================================\n",
"Total params: 347,146\n",
"Trainable params: 347,146\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"simple_convnet_model_avoid_overfitting = models.Sequential()\n",
"simple_convnet_model_avoid_overfitting.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\n",
"simple_convnet_model_avoid_overfitting.add(layers.MaxPooling2D((2, 2)))\n",
"simple_convnet_model_avoid_overfitting.add(layers.Flatten())\n",
"simple_convnet_model_avoid_overfitting.add(layers.Dense(64, activation='relu'))\n",
"simple_convnet_model_avoid_overfitting.add(layers.Dense(10, activation='softmax'))\n",
"simple_convnet_model_avoid_overfitting.summary()\n",
"\n",
"simple_convnet_model_avoid_overfitting.compile(optimizer='rmsprop',\n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/5\n",
"60000/60000 [==============================] - 35s 578us/step - loss: 0.2446 - acc: 0.9294\n",
"Epoch 2/5\n",
"60000/60000 [==============================] - 34s 573us/step - loss: 0.0774 - acc: 0.9772\n",
"Epoch 3/5\n",
"60000/60000 [==============================] - 37s 616us/step - loss: 0.0523 - acc: 0.9842\n",
"Epoch 4/5\n",
"60000/60000 [==============================] - 37s 611us/step - loss: 0.0398 - acc: 0.9882\n",
"Epoch 5/5\n",
"60000/60000 [==============================] - 36s 593us/step - loss: 0.0308 - acc: 0.9909\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x183beb1a58>"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"simple_convnet_model_avoid_overfitting.fit(scaled_train_images, train_labels_categorical, epochs=5, batch_size=128)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 2s 206us/step\n",
"Test accuracy: 0.9842\n"
]
}
],
"source": [
"test_loss, test_acc = simple_convnet_model_avoid_overfitting.evaluate(scaled_test_images, test_labels_categorical)\n",
"print('Test accuracy:', test_acc)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d_8 (Conv2D) (None, 26, 26, 32) 320 \n",
"_________________________________________________________________\n",
"max_pooling2d_8 (MaxPooling2 (None, 13, 13, 32) 0 \n",
"_________________________________________________________________\n",
"conv2d_9 (Conv2D) (None, 11, 11, 32) 9248 \n",
"_________________________________________________________________\n",
"max_pooling2d_9 (MaxPooling2 (None, 5, 5, 32) 0 \n",
"_________________________________________________________________\n",
"flatten_6 (Flatten) (None, 800) 0 \n",
"_________________________________________________________________\n",
"dense_16 (Dense) (None, 64) 51264 \n",
"_________________________________________________________________\n",
"dense_17 (Dense) (None, 10) 650 \n",
"=================================================================\n",
"Total params: 61,482\n",
"Trainable params: 61,482\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"deeper_convnet_model_avoid_overfitting = models.Sequential()\n",
"deeper_convnet_model_avoid_overfitting.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\n",
"deeper_convnet_model_avoid_overfitting.add(layers.MaxPooling2D((2, 2)))\n",
"deeper_convnet_model_avoid_overfitting.add(layers.Conv2D(32, (3, 3), activation='relu'))\n",
"deeper_convnet_model_avoid_overfitting.add(layers.MaxPooling2D((2, 2)))\n",
"deeper_convnet_model_avoid_overfitting.add(layers.Flatten())\n",
"deeper_convnet_model_avoid_overfitting.add(layers.Dense(64, activation='relu'))\n",
"deeper_convnet_model_avoid_overfitting.add(layers.Dense(10, activation='softmax'))\n",
"deeper_convnet_model_avoid_overfitting.summary()\n",
"\n",
"deeper_convnet_model_avoid_overfitting.compile(optimizer='rmsprop',\n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/5\n",
"60000/60000 [==============================] - 48s 795us/step - loss: 0.2635 - acc: 0.9201\n",
"Epoch 2/5\n",
"60000/60000 [==============================] - 53s 883us/step - loss: 0.0739 - acc: 0.9775\n",
"Epoch 3/5\n",
"60000/60000 [==============================] - 51s 849us/step - loss: 0.0491 - acc: 0.9843\n",
"Epoch 4/5\n",
"60000/60000 [==============================] - 44s 731us/step - loss: 0.0389 - acc: 0.9880\n",
"Epoch 5/5\n",
"60000/60000 [==============================] - 48s 801us/step - loss: 0.0314 - acc: 0.9902\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x183a71b908>"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"deeper_convnet_model_avoid_overfitting.fit(scaled_train_images, train_labels_categorical, epochs=5, batch_size=128)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 3s 290us/step\n",
"Test accuracy: 0.9891\n"
]
}
],
"source": [
"test_loss, test_acc = deeper_convnet_model_avoid_overfitting.evaluate(scaled_test_images, test_labels_categorical)\n",
"print('Test accuracy:', test_acc)"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment