|
1 |
| -under dev.. |
| 1 | +''' |
| 2 | +A Reccurent Neural Network (LSTM) implementation example using TensorFlow library. |
| 3 | +This example is using the MNIST database of handwritten digits (http://yann.lecun.com/exdb/mnist/) |
| 4 | +Long Short Term Memory paper: http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf |
| 5 | +
|
| 6 | +Author: Aymeric Damien |
| 7 | +Project: https://github.com/aymericdamien/TensorFlow-Examples/ |
| 8 | +''' |
| 9 | + |
| 10 | +# Import MINST data |
| 11 | +import input_data |
| 12 | +mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) |
| 13 | + |
| 14 | +import tensorflow as tf |
| 15 | +from tensorflow.models.rnn import rnn, rnn_cell |
| 16 | +import numpy as np |
| 17 | + |
| 18 | +# Parameters |
| 19 | +learning_rate = 0.001 |
| 20 | +training_iters = 100000 |
| 21 | +batch_size = 128 |
| 22 | +display_step = 10 |
| 23 | + |
| 24 | +# Network Parameters |
| 25 | +n_input = 28 # MNIST data input (img shape: 28*28) |
| 26 | +n_steps = 28 # timesteps |
| 27 | +n_hidden = 128 # hidden layer num of features |
| 28 | +n_classes = 10 # MNIST total classes (0-9 digits) |
| 29 | + |
| 30 | +# tf Graph input |
| 31 | +x = tf.placeholder("float", [None, n_steps, n_input]) |
| 32 | +istate = tf.placeholder("float", [None, 2*n_hidden]) #state & cell => 2x n_hidden |
| 33 | +y = tf.placeholder("float", [None, n_classes]) |
| 34 | + |
| 35 | +# Define weights |
| 36 | +weights = { |
| 37 | + 'hidden': tf.Variable(tf.random_normal([n_input, n_hidden])), # Hidden layer weights |
| 38 | + 'out': tf.Variable(tf.random_normal([n_hidden, n_classes])) |
| 39 | +} |
| 40 | +biases = { |
| 41 | + 'hidden': tf.Variable(tf.random_normal([n_hidden])), |
| 42 | + 'out': tf.Variable(tf.random_normal([n_classes])) |
| 43 | +} |
| 44 | + |
| 45 | +def RNN(_X, _istate, _weights, _biases): |
| 46 | + |
| 47 | + # input shape: (batch_size, n_steps, n_input) |
| 48 | + _X = tf.transpose(_X, [1, 0, 2]) # permute n_steps and batch_size |
| 49 | + # Reshape to prepare input to hidden activation |
| 50 | + _X = tf.reshape(_X, [-1, n_input]) # (n_steps*batch_size, n_input) |
| 51 | + # Linear activation |
| 52 | + _X = tf.matmul(_X, _weights['hidden']) + _biases['hidden'] |
| 53 | + |
| 54 | + # Define a lstm cell with tensorflow |
| 55 | + lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0) |
| 56 | + # Split data because rnn cell needs a list of inputs for the RNN inner loop |
| 57 | + _X = tf.split(0, n_steps, _X) # n_steps * (batch_size, n_hidden) |
| 58 | + |
| 59 | + # Get lstm cell output |
| 60 | + outputs, states = rnn.rnn(lstm_cell, _X, initial_state=_istate) |
| 61 | + |
| 62 | + # Linear activation |
| 63 | + # Get inner loop last output |
| 64 | + return tf.matmul(outputs[-1], _weights['out']) + _biases['out'] |
| 65 | + |
| 66 | +pred = RNN(x, istate, weights, biases) |
| 67 | + |
| 68 | +# Define loss and optimizer |
| 69 | +cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y)) # Softmax loss |
| 70 | +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # Adam Optimizer |
| 71 | + |
| 72 | +# Evaluate model |
| 73 | +correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1)) |
| 74 | +accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.types.float32)) |
| 75 | + |
| 76 | +# Initializing the variables |
| 77 | +init = tf.initialize_all_variables() |
| 78 | + |
| 79 | +# Launch the graph |
| 80 | +with tf.Session() as sess: |
| 81 | + sess.run(init) |
| 82 | + step = 1 |
| 83 | + # Keep training until reach max iterations |
| 84 | + while step * batch_size < training_iters: |
| 85 | + batch_xs, batch_ys = mnist.train.next_batch(batch_size) |
| 86 | + # Reshape data to get 28 seq of 28 elements |
| 87 | + batch_xs = batch_xs.reshape((batch_size, n_steps, n_input)) |
| 88 | + # Fit training using batch data |
| 89 | + sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, |
| 90 | + istate: np.zeros((batch_size, 2*n_hidden))}) |
| 91 | + if step % display_step == 0: |
| 92 | + # Calculate batch accuracy |
| 93 | + acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, |
| 94 | + istate: np.zeros((batch_size, 2*n_hidden))}) |
| 95 | + # Calculate batch loss |
| 96 | + loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, |
| 97 | + istate: np.zeros((batch_size, 2*n_hidden))}) |
| 98 | + print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + \ |
| 99 | + ", Training Accuracy= " + "{:.5f}".format(acc) |
| 100 | + step += 1 |
| 101 | + print "Optimization Finished!" |
| 102 | + # Calculate accuracy for 256 mnist test images |
| 103 | + test_len = 256 |
| 104 | + test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input)) |
| 105 | + test_label = mnist.test.labels[:test_len] |
| 106 | + print "Testing Accuracy:", sess.run(accuracy, feed_dict={x: test_data, y: test_label, |
| 107 | + istate: np.zeros((test_len, 2*n_hidden))}) |
0 commit comments