{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "'''\n", "A Convolutional Network implementation example using TensorFlow library.\n", "This example is using the MNIST database of handwritten digits\n", "(http://yann.lecun.com/exdb/mnist/)\n", "\n", "Author: Aymeric Damien\n", "Project: https://github.com/aymericdamien/TensorFlow-Examples/\n", "'''" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import tensorflow as tf\n", "\n", "# Import MNIST data\n", "from tensorflow.examples.tutorials.mnist import input_data\n", "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Parameters\n", "learning_rate = 0.001\n", "training_iters = 200000\n", "batch_size = 128\n", "display_step = 10\n", "\n", "# Network Parameters\n", "n_input = 784 # MNIST data input (img shape: 28*28)\n", "n_classes = 10 # MNIST total classes (0-9 digits)\n", "dropout = 0.75 # Dropout, probability to keep units\n", "\n", "# tf Graph input\n", "x = tf.placeholder(tf.float32, [None, n_input])\n", "y = tf.placeholder(tf.float32, [None, n_classes])\n", "keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create some wrappers for simplicity\n", "def conv2d(x, W, b, strides=1):\n", " # Conv2D wrapper, with bias and relu activation\n", " x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')\n", " x = tf.nn.bias_add(x, b)\n", " return tf.nn.relu(x)\n", "\n", "\n", "def maxpool2d(x, k=2):\n", " # MaxPool2D wrapper\n", " return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],\n", " padding='SAME')\n", "\n", "\n", "# Create model\n", "def conv_net(x, weights, biases, dropout):\n", " # Reshape input picture\n", " x = tf.reshape(x, shape=[-1, 28, 28, 1])\n", "\n", " # Convolution Layer\n", " conv1 = conv2d(x, weights['wc1'], biases['bc1'])\n", " # Max Pooling (down-sampling)\n", " conv1 = maxpool2d(conv1, k=2)\n", "\n", " # Convolution Layer\n", " conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])\n", " # Max Pooling (down-sampling)\n", " conv2 = maxpool2d(conv2, k=2)\n", "\n", " # Fully connected layer\n", " # Reshape conv2 output to fit fully connected layer input\n", " fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])\n", " fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])\n", " fc1 = tf.nn.relu(fc1)\n", " # Apply Dropout\n", " fc1 = tf.nn.dropout(fc1, dropout)\n", "\n", " # Output, class prediction\n", " out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])\n", " return out" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Store layers weight & bias\n", "weights = {\n", " # 5x5 conv, 1 input, 32 outputs\n", " 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),\n", " # 5x5 conv, 32 inputs, 64 outputs\n", " 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),\n", " # fully connected, 7*7*64 inputs, 1024 outputs\n", " 'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),\n", " # 1024 inputs, 10 outputs (class prediction)\n", " 'out': tf.Variable(tf.random_normal([1024, n_classes]))\n", "}\n", "\n", "biases = {\n", " 'bc1': tf.Variable(tf.random_normal([32])),\n", " 'bc2': tf.Variable(tf.random_normal([64])),\n", " 'bd1': tf.Variable(tf.random_normal([1024])),\n", " 'out': tf.Variable(tf.random_normal([n_classes]))\n", "}\n", "\n", "# Construct model\n", "pred = conv_net(x, weights, biases, keep_prob)\n", "\n", "# Define loss and optimizer\n", "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))\n", "optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)\n", "\n", "# Evaluate model\n", "correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n", "accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))\n", "\n", "# Initializing the variables\n", "init = tf.global_variables_initializer()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Launch the graph\n", "with tf.Session() as sess:\n", " sess.run(init)\n", " step = 1\n", " # Keep training until reach max iterations\n", " while step * batch_size < training_iters:\n", " batch_x, batch_y = mnist.train.next_batch(batch_size)\n", " # Run optimization op (backprop)\n", " sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,\n", " keep_prob: dropout})\n", " if step % display_step == 0:\n", " # Calculate batch loss and accuracy\n", " loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,\n", " y: batch_y,\n", " keep_prob: 1.})\n", " print \"Iter \" + str(step*batch_size) + \", Minibatch Loss= \" + \\\n", " \"{:.6f}\".format(loss) + \", Training Accuracy= \" + \\\n", " \"{:.5f}\".format(acc)\n", " step += 1\n", " print \"Optimization Finished!\"\n", "\n", " # Calculate accuracy for 256 mnist test images\n", " print \"Testing Accuracy:\", \\\n", " sess.run(accuracy, feed_dict={x: mnist.test.images[:256],\n", " y: mnist.test.labels[:256],\n", " keep_prob: 1.})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 0 }