Skip to content

Instantly share code, notes, and snippets.

@georgehc
Last active February 18, 2019 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgehc/c49023ddf82ec6a425aa71f01291b680 to your computer and use it in GitHub Desktop.
Save georgehc/c49023ddf82ec6a425aa71f01291b680 to your computer and use it in GitHub Desktop.
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"
]
}
],
"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",
"\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"from keras.datasets import mnist\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense\n",
"\n",
"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\n",
"\n",
"flattened_train_images = train_images.reshape(len(train_images), -1) # flattens out each training image\n",
"flattened_train_images = flattened_train_images.astype(np.float32) / 255 # rescale to be between 0 and 1\n",
"flattened_test_images = test_images.reshape(len(test_images), -1) # flattens out each test image\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": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_labels[0]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], dtype=float32)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_labels_categorical[0]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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",
"single_layer_model = Sequential() # this is Keras's way of specifying a model that is a single sequence of layers\n",
"single_layer_model.add(Dense(10, activation='softmax', input_shape=(784,)))\n",
"single_layer_model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"single_layer_model.compile(optimizer='rmsprop',\n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 48000 samples, validate on 12000 samples\n",
"Epoch 1/5\n",
"48000/48000 [==============================] - 3s 66us/step - loss: 0.6779 - acc: 0.8305 - val_loss: 0.3614 - val_acc: 0.9015\n",
"Epoch 2/5\n",
"48000/48000 [==============================] - 3s 62us/step - loss: 0.3526 - acc: 0.9020 - val_loss: 0.3081 - val_acc: 0.9163\n",
"Epoch 3/5\n",
"48000/48000 [==============================] - 3s 67us/step - loss: 0.3159 - acc: 0.9120 - val_loss: 0.2895 - val_acc: 0.9203\n",
"Epoch 4/5\n",
"48000/48000 [==============================] - 3s 64us/step - loss: 0.2994 - acc: 0.9166 - val_loss: 0.2807 - val_acc: 0.9223\n",
"Epoch 5/5\n",
"48000/48000 [==============================] - 3s 62us/step - loss: 0.2896 - acc: 0.9183 - val_loss: 0.2771 - val_acc: 0.9217\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x13275d0f0>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"single_layer_model.fit(flattened_train_images,\n",
" train_labels_categorical,\n",
" validation_split=0.2,\n",
" epochs=5,\n",
" batch_size=128)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"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 = Sequential() # this is Keras's way of specifying a model that is a single sequence of layers\n",
"two_layer_model.add(Dense(512, activation='relu', input_shape=(784,)))\n",
"two_layer_model.add(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": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 48000 samples, validate on 12000 samples\n",
"Epoch 1/5\n",
"48000/48000 [==============================] - 55s 1ms/step - loss: 0.2878 - acc: 0.9173 - val_loss: 0.1465 - val_acc: 0.9573\n",
"Epoch 2/5\n",
"48000/48000 [==============================] - 53s 1ms/step - loss: 0.1178 - acc: 0.9654 - val_loss: 0.1098 - val_acc: 0.9672\n",
"Epoch 3/5\n",
"48000/48000 [==============================] - 53s 1ms/step - loss: 0.0774 - acc: 0.9772 - val_loss: 0.0925 - val_acc: 0.9714\n",
"Epoch 4/5\n",
"48000/48000 [==============================] - 54s 1ms/step - loss: 0.0559 - acc: 0.9836 - val_loss: 0.0898 - val_acc: 0.9707\n",
"Epoch 5/5\n",
"48000/48000 [==============================] - 52s 1ms/step - loss: 0.0419 - acc: 0.9871 - val_loss: 0.0795 - val_acc: 0.9773\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x132735a20>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"two_layer_model.fit(flattened_train_images,\n",
" train_labels_categorical,\n",
" validation_split=0.2,\n",
" epochs=5,\n",
" batch_size=128)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finally evaluate on test data"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 0s 48us/step\n",
"Test accuracy: 0.9216\n"
]
}
],
"source": [
"test_loss, test_acc = single_layer_model.evaluate(flattened_test_images, test_labels_categorical)\n",
"print('Test accuracy:', test_acc)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 5s 503us/step\n",
"Test accuracy: 0.9776\n"
]
}
],
"source": [
"test_loss, test_acc = two_layer_model.evaluate(flattened_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.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment