Skip to content

Commit ad5cf59

Browse files
committed
load MNIST data as two-dimensional matrix
1 parent 730ad36 commit ad5cf59

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

LeNet-Lab-Solution.ipynb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"source": [
3131
"from tensorflow.examples.tutorials.mnist import input_data\n",
3232
"\n",
33-
"mnist = input_data.read_data_sets(\"MNIST_data/\")\n",
33+
"mnist = input_data.read_data_sets(\"MNIST_data/\", reshape=False)\n",
3434
"X_train, y_train = mnist.train.images, mnist.train.labels\n",
3535
"X_validation, y_validation = mnist.validation.images, mnist.validation.labels\n",
3636
"X_test, y_test = mnist.test.images, mnist.test.labels\n",
@@ -51,14 +51,11 @@
5151
"cell_type": "markdown",
5252
"metadata": {},
5353
"source": [
54-
"The MNIST data that TensorFlow pre-loads comes as an \"unrolled\" vector of 784 pixels.\n",
54+
"The MNIST data that TensorFlow pre-loads comes as 28x28x1 images.\n",
5555
"\n",
56-
"However, the LeNet architecture only accepts 32x32 images.\n",
56+
"However, the LeNet architecture only accepts 32x32xC images, where C is the number of color channels.\n",
5757
"\n",
58-
"In order to reformat the MNIST data into a shape that LeNet will accept, we proceed in two steps:\n",
59-
"\n",
60-
"1. Reshape the 784 pixel vector into a 28x28 matrix (28x28 = 784).\n",
61-
"2. Pad the data with two rows of zeros on the top and bottom, and two columns of zeros on the left and right (28+2+2 = 32).\n",
58+
"In order to reformat the MNIST data into a shape that LeNet will accept, we pad the data with two rows of zeros on the top and bottom, and two columns of zeros on the left and right (28+2+2 = 32).\n",
6259
"\n",
6360
"You do not need to modify this section."
6461
]
@@ -73,11 +70,6 @@
7370
"source": [
7471
"import numpy as np\n",
7572
"\n",
76-
"# Reshape MNIST image from vector to matrix\n",
77-
"X_train = np.reshape(X_train, (-1, 28, 28, 1))\n",
78-
"X_validation = np.reshape(X_validation, (-1, 28, 28, 1))\n",
79-
"X_test = np.reshape(X_test, (-1, 28, 28, 1))\n",
80-
"\n",
8173
"# Pad images with 0s\n",
8274
"X_train = np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')\n",
8375
"X_validation = np.pad(X_validation, ((0,0),(2,2),(2,2),(0,0)), 'constant')\n",

LeNet-Lab.ipynb

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"source": [
3131
"from tensorflow.examples.tutorials.mnist import input_data\n",
3232
"\n",
33-
"mnist = input_data.read_data_sets(\"MNIST_data/\")\n",
33+
"mnist = input_data.read_data_sets(\"MNIST_data/\", reshape=False)\n",
3434
"X_train, y_train = mnist.train.images, mnist.train.labels\n",
3535
"X_validation, y_validation = mnist.validation.images, mnist.validation.labels\n",
3636
"X_test, y_test = mnist.test.images, mnist.test.labels\n",
@@ -51,14 +51,11 @@
5151
"cell_type": "markdown",
5252
"metadata": {},
5353
"source": [
54-
"The MNIST data that TensorFlow pre-loads comes as an \"unrolled\" vector of 784 pixels.\n",
54+
"The MNIST data that TensorFlow pre-loads comes as 28x28x1 images.\n",
5555
"\n",
56-
"However, the LeNet architecture only accepts 32x32 images.\n",
56+
"However, the LeNet architecture only accepts 32x32xC images, where C is the number of color channels.\n",
5757
"\n",
58-
"In order to reformat the MNIST data into a shape that LeNet will accept, we proceed in two steps:\n",
59-
"\n",
60-
"1. Reshape the 784 pixel vector into a 28x28 matrix (28x28 = 784).\n",
61-
"2. Pad the data with two rows of zeros on the top and bottom, and two columns of zeros on the left and right (28+2+2 = 32).\n",
58+
"In order to reformat the MNIST data into a shape that LeNet will accept, we pad the data with two rows of zeros on the top and bottom, and two columns of zeros on the left and right (28+2+2 = 32).\n",
6259
"\n",
6360
"You do not need to modify this section."
6461
]
@@ -73,11 +70,6 @@
7370
"source": [
7471
"import numpy as np\n",
7572
"\n",
76-
"# Reshape MNIST image from vector to matrix\n",
77-
"X_train = np.reshape(X_train, (-1, 28, 28, 1))\n",
78-
"X_validation = np.reshape(X_validation, (-1, 28, 28, 1))\n",
79-
"X_test = np.reshape(X_test, (-1, 28, 28, 1))\n",
80-
"\n",
8173
"# Pad images with 0s\n",
8274
"X_train = np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')\n",
8375
"X_validation = np.pad(X_validation, ((0,0),(2,2),(2,2),(0,0)), 'constant')\n",
@@ -328,7 +320,7 @@
328320
" sess = tf.get_default_session()\n",
329321
" for offset in range(0, num_examples, BATCH_SIZE):\n",
330322
" batch_x, batch_y = X_data[offset:offset+BATCH_SIZE], y_data[offset:offset+BATCH_SIZE]\n",
331-
" accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y})\n",
323+
" accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y})\n",
332324
" total_accuracy += (accuracy * len(batch_x))\n",
333325
" return total_accuracy / num_examples"
334326
]
@@ -368,7 +360,7 @@
368360
" for offset in range(0, num_examples, BATCH_SIZE):\n",
369361
" end = offset + BATCH_SIZE\n",
370362
" batch_x, batch_y = X_train[offset:end], y_train[offset:end]\n",
371-
" loss = sess.run(training_operation, feed_dict={x: batch_x, y: batch_y})\n",
363+
" sess.run(training_operation, feed_dict={x: batch_x, y: batch_y})\n",
372364
" \n",
373365
" validation_accuracy = evaluate(X_validation, y_validation)\n",
374366
" print(\"EPOCH {} ...\".format(i+1))\n",

0 commit comments

Comments
 (0)