recurrent_network.py 4.4 KB

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