{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''\n", "A Bidirectional Reccurent Neural Network (LSTM) implementation example using TensorFlow library.\n", "This example is using the MNIST database of handwritten digits (http://yann.lecun.com/exdb/mnist/)\n", "Long Short Term Memory paper: http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf\n", "\n", "Author: Aymeric Damien\n", "Project: https://github.com/aymericdamien/TensorFlow-Examples/\n", "'''" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting /tmp/data/train-images-idx3-ubyte.gz\n", "Extracting /tmp/data/train-labels-idx1-ubyte.gz\n", "Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n", "Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n" ] } ], "source": [ "import tensorflow as tf\n", "from tensorflow.models.rnn import rnn, rnn_cell\n", "import numpy as np\n", "\n", "# Import MINST data\n", "from tensorflow.examples.tutorials.mnist import input_data\n", "mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''\n", "To classify images using a bidirectional reccurent neural network, we consider\n", "every image row as a sequence of pixels. Because MNIST image shape is 28*28px,\n", "we will then handle 28 sequences of 28 steps for every sample.\n", "'''" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Parameters\n", "learning_rate = 0.001\n", "training_iters = 100000\n", "batch_size = 128\n", "display_step = 10\n", "\n", "# Network Parameters\n", "n_input = 28 # MNIST data input (img shape: 28*28)\n", "n_steps = 28 # timesteps\n", "n_hidden = 128 # hidden layer num of features\n", "n_classes = 10 # MNIST total classes (0-9 digits)\n", "\n", "# tf Graph input\n", "x = tf.placeholder(\"float\", [None, n_steps, n_input])\n", "y = tf.placeholder(\"float\", [None, n_classes])\n", "\n", "# Define weights\n", "weights = {\n", " # Hidden layer weights => 2*n_hidden because of foward + backward cells\n", " 'out': tf.Variable(tf.random_normal([2*n_hidden, n_classes]))\n", "}\n", "biases = {\n", " 'out': tf.Variable(tf.random_normal([n_classes]))\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def BiRNN(x, weights, biases):\n", "\n", " # Prepare data shape to match `bidirectional_rnn` function requirements\n", " # Current data input shape: (batch_size, n_steps, n_input)\n", " # Required shape: 'n_steps' tensors list of shape (batch_size, n_input)\n", " \n", " # Permuting batch_size and n_steps\n", " x = tf.transpose(x, [1, 0, 2])\n", " # Reshape to (n_steps*batch_size, n_input)\n", " x = tf.reshape(x, [-1, n_input])\n", " # Split to get a list of 'n_steps' tensors of shape (batch_size, n_input)\n", " x = tf.split(0, n_steps, x)\n", "\n", " # Define lstm cells with tensorflow\n", " # Forward direction cell\n", " lstm_fw_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)\n", " # Backward direction cell\n", " lstm_bw_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)\n", "\n", " # Get lstm cell output\n", " try:\n", " outputs, _, _ = rnn.bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,\n", " dtype=tf.float32)\n", " except Exception: # Old TensorFlow version only returns outputs not states\n", " outputs = rnn.bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,\n", " dtype=tf.float32)\n", "\n", " # Linear activation, using rnn inner loop last output\n", " return tf.matmul(outputs[-1], weights['out']) + biases['out']\n", "\n", "pred = BiRNN(x, weights, biases)\n", "\n", "# Define loss and optimizer\n", "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, 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": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Iter 1280, Minibatch Loss= 1.689740, Training Accuracy= 0.36719\n", "Iter 2560, Minibatch Loss= 1.477009, Training Accuracy= 0.44531\n", "Iter 3840, Minibatch Loss= 1.245874, Training Accuracy= 0.53125\n", "Iter 5120, Minibatch Loss= 0.990923, Training Accuracy= 0.64062\n", "Iter 6400, Minibatch Loss= 0.752950, Training Accuracy= 0.71875\n", "Iter 7680, Minibatch Loss= 1.023025, Training Accuracy= 0.61719\n", "Iter 8960, Minibatch Loss= 0.921414, Training Accuracy= 0.68750\n", "Iter 10240, Minibatch Loss= 0.719829, Training Accuracy= 0.75000\n", "Iter 11520, Minibatch Loss= 0.468657, Training Accuracy= 0.86719\n", "Iter 12800, Minibatch Loss= 0.654315, Training Accuracy= 0.78125\n", "Iter 14080, Minibatch Loss= 0.595391, Training Accuracy= 0.83594\n", "Iter 15360, Minibatch Loss= 0.392862, Training Accuracy= 0.83594\n", "Iter 16640, Minibatch Loss= 0.421122, Training Accuracy= 0.92188\n", "Iter 17920, Minibatch Loss= 0.311471, Training Accuracy= 0.88281\n", "Iter 19200, Minibatch Loss= 0.276949, Training Accuracy= 0.92188\n", "Iter 20480, Minibatch Loss= 0.170499, Training Accuracy= 0.94531\n", "Iter 21760, Minibatch Loss= 0.419481, Training Accuracy= 0.86719\n", "Iter 23040, Minibatch Loss= 0.183765, Training Accuracy= 0.92188\n", "Iter 24320, Minibatch Loss= 0.386232, Training Accuracy= 0.86719\n", "Iter 25600, Minibatch Loss= 0.335571, Training Accuracy= 0.88281\n", "Iter 26880, Minibatch Loss= 0.169092, Training Accuracy= 0.92969\n", "Iter 28160, Minibatch Loss= 0.247623, Training Accuracy= 0.92969\n", "Iter 29440, Minibatch Loss= 0.242989, Training Accuracy= 0.94531\n", "Iter 30720, Minibatch Loss= 0.253811, Training Accuracy= 0.92188\n", "Iter 32000, Minibatch Loss= 0.169660, Training Accuracy= 0.93750\n", "Iter 33280, Minibatch Loss= 0.291349, Training Accuracy= 0.90625\n", "Iter 34560, Minibatch Loss= 0.172026, Training Accuracy= 0.95312\n", "Iter 35840, Minibatch Loss= 0.186019, Training Accuracy= 0.93750\n", "Iter 37120, Minibatch Loss= 0.298480, Training Accuracy= 0.89062\n", "Iter 38400, Minibatch Loss= 0.158750, Training Accuracy= 0.92188\n", "Iter 39680, Minibatch Loss= 0.162706, Training Accuracy= 0.94531\n", "Iter 40960, Minibatch Loss= 0.339814, Training Accuracy= 0.86719\n", "Iter 42240, Minibatch Loss= 0.068817, Training Accuracy= 0.99219\n", "Iter 43520, Minibatch Loss= 0.188742, Training Accuracy= 0.93750\n", "Iter 44800, Minibatch Loss= 0.176708, Training Accuracy= 0.92969\n", "Iter 46080, Minibatch Loss= 0.096726, Training Accuracy= 0.96875\n", "Iter 47360, Minibatch Loss= 0.220973, Training Accuracy= 0.92969\n", "Iter 48640, Minibatch Loss= 0.226749, Training Accuracy= 0.94531\n", "Iter 49920, Minibatch Loss= 0.188906, Training Accuracy= 0.94531\n", "Iter 51200, Minibatch Loss= 0.145194, Training Accuracy= 0.95312\n", "Iter 52480, Minibatch Loss= 0.168948, Training Accuracy= 0.95312\n", "Iter 53760, Minibatch Loss= 0.069116, Training Accuracy= 0.97656\n", "Iter 55040, Minibatch Loss= 0.228721, Training Accuracy= 0.93750\n", "Iter 56320, Minibatch Loss= 0.152915, Training Accuracy= 0.95312\n", "Iter 57600, Minibatch Loss= 0.126974, Training Accuracy= 0.96875\n", "Iter 58880, Minibatch Loss= 0.078870, Training Accuracy= 0.97656\n", "Iter 60160, Minibatch Loss= 0.225498, Training Accuracy= 0.95312\n", "Iter 61440, Minibatch Loss= 0.111760, Training Accuracy= 0.97656\n", "Iter 62720, Minibatch Loss= 0.161434, Training Accuracy= 0.97656\n", "Iter 64000, Minibatch Loss= 0.207190, Training Accuracy= 0.94531\n", "Iter 65280, Minibatch Loss= 0.103831, Training Accuracy= 0.96094\n", "Iter 66560, Minibatch Loss= 0.153846, Training Accuracy= 0.93750\n", "Iter 67840, Minibatch Loss= 0.082717, Training Accuracy= 0.96875\n", "Iter 69120, Minibatch Loss= 0.199301, Training Accuracy= 0.95312\n", "Iter 70400, Minibatch Loss= 0.139725, Training Accuracy= 0.96875\n", "Iter 71680, Minibatch Loss= 0.169596, Training Accuracy= 0.95312\n", "Iter 72960, Minibatch Loss= 0.142444, Training Accuracy= 0.96094\n", "Iter 74240, Minibatch Loss= 0.145822, Training Accuracy= 0.95312\n", "Iter 75520, Minibatch Loss= 0.129086, Training Accuracy= 0.94531\n", "Iter 76800, Minibatch Loss= 0.078082, Training Accuracy= 0.97656\n", "Iter 78080, Minibatch Loss= 0.151803, Training Accuracy= 0.94531\n", "Iter 79360, Minibatch Loss= 0.050142, Training Accuracy= 0.98438\n", "Iter 80640, Minibatch Loss= 0.136788, Training Accuracy= 0.95312\n", "Iter 81920, Minibatch Loss= 0.130100, Training Accuracy= 0.94531\n", "Iter 83200, Minibatch Loss= 0.058298, Training Accuracy= 0.98438\n", "Iter 84480, Minibatch Loss= 0.120124, Training Accuracy= 0.96094\n", "Iter 85760, Minibatch Loss= 0.064916, Training Accuracy= 0.97656\n", "Iter 87040, Minibatch Loss= 0.137179, Training Accuracy= 0.93750\n", "Iter 88320, Minibatch Loss= 0.138268, Training Accuracy= 0.95312\n", "Iter 89600, Minibatch Loss= 0.072827, Training Accuracy= 0.97656\n", "Iter 90880, Minibatch Loss= 0.123839, Training Accuracy= 0.96875\n", "Iter 92160, Minibatch Loss= 0.087194, Training Accuracy= 0.96875\n", "Iter 93440, Minibatch Loss= 0.083489, Training Accuracy= 0.97656\n", "Iter 94720, Minibatch Loss= 0.131827, Training Accuracy= 0.95312\n", "Iter 96000, Minibatch Loss= 0.098764, Training Accuracy= 0.96875\n", "Iter 97280, Minibatch Loss= 0.115553, Training Accuracy= 0.94531\n", "Iter 98560, Minibatch Loss= 0.079704, Training Accuracy= 0.96875\n", "Iter 99840, Minibatch Loss= 0.064562, Training Accuracy= 0.98438\n", "Optimization Finished!\n", "Testing Accuracy: 0.992188\n" ] } ], "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", " # Reshape data to get 28 seq of 28 elements\n", " batch_x = batch_x.reshape((batch_size, n_steps, n_input))\n", " # Run optimization op (backprop)\n", " sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})\n", " if step % display_step == 0:\n", " # Calculate batch accuracy\n", " acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})\n", " # Calculate batch loss\n", " loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})\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 128 mnist test images\n", " test_len = 128\n", " test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))\n", " test_label = mnist.test.labels[:test_len]\n", " print \"Testing Accuracy:\", \\\n", " sess.run(accuracy, feed_dict={x: test_data, y: test_label})" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2.0 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }