|
30 | 30 | "source": [
|
31 | 31 | "from tensorflow.examples.tutorials.mnist import input_data\n",
|
32 | 32 | "\n",
|
33 |
| - "mnist = input_data.read_data_sets(\"MNIST_data/\")\n", |
| 33 | + "mnist = input_data.read_data_sets(\"MNIST_data/\", reshape=False)\n", |
34 | 34 | "X_train, y_train = mnist.train.images, mnist.train.labels\n",
|
35 | 35 | "X_validation, y_validation = mnist.validation.images, mnist.validation.labels\n",
|
36 | 36 | "X_test, y_test = mnist.test.images, mnist.test.labels\n",
|
|
51 | 51 | "cell_type": "markdown",
|
52 | 52 | "metadata": {},
|
53 | 53 | "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", |
55 | 55 | "\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", |
57 | 57 | "\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", |
62 | 59 | "\n",
|
63 | 60 | "You do not need to modify this section."
|
64 | 61 | ]
|
|
73 | 70 | "source": [
|
74 | 71 | "import numpy as np\n",
|
75 | 72 | "\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", |
81 | 73 | "# Pad images with 0s\n",
|
82 | 74 | "X_train = np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')\n",
|
83 | 75 | "X_validation = np.pad(X_validation, ((0,0),(2,2),(2,2),(0,0)), 'constant')\n",
|
|
328 | 320 | " sess = tf.get_default_session()\n",
|
329 | 321 | " for offset in range(0, num_examples, BATCH_SIZE):\n",
|
330 | 322 | " 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", |
332 | 324 | " total_accuracy += (accuracy * len(batch_x))\n",
|
333 | 325 | " return total_accuracy / num_examples"
|
334 | 326 | ]
|
|
368 | 360 | " for offset in range(0, num_examples, BATCH_SIZE):\n",
|
369 | 361 | " end = offset + BATCH_SIZE\n",
|
370 | 362 | " 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", |
372 | 364 | " \n",
|
373 | 365 | " validation_accuracy = evaluate(X_validation, y_validation)\n",
|
374 | 366 | " print(\"EPOCH {} ...\".format(i+1))\n",
|
|
0 commit comments