|
@@ -6,44 +6,63 @@
|
|
"collapsed": true
|
|
"collapsed": true
|
|
},
|
|
},
|
|
"source": [
|
|
"source": [
|
|
- "'''\n",
|
|
|
|
- "A 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",
|
|
|
|
|
|
+ "# Recurrent Neural Network Example\n",
|
|
"\n",
|
|
"\n",
|
|
- "Author: Aymeric Damien\n",
|
|
|
|
- "Project: https://github.com/aymericdamien/TensorFlow-Examples/\n",
|
|
|
|
- "'''"
|
|
|
|
|
|
+ "Build a recurrent neural network (LSTM) with TensorFlow.\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "- Author: Aymeric Damien\n",
|
|
|
|
+ "- Project: https://github.com/aymericdamien/TensorFlow-Examples/"
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "cell_type": "markdown",
|
|
|
|
+ "metadata": {},
|
|
|
|
+ "source": [
|
|
|
|
+ "## RNN Overview\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "<img src=\"http://colah.github.io/posts/2015-08-Understanding-LSTMs/img/RNN-unrolled.png\" alt=\"nn\" style=\"width: 600px;\"/>\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "References:\n",
|
|
|
|
+ "- [Long Short Term Memory](http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf), Sepp Hochreiter & Jurgen Schmidhuber, Neural Computation 9(8): 1735-1780, 1997.\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "## MNIST Dataset Overview\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "This example is using MNIST handwritten digits. The dataset contains 60,000 examples for training and 10,000 examples for testing. The digits have been size-normalized and centered in a fixed-size image (28x28 pixels) with values from 0 to 1. For simplicity, each image has been flatten and converted to a 1-D numpy array of 784 features (28*28).\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "To classify images using a recurrent neural network, we consider every image row as a sequence of pixels. Because MNIST image shape is 28*28px, we will then handle 28 sequences of 28 timesteps for every sample.\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "More info: http://yann.lecun.com/exdb/mnist/"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": null,
|
|
|
|
|
|
+ "execution_count": 1,
|
|
"metadata": {
|
|
"metadata": {
|
|
"collapsed": false
|
|
"collapsed": false
|
|
},
|
|
},
|
|
- "outputs": [],
|
|
|
|
|
|
+ "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": [
|
|
"source": [
|
|
|
|
+ "from __future__ import print_function\n",
|
|
|
|
+ "\n",
|
|
"import tensorflow as tf\n",
|
|
"import tensorflow as tf\n",
|
|
"from tensorflow.contrib import rnn\n",
|
|
"from tensorflow.contrib import rnn\n",
|
|
- "import numpy as np\n",
|
|
|
|
"\n",
|
|
"\n",
|
|
- "# Import MINST data\n",
|
|
|
|
|
|
+ "# Import MNIST data\n",
|
|
"from tensorflow.examples.tutorials.mnist import input_data\n",
|
|
"from tensorflow.examples.tutorials.mnist import input_data\n",
|
|
- "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)"
|
|
|
|
- ]
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- "cell_type": "markdown",
|
|
|
|
- "metadata": {
|
|
|
|
- "collapsed": true
|
|
|
|
- },
|
|
|
|
- "source": [
|
|
|
|
- "'''\n",
|
|
|
|
- "To classify images using a reccurent neural network, we consider every image\n",
|
|
|
|
- "row as a sequence of pixels. Because MNIST image shape is 28*28px, we will then\n",
|
|
|
|
- "handle 28 sequences of 28 steps for every sample.\n",
|
|
|
|
- "'''"
|
|
|
|
|
|
+ "mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
@@ -54,34 +73,43 @@
|
|
},
|
|
},
|
|
"outputs": [],
|
|
"outputs": [],
|
|
"source": [
|
|
"source": [
|
|
- "# Parameters\n",
|
|
|
|
|
|
+ "# Training Parameters\n",
|
|
"learning_rate = 0.001\n",
|
|
"learning_rate = 0.001\n",
|
|
- "training_iters = 100000\n",
|
|
|
|
|
|
+ "training_steps = 10000\n",
|
|
"batch_size = 128\n",
|
|
"batch_size = 128\n",
|
|
- "display_step = 10\n",
|
|
|
|
|
|
+ "display_step = 200\n",
|
|
"\n",
|
|
"\n",
|
|
"# Network Parameters\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",
|
|
|
|
|
|
+ "num_input = 28 # MNIST data input (img shape: 28*28)\n",
|
|
|
|
+ "timesteps = 28 # timesteps\n",
|
|
|
|
+ "num_hidden = 128 # hidden layer num of features\n",
|
|
|
|
+ "num_classes = 10 # MNIST total classes (0-9 digits)\n",
|
|
"\n",
|
|
"\n",
|
|
"# tf Graph input\n",
|
|
"# tf Graph input\n",
|
|
- "x = tf.placeholder(\"float\", [None, n_steps, n_input])\n",
|
|
|
|
- "y = tf.placeholder(\"float\", [None, n_classes])\n",
|
|
|
|
- "\n",
|
|
|
|
|
|
+ "X = tf.placeholder(\"float\", [None, timesteps, num_input])\n",
|
|
|
|
+ "Y = tf.placeholder(\"float\", [None, num_classes])"
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "cell_type": "code",
|
|
|
|
+ "execution_count": 3,
|
|
|
|
+ "metadata": {
|
|
|
|
+ "collapsed": true
|
|
|
|
+ },
|
|
|
|
+ "outputs": [],
|
|
|
|
+ "source": [
|
|
"# Define weights\n",
|
|
"# Define weights\n",
|
|
"weights = {\n",
|
|
"weights = {\n",
|
|
- " 'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))\n",
|
|
|
|
|
|
+ " 'out': tf.Variable(tf.random_normal([num_hidden, num_classes]))\n",
|
|
"}\n",
|
|
"}\n",
|
|
"biases = {\n",
|
|
"biases = {\n",
|
|
- " 'out': tf.Variable(tf.random_normal([n_classes]))\n",
|
|
|
|
|
|
+ " 'out': tf.Variable(tf.random_normal([num_classes]))\n",
|
|
"}"
|
|
"}"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 3,
|
|
|
|
|
|
+ "execution_count": 4,
|
|
"metadata": {
|
|
"metadata": {
|
|
"collapsed": false
|
|
"collapsed": false
|
|
},
|
|
},
|
|
@@ -90,38 +118,50 @@
|
|
"def RNN(x, weights, biases):\n",
|
|
"def RNN(x, weights, biases):\n",
|
|
"\n",
|
|
"\n",
|
|
" # Prepare data shape to match `rnn` function requirements\n",
|
|
" # Prepare data shape to match `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",
|
|
|
|
- " # Unstack to get a list of 'n_steps' tensors of shape (batch_size, n_input)\n",
|
|
|
|
- " x = tf.unstack(x, n_steps, 1)\n",
|
|
|
|
|
|
+ " # Current data input shape: (batch_size, timesteps, n_input)\n",
|
|
|
|
+ " # Required shape: 'timesteps' tensors list of shape (batch_size, n_input)\n",
|
|
|
|
+ "\n",
|
|
|
|
+ " # Unstack to get a list of 'timesteps' tensors of shape (batch_size, n_input)\n",
|
|
|
|
+ " x = tf.unstack(x, timesteps, 1)\n",
|
|
"\n",
|
|
"\n",
|
|
" # Define a lstm cell with tensorflow\n",
|
|
" # Define a lstm cell with tensorflow\n",
|
|
- " lstm_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)\n",
|
|
|
|
|
|
+ " lstm_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0)\n",
|
|
"\n",
|
|
"\n",
|
|
" # Get lstm cell output\n",
|
|
" # Get lstm cell output\n",
|
|
" outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)\n",
|
|
" outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)\n",
|
|
"\n",
|
|
"\n",
|
|
" # Linear activation, using rnn inner loop last output\n",
|
|
" # Linear activation, using rnn inner loop last output\n",
|
|
- " return tf.matmul(outputs[-1], weights['out']) + biases['out']\n",
|
|
|
|
- "\n",
|
|
|
|
- "pred = RNN(x, weights, biases)\n",
|
|
|
|
|
|
+ " return tf.matmul(outputs[-1], weights['out']) + biases['out']"
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "cell_type": "code",
|
|
|
|
+ "execution_count": 5,
|
|
|
|
+ "metadata": {
|
|
|
|
+ "collapsed": true
|
|
|
|
+ },
|
|
|
|
+ "outputs": [],
|
|
|
|
+ "source": [
|
|
|
|
+ "logits = RNN(X, weights, biases)\n",
|
|
|
|
+ "prediction = tf.nn.softmax(logits)\n",
|
|
"\n",
|
|
"\n",
|
|
"# Define loss and optimizer\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",
|
|
|
|
|
|
+ "loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(\n",
|
|
|
|
+ " logits=logits, labels=Y))\n",
|
|
|
|
+ "optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\n",
|
|
|
|
+ "train_op = optimizer.minimize(loss_op)\n",
|
|
"\n",
|
|
"\n",
|
|
- "# Evaluate model\n",
|
|
|
|
- "correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))\n",
|
|
|
|
|
|
+ "# Evaluate model (with test logits, for dropout to be disabled)\n",
|
|
|
|
+ "correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))\n",
|
|
"accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))\n",
|
|
"accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))\n",
|
|
"\n",
|
|
"\n",
|
|
- "# Initializing the variables\n",
|
|
|
|
|
|
+ "# Initialize the variables (i.e. assign their default value)\n",
|
|
"init = tf.global_variables_initializer()"
|
|
"init = tf.global_variables_initializer()"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 4,
|
|
|
|
|
|
+ "execution_count": 6,
|
|
"metadata": {
|
|
"metadata": {
|
|
"collapsed": false
|
|
"collapsed": false
|
|
},
|
|
},
|
|
@@ -130,118 +170,91 @@
|
|
"name": "stdout",
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"text": [
|
|
- "Iter 1280, Minibatch Loss= 1.576423, Training Accuracy= 0.51562\n",
|
|
|
|
- "Iter 2560, Minibatch Loss= 1.450179, Training Accuracy= 0.53906\n",
|
|
|
|
- "Iter 3840, Minibatch Loss= 1.160066, Training Accuracy= 0.64844\n",
|
|
|
|
- "Iter 5120, Minibatch Loss= 0.898589, Training Accuracy= 0.73438\n",
|
|
|
|
- "Iter 6400, Minibatch Loss= 0.685712, Training Accuracy= 0.75781\n",
|
|
|
|
- "Iter 7680, Minibatch Loss= 1.085666, Training Accuracy= 0.64844\n",
|
|
|
|
- "Iter 8960, Minibatch Loss= 0.681488, Training Accuracy= 0.73438\n",
|
|
|
|
- "Iter 10240, Minibatch Loss= 0.557049, Training Accuracy= 0.82812\n",
|
|
|
|
- "Iter 11520, Minibatch Loss= 0.340857, Training Accuracy= 0.92188\n",
|
|
|
|
- "Iter 12800, Minibatch Loss= 0.596482, Training Accuracy= 0.78906\n",
|
|
|
|
- "Iter 14080, Minibatch Loss= 0.486564, Training Accuracy= 0.84375\n",
|
|
|
|
- "Iter 15360, Minibatch Loss= 0.302493, Training Accuracy= 0.90625\n",
|
|
|
|
- "Iter 16640, Minibatch Loss= 0.334277, Training Accuracy= 0.92188\n",
|
|
|
|
- "Iter 17920, Minibatch Loss= 0.222026, Training Accuracy= 0.90625\n",
|
|
|
|
- "Iter 19200, Minibatch Loss= 0.228581, Training Accuracy= 0.92188\n",
|
|
|
|
- "Iter 20480, Minibatch Loss= 0.150356, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 21760, Minibatch Loss= 0.415417, Training Accuracy= 0.86719\n",
|
|
|
|
- "Iter 23040, Minibatch Loss= 0.159742, Training Accuracy= 0.94531\n",
|
|
|
|
- "Iter 24320, Minibatch Loss= 0.333764, Training Accuracy= 0.89844\n",
|
|
|
|
- "Iter 25600, Minibatch Loss= 0.379070, Training Accuracy= 0.88281\n",
|
|
|
|
- "Iter 26880, Minibatch Loss= 0.241612, Training Accuracy= 0.91406\n",
|
|
|
|
- "Iter 28160, Minibatch Loss= 0.200397, Training Accuracy= 0.93750\n",
|
|
|
|
- "Iter 29440, Minibatch Loss= 0.197994, Training Accuracy= 0.93750\n",
|
|
|
|
- "Iter 30720, Minibatch Loss= 0.330214, Training Accuracy= 0.89062\n",
|
|
|
|
- "Iter 32000, Minibatch Loss= 0.174626, Training Accuracy= 0.92969\n",
|
|
|
|
- "Iter 33280, Minibatch Loss= 0.202369, Training Accuracy= 0.93750\n",
|
|
|
|
- "Iter 34560, Minibatch Loss= 0.240835, Training Accuracy= 0.94531\n",
|
|
|
|
- "Iter 35840, Minibatch Loss= 0.207867, Training Accuracy= 0.93750\n",
|
|
|
|
- "Iter 37120, Minibatch Loss= 0.313306, Training Accuracy= 0.90625\n",
|
|
|
|
- "Iter 38400, Minibatch Loss= 0.089850, Training Accuracy= 0.96875\n",
|
|
|
|
- "Iter 39680, Minibatch Loss= 0.184803, Training Accuracy= 0.92188\n",
|
|
|
|
- "Iter 40960, Minibatch Loss= 0.236523, Training Accuracy= 0.92969\n",
|
|
|
|
- "Iter 42240, Minibatch Loss= 0.174834, Training Accuracy= 0.94531\n",
|
|
|
|
- "Iter 43520, Minibatch Loss= 0.127905, Training Accuracy= 0.93750\n",
|
|
|
|
- "Iter 44800, Minibatch Loss= 0.120045, Training Accuracy= 0.96875\n",
|
|
|
|
- "Iter 46080, Minibatch Loss= 0.068337, Training Accuracy= 0.98438\n",
|
|
|
|
- "Iter 47360, Minibatch Loss= 0.141118, Training Accuracy= 0.95312\n",
|
|
|
|
- "Iter 48640, Minibatch Loss= 0.182404, Training Accuracy= 0.92188\n",
|
|
|
|
- "Iter 49920, Minibatch Loss= 0.176778, Training Accuracy= 0.93750\n",
|
|
|
|
- "Iter 51200, Minibatch Loss= 0.098927, Training Accuracy= 0.97656\n",
|
|
|
|
- "Iter 52480, Minibatch Loss= 0.158776, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 53760, Minibatch Loss= 0.031863, Training Accuracy= 0.99219\n",
|
|
|
|
- "Iter 55040, Minibatch Loss= 0.101799, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 56320, Minibatch Loss= 0.176387, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 57600, Minibatch Loss= 0.096277, Training Accuracy= 0.96875\n",
|
|
|
|
- "Iter 58880, Minibatch Loss= 0.137416, Training Accuracy= 0.94531\n",
|
|
|
|
- "Iter 60160, Minibatch Loss= 0.062801, Training Accuracy= 0.97656\n",
|
|
|
|
- "Iter 61440, Minibatch Loss= 0.036346, Training Accuracy= 0.98438\n",
|
|
|
|
- "Iter 62720, Minibatch Loss= 0.153030, Training Accuracy= 0.92969\n",
|
|
|
|
- "Iter 64000, Minibatch Loss= 0.117716, Training Accuracy= 0.95312\n",
|
|
|
|
- "Iter 65280, Minibatch Loss= 0.048387, Training Accuracy= 0.99219\n",
|
|
|
|
- "Iter 66560, Minibatch Loss= 0.070802, Training Accuracy= 0.97656\n",
|
|
|
|
- "Iter 67840, Minibatch Loss= 0.221085, Training Accuracy= 0.96875\n",
|
|
|
|
- "Iter 69120, Minibatch Loss= 0.184049, Training Accuracy= 0.93750\n",
|
|
|
|
- "Iter 70400, Minibatch Loss= 0.094883, Training Accuracy= 0.95312\n",
|
|
|
|
- "Iter 71680, Minibatch Loss= 0.087278, Training Accuracy= 0.96875\n",
|
|
|
|
- "Iter 72960, Minibatch Loss= 0.153267, Training Accuracy= 0.95312\n",
|
|
|
|
- "Iter 74240, Minibatch Loss= 0.161794, Training Accuracy= 0.94531\n",
|
|
|
|
- "Iter 75520, Minibatch Loss= 0.103779, Training Accuracy= 0.96875\n",
|
|
|
|
- "Iter 76800, Minibatch Loss= 0.165586, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 78080, Minibatch Loss= 0.137721, Training Accuracy= 0.95312\n",
|
|
|
|
- "Iter 79360, Minibatch Loss= 0.124014, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 80640, Minibatch Loss= 0.051460, Training Accuracy= 0.99219\n",
|
|
|
|
- "Iter 81920, Minibatch Loss= 0.185836, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 83200, Minibatch Loss= 0.147694, Training Accuracy= 0.94531\n",
|
|
|
|
- "Iter 84480, Minibatch Loss= 0.061550, Training Accuracy= 0.98438\n",
|
|
|
|
- "Iter 85760, Minibatch Loss= 0.093457, Training Accuracy= 0.96875\n",
|
|
|
|
- "Iter 87040, Minibatch Loss= 0.094497, Training Accuracy= 0.98438\n",
|
|
|
|
- "Iter 88320, Minibatch Loss= 0.093934, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 89600, Minibatch Loss= 0.061550, Training Accuracy= 0.96875\n",
|
|
|
|
- "Iter 90880, Minibatch Loss= 0.082452, Training Accuracy= 0.97656\n",
|
|
|
|
- "Iter 92160, Minibatch Loss= 0.087423, Training Accuracy= 0.97656\n",
|
|
|
|
- "Iter 93440, Minibatch Loss= 0.032694, Training Accuracy= 0.99219\n",
|
|
|
|
- "Iter 94720, Minibatch Loss= 0.069597, Training Accuracy= 0.97656\n",
|
|
|
|
- "Iter 96000, Minibatch Loss= 0.193636, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 97280, Minibatch Loss= 0.134405, Training Accuracy= 0.96094\n",
|
|
|
|
- "Iter 98560, Minibatch Loss= 0.072992, Training Accuracy= 0.96875\n",
|
|
|
|
- "Iter 99840, Minibatch Loss= 0.041049, Training Accuracy= 0.99219\n",
|
|
|
|
|
|
+ "Step 1, Minibatch Loss= 2.6268, Training Accuracy= 0.102\n",
|
|
|
|
+ "Step 200, Minibatch Loss= 2.0722, Training Accuracy= 0.328\n",
|
|
|
|
+ "Step 400, Minibatch Loss= 1.9181, Training Accuracy= 0.336\n",
|
|
|
|
+ "Step 600, Minibatch Loss= 1.8858, Training Accuracy= 0.336\n",
|
|
|
|
+ "Step 800, Minibatch Loss= 1.7022, Training Accuracy= 0.422\n",
|
|
|
|
+ "Step 1000, Minibatch Loss= 1.6365, Training Accuracy= 0.477\n",
|
|
|
|
+ "Step 1200, Minibatch Loss= 1.6691, Training Accuracy= 0.516\n",
|
|
|
|
+ "Step 1400, Minibatch Loss= 1.4626, Training Accuracy= 0.547\n",
|
|
|
|
+ "Step 1600, Minibatch Loss= 1.4707, Training Accuracy= 0.539\n",
|
|
|
|
+ "Step 1800, Minibatch Loss= 1.4087, Training Accuracy= 0.570\n",
|
|
|
|
+ "Step 2000, Minibatch Loss= 1.3033, Training Accuracy= 0.570\n",
|
|
|
|
+ "Step 2200, Minibatch Loss= 1.3773, Training Accuracy= 0.508\n",
|
|
|
|
+ "Step 2400, Minibatch Loss= 1.3092, Training Accuracy= 0.570\n",
|
|
|
|
+ "Step 2600, Minibatch Loss= 1.2272, Training Accuracy= 0.609\n",
|
|
|
|
+ "Step 2800, Minibatch Loss= 1.1827, Training Accuracy= 0.633\n",
|
|
|
|
+ "Step 3000, Minibatch Loss= 1.0453, Training Accuracy= 0.641\n",
|
|
|
|
+ "Step 3200, Minibatch Loss= 1.0400, Training Accuracy= 0.648\n",
|
|
|
|
+ "Step 3400, Minibatch Loss= 1.1145, Training Accuracy= 0.656\n",
|
|
|
|
+ "Step 3600, Minibatch Loss= 0.9884, Training Accuracy= 0.688\n",
|
|
|
|
+ "Step 3800, Minibatch Loss= 1.0395, Training Accuracy= 0.703\n",
|
|
|
|
+ "Step 4000, Minibatch Loss= 1.0096, Training Accuracy= 0.664\n",
|
|
|
|
+ "Step 4200, Minibatch Loss= 0.8806, Training Accuracy= 0.758\n",
|
|
|
|
+ "Step 4400, Minibatch Loss= 0.9090, Training Accuracy= 0.766\n",
|
|
|
|
+ "Step 4600, Minibatch Loss= 1.0060, Training Accuracy= 0.703\n",
|
|
|
|
+ "Step 4800, Minibatch Loss= 0.8954, Training Accuracy= 0.703\n",
|
|
|
|
+ "Step 5000, Minibatch Loss= 0.8163, Training Accuracy= 0.750\n",
|
|
|
|
+ "Step 5200, Minibatch Loss= 0.7620, Training Accuracy= 0.773\n",
|
|
|
|
+ "Step 5400, Minibatch Loss= 0.7388, Training Accuracy= 0.758\n",
|
|
|
|
+ "Step 5600, Minibatch Loss= 0.7604, Training Accuracy= 0.695\n",
|
|
|
|
+ "Step 5800, Minibatch Loss= 0.7459, Training Accuracy= 0.734\n",
|
|
|
|
+ "Step 6000, Minibatch Loss= 0.7448, Training Accuracy= 0.734\n",
|
|
|
|
+ "Step 6200, Minibatch Loss= 0.7208, Training Accuracy= 0.773\n",
|
|
|
|
+ "Step 6400, Minibatch Loss= 0.6557, Training Accuracy= 0.773\n",
|
|
|
|
+ "Step 6600, Minibatch Loss= 0.8616, Training Accuracy= 0.758\n",
|
|
|
|
+ "Step 6800, Minibatch Loss= 0.6089, Training Accuracy= 0.773\n",
|
|
|
|
+ "Step 7000, Minibatch Loss= 0.5020, Training Accuracy= 0.844\n",
|
|
|
|
+ "Step 7200, Minibatch Loss= 0.5980, Training Accuracy= 0.812\n",
|
|
|
|
+ "Step 7400, Minibatch Loss= 0.6786, Training Accuracy= 0.766\n",
|
|
|
|
+ "Step 7600, Minibatch Loss= 0.4891, Training Accuracy= 0.859\n",
|
|
|
|
+ "Step 7800, Minibatch Loss= 0.7042, Training Accuracy= 0.797\n",
|
|
|
|
+ "Step 8000, Minibatch Loss= 0.4200, Training Accuracy= 0.859\n",
|
|
|
|
+ "Step 8200, Minibatch Loss= 0.6442, Training Accuracy= 0.742\n",
|
|
|
|
+ "Step 8400, Minibatch Loss= 0.5569, Training Accuracy= 0.828\n",
|
|
|
|
+ "Step 8600, Minibatch Loss= 0.5838, Training Accuracy= 0.836\n",
|
|
|
|
+ "Step 8800, Minibatch Loss= 0.5579, Training Accuracy= 0.812\n",
|
|
|
|
+ "Step 9000, Minibatch Loss= 0.4337, Training Accuracy= 0.867\n",
|
|
|
|
+ "Step 9200, Minibatch Loss= 0.4366, Training Accuracy= 0.844\n",
|
|
|
|
+ "Step 9400, Minibatch Loss= 0.5051, Training Accuracy= 0.844\n",
|
|
|
|
+ "Step 9600, Minibatch Loss= 0.5244, Training Accuracy= 0.805\n",
|
|
|
|
+ "Step 9800, Minibatch Loss= 0.4932, Training Accuracy= 0.805\n",
|
|
|
|
+ "Step 10000, Minibatch Loss= 0.4833, Training Accuracy= 0.852\n",
|
|
"Optimization Finished!\n",
|
|
"Optimization Finished!\n",
|
|
- "Testing Accuracy: 0.960938\n"
|
|
|
|
|
|
+ "Testing Accuracy: 0.882812\n"
|
|
]
|
|
]
|
|
}
|
|
}
|
|
],
|
|
],
|
|
"source": [
|
|
"source": [
|
|
- "# Launch the graph\n",
|
|
|
|
|
|
+ "# Start training\n",
|
|
"with tf.Session() as sess:\n",
|
|
"with tf.Session() as sess:\n",
|
|
|
|
+ "\n",
|
|
|
|
+ " # Run the initializer\n",
|
|
" sess.run(init)\n",
|
|
" sess.run(init)\n",
|
|
- " step = 1\n",
|
|
|
|
- " # Keep training until reach max iterations\n",
|
|
|
|
- " while step * batch_size < training_iters:\n",
|
|
|
|
|
|
+ "\n",
|
|
|
|
+ " for step in range(1, training_steps+1):\n",
|
|
" batch_x, batch_y = mnist.train.next_batch(batch_size)\n",
|
|
" batch_x, batch_y = mnist.train.next_batch(batch_size)\n",
|
|
" # Reshape data to get 28 seq of 28 elements\n",
|
|
" # Reshape data to get 28 seq of 28 elements\n",
|
|
- " batch_x = batch_x.reshape((batch_size, n_steps, n_input))\n",
|
|
|
|
|
|
+ " batch_x = batch_x.reshape((batch_size, timesteps, num_input))\n",
|
|
" # Run optimization op (backprop)\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",
|
|
|
|
|
|
+ " sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})\n",
|
|
|
|
+ " if step % display_step == 0 or step == 1:\n",
|
|
|
|
+ " # Calculate batch loss and accuracy\n",
|
|
|
|
+ " loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,\n",
|
|
|
|
+ " Y: batch_y})\n",
|
|
|
|
+ " print(\"Step \" + str(step) + \", Minibatch Loss= \" + \\\n",
|
|
|
|
+ " \"{:.4f}\".format(loss) + \", Training Accuracy= \" + \\\n",
|
|
|
|
+ " \"{:.3f}\".format(acc))\n",
|
|
|
|
+ "\n",
|
|
|
|
+ " print(\"Optimization Finished!\")\n",
|
|
"\n",
|
|
"\n",
|
|
" # Calculate accuracy for 128 mnist test images\n",
|
|
" # Calculate accuracy for 128 mnist test images\n",
|
|
" test_len = 128\n",
|
|
" test_len = 128\n",
|
|
- " test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))\n",
|
|
|
|
|
|
+ " test_data = mnist.test.images[:test_len].reshape((-1, timesteps, num_input))\n",
|
|
" test_label = mnist.test.labels[:test_len]\n",
|
|
" test_label = mnist.test.labels[:test_len]\n",
|
|
- " print \"Testing Accuracy:\", \\\n",
|
|
|
|
- " sess.run(accuracy, feed_dict={x: test_data, y: test_label})"
|
|
|
|
|
|
+ " print(\"Testing Accuracy:\", \\\n",
|
|
|
|
+ " sess.run(accuracy, feed_dict={X: test_data, Y: test_label}))"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
@@ -255,8 +268,9 @@
|
|
}
|
|
}
|
|
],
|
|
],
|
|
"metadata": {
|
|
"metadata": {
|
|
|
|
+ "anaconda-cloud": {},
|
|
"kernelspec": {
|
|
"kernelspec": {
|
|
- "display_name": "Python 2",
|
|
|
|
|
|
+ "display_name": "Python [default]",
|
|
"language": "python",
|
|
"language": "python",
|
|
"name": "python2"
|
|
"name": "python2"
|
|
},
|
|
},
|
|
@@ -270,7 +284,7 @@
|
|
"name": "python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"pygments_lexer": "ipython2",
|
|
- "version": "2.7.13"
|
|
|
|
|
|
+ "version": "2.7.12"
|
|
}
|
|
}
|
|
},
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat": 4,
|