Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save georgehc/5d20844bad25be837ae9583188c6bfef to your computer and use it in GitHub Desktop.
Save georgehc/5d20844bad25be837ae9583188c6bfef 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 tensorflow.python import keras\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": [
"Model: \"sequential_1\"\n",
"_________________________________________________________________\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='adam',\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 [==============================] - 1s 23us/step - loss: 0.7324 - accuracy: 0.8216 - val_loss: 0.4063 - val_accuracy: 0.8978\n",
"Epoch 2/5\n",
"48000/48000 [==============================] - 1s 24us/step - loss: 0.3882 - accuracy: 0.8972 - val_loss: 0.3352 - val_accuracy: 0.9096\n",
"Epoch 3/5\n",
"48000/48000 [==============================] - 1s 21us/step - loss: 0.3376 - accuracy: 0.9070 - val_loss: 0.3066 - val_accuracy: 0.9155\n",
"Epoch 4/5\n",
"48000/48000 [==============================] - 1s 19us/step - loss: 0.3141 - accuracy: 0.9133 - val_loss: 0.2933 - val_accuracy: 0.9186\n",
"Epoch 5/5\n",
"48000/48000 [==============================] - 1s 20us/step - loss: 0.2999 - accuracy: 0.9169 - val_loss: 0.2841 - val_accuracy: 0.9202\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.callbacks.History at 0x64be81f10>"
]
},
"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": [
"Model: \"sequential_2\"\n",
"_________________________________________________________________\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='adam',\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 [==============================] - 4s 82us/step - loss: 0.2961 - accuracy: 0.9159 - val_loss: 0.1568 - val_accuracy: 0.9561\n",
"Epoch 2/5\n",
"48000/48000 [==============================] - 3s 73us/step - loss: 0.1219 - accuracy: 0.9647 - val_loss: 0.1098 - val_accuracy: 0.9673\n",
"Epoch 3/5\n",
"48000/48000 [==============================] - 4s 75us/step - loss: 0.0780 - accuracy: 0.9779 - val_loss: 0.0939 - val_accuracy: 0.9716\n",
"Epoch 4/5\n",
"48000/48000 [==============================] - 4s 74us/step - loss: 0.0560 - accuracy: 0.9834 - val_loss: 0.0871 - val_accuracy: 0.9756\n",
"Epoch 5/5\n",
"48000/48000 [==============================] - 4s 75us/step - loss: 0.0408 - accuracy: 0.9884 - val_loss: 0.0856 - val_accuracy: 0.9758\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.callbacks.History at 0x64c90bb10>"
]
},
"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 25us/step\n",
"Test accuracy: 0.9214000105857849\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 [==============================] - 0s 46us/step\n",
"Test accuracy: 0.9764999747276306\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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment