|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 2, |
| 6 | + "metadata": { |
| 7 | + "collapsed": false |
| 8 | + }, |
| 9 | + "outputs": [ |
| 10 | + { |
| 11 | + "name": "stdout", |
| 12 | + "output_type": "stream", |
| 13 | + "text": [ |
| 14 | + "Extracting ./mnist/train-images-idx3-ubyte.gz\n", |
| 15 | + "Extracting ./mnist/train-labels-idx1-ubyte.gz\n", |
| 16 | + "Extracting ./mnist/t10k-images-idx3-ubyte.gz\n", |
| 17 | + "Extracting ./mnist/t10k-labels-idx1-ubyte.gz\n", |
| 18 | + "('Epoch:', '0001', 'cost=', '215.548141965')\n", |
| 19 | + "('Epoch:', '0002', 'cost=', '54.977557694')\n", |
| 20 | + "('Epoch:', '0003', 'cost=', '33.899888993')\n", |
| 21 | + "('Epoch:', '0004', 'cost=', '23.234023376')\n", |
| 22 | + "('Epoch:', '0005', 'cost=', '16.552313167')\n", |
| 23 | + "('Epoch:', '0006', 'cost=', '12.184614655')\n", |
| 24 | + "('Epoch:', '0007', 'cost=', '8.918999288')\n", |
| 25 | + "('Epoch:', '0008', 'cost=', '6.555203167')\n", |
| 26 | + "('Epoch:', '0009', 'cost=', '4.864825427')\n", |
| 27 | + "('Epoch:', '0010', 'cost=', '3.541727996')\n", |
| 28 | + "('Epoch:', '0011', 'cost=', '2.601980731')\n", |
| 29 | + "('Epoch:', '0012', 'cost=', '2.013708151')\n", |
| 30 | + "('Epoch:', '0013', 'cost=', '1.447752024')\n", |
| 31 | + "('Epoch:', '0014', 'cost=', '1.284220558')\n", |
| 32 | + "('Epoch:', '0015', 'cost=', '1.063494972')\n", |
| 33 | + "('Epoch:', '0016', 'cost=', '1.089214503')\n", |
| 34 | + "('Epoch:', '0017', 'cost=', '0.819465103')\n", |
| 35 | + "('Epoch:', '0018', 'cost=', '0.826465986')\n", |
| 36 | + "('Epoch:', '0019', 'cost=', '0.756363073')\n", |
| 37 | + "('Epoch:', '0020', 'cost=', '0.756904836')\n", |
| 38 | + "('Epoch:', '0021', 'cost=', '0.772401051')\n", |
| 39 | + "('Epoch:', '0022', 'cost=', '0.591537078')\n", |
| 40 | + "('Epoch:', '0023', 'cost=', '0.518754110')\n", |
| 41 | + "('Epoch:', '0024', 'cost=', '0.653424654')\n", |
| 42 | + "('Epoch:', '0025', 'cost=', '0.639180361')\n", |
| 43 | + "('Epoch:', '0026', 'cost=', '0.418257485')\n", |
| 44 | + "('Epoch:', '0027', 'cost=', '0.434976982')\n", |
| 45 | + "('Epoch:', '0028', 'cost=', '0.606400410')\n", |
| 46 | + "('Epoch:', '0029', 'cost=', '0.475488307')\n", |
| 47 | + "('Epoch:', '0030', 'cost=', '0.458589170')\n", |
| 48 | + "Optimization Finished!\n", |
| 49 | + "('Accuracy:', 0.96039999)\n" |
| 50 | + ] |
| 51 | + } |
| 52 | + ], |
| 53 | + "source": [ |
| 54 | + "#get the mnist data \n", |
| 55 | + "# wget http://deeplearning.net/data/mnist/mnist.pkl.gz\n", |
| 56 | + "\n", |
| 57 | + "\n", |
| 58 | + "\n", |
| 59 | + "\n", |
| 60 | + "from tensorflow.examples.tutorials.mnist import input_data\n", |
| 61 | + "mnist = input_data.read_data_sets(\"./mnist/\", one_hot=True)\n", |
| 62 | + "\n", |
| 63 | + "import tensorflow as tf\n", |
| 64 | + "\n", |
| 65 | + "# Parameters\n", |
| 66 | + "learning_rate = 0.001\n", |
| 67 | + "training_epochs = 30\n", |
| 68 | + "batch_size = 100\n", |
| 69 | + "display_step = 1\n", |
| 70 | + "\n", |
| 71 | + "# Network Parameters\n", |
| 72 | + "n_hidden_1 = 256 # 1st layer number of features\n", |
| 73 | + "n_hidden_2 = 512 # 2nd layer number of features\n", |
| 74 | + "n_input = 784 # MNIST data input (img shape: 28*28)\n", |
| 75 | + "n_classes = 10 # MNIST total classes (0-9 digits)\n", |
| 76 | + "\n", |
| 77 | + "# tf Graph input\n", |
| 78 | + "x = tf.placeholder(\"float\", [None, n_input])\n", |
| 79 | + "y = tf.placeholder(\"float\", [None, n_classes])\n", |
| 80 | + "\n", |
| 81 | + "\n", |
| 82 | + "# Create model\n", |
| 83 | + "def multilayer_perceptron(x, weights, biases):\n", |
| 84 | + " # Hidden layer with RELU activation\n", |
| 85 | + " layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])\n", |
| 86 | + " layer_1 = tf.nn.relu(layer_1)\n", |
| 87 | + " # Hidden layer with RELU activation\n", |
| 88 | + " layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])\n", |
| 89 | + " layer_2 = tf.nn.relu(layer_2)\n", |
| 90 | + "\n", |
| 91 | + " # layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])\n", |
| 92 | + " # layer_3 = tf.nn.relu(layer_3)\n", |
| 93 | + "\n", |
| 94 | + "\n", |
| 95 | + "\n", |
| 96 | + " #we can add dropout layer\n", |
| 97 | + " # drop_out = tf.nn.dropout(layer_2, 0.75)\n", |
| 98 | + "\n", |
| 99 | + "\n", |
| 100 | + "\n", |
| 101 | + " # Output layer with linear activation\n", |
| 102 | + " out_layer = tf.matmul(layer_2, weights['out']) + biases['out']\n", |
| 103 | + " return out_layer\n", |
| 104 | + "\n", |
| 105 | + "# Store layers weight & biases\n", |
| 106 | + "weights = {\n", |
| 107 | + " #you can change \n", |
| 108 | + " 'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),\n", |
| 109 | + " 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),\n", |
| 110 | + " #'h3': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),\n", |
| 111 | + " 'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))\n", |
| 112 | + "}\n", |
| 113 | + "biases = {\n", |
| 114 | + " 'b1': tf.Variable(tf.random_normal([n_hidden_1])),\n", |
| 115 | + " 'b2': tf.Variable(tf.random_normal([n_hidden_2])),\n", |
| 116 | + " #'b3': tf.Variable(tf.random_normal([n_hidden_2])),\n", |
| 117 | + " 'out': tf.Variable(tf.random_normal([n_classes]))\n", |
| 118 | + "}\n", |
| 119 | + "\n", |
| 120 | + "# Construct model\n", |
| 121 | + "pred = multilayer_perceptron(x, weights, biases)\n", |
| 122 | + "\n", |
| 123 | + "# Define loss and optimizer\n", |
| 124 | + "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))\n", |
| 125 | + "optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)\n", |
| 126 | + "\n", |
| 127 | + "# Initializing the variables\n", |
| 128 | + "init = tf.global_variables_initializer()\n", |
| 129 | + "\n", |
| 130 | + "# Launch the graph\n", |
| 131 | + "with tf.Session() as sess:\n", |
| 132 | + " sess.run(init)\n", |
| 133 | + "\n", |
| 134 | + " # Training cycle\n", |
| 135 | + " for epoch in range(training_epochs):\n", |
| 136 | + " avg_cost = 0.\n", |
| 137 | + " total_batch = int(mnist.train.num_examples/batch_size)\n", |
| 138 | + " # Loop over all batches\n", |
| 139 | + " for i in range(total_batch):\n", |
| 140 | + " batch_x, batch_y = mnist.train.next_batch(batch_size)\n", |
| 141 | + " # Run optimization op (backprop) and cost op (to get loss value)\n", |
| 142 | + " _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,\n", |
| 143 | + " y: batch_y})\n", |
| 144 | + " # Compute average loss\n", |
| 145 | + " avg_cost += c / total_batch\n", |
| 146 | + " # Display logs per epoch step\n", |
| 147 | + " if epoch % display_step == 0:\n", |
| 148 | + " print(\"Epoch:\", '%04d' % (epoch+1), \"cost=\", \\\n", |
| 149 | + " \"{:.9f}\".format(avg_cost))\n", |
| 150 | + " print(\"Optimization Finished!\")\n", |
| 151 | + "\n", |
| 152 | + " # Test model\n", |
| 153 | + " correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n", |
| 154 | + " # Calculate accuracy\n", |
| 155 | + " accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n", |
| 156 | + " print(\"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))\n" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": null, |
| 162 | + "metadata": { |
| 163 | + "collapsed": true |
| 164 | + }, |
| 165 | + "outputs": [], |
| 166 | + "source": [] |
| 167 | + } |
| 168 | + ], |
| 169 | + "metadata": { |
| 170 | + "kernelspec": { |
| 171 | + "display_name": "Python 2", |
| 172 | + "language": "python", |
| 173 | + "name": "python2" |
| 174 | + }, |
| 175 | + "language_info": { |
| 176 | + "codemirror_mode": { |
| 177 | + "name": "ipython", |
| 178 | + "version": 2 |
| 179 | + }, |
| 180 | + "file_extension": ".py", |
| 181 | + "mimetype": "text/x-python", |
| 182 | + "name": "python", |
| 183 | + "nbconvert_exporter": "python", |
| 184 | + "pygments_lexer": "ipython2", |
| 185 | + "version": "2.7.12" |
| 186 | + } |
| 187 | + }, |
| 188 | + "nbformat": 4, |
| 189 | + "nbformat_minor": 2 |
| 190 | +} |
0 commit comments