bidirectional_rnn.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. '''
  2. A Bidirectional Recurrent 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. from __future__ import print_function
  9. import tensorflow as tf
  10. from tensorflow.contrib import rnn
  11. import numpy as np
  12. # Import MNIST data
  13. from tensorflow.examples.tutorials.mnist import input_data
  14. mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
  15. '''
  16. To classify images using a bidirectional recurrent neural network, we consider
  17. every image row as a sequence of pixels. Because MNIST image shape is 28*28px,
  18. we will then handle 28 sequences of 28 steps for every sample.
  19. '''
  20. # Parameters
  21. learning_rate = 0.001
  22. training_iters = 100000
  23. batch_size = 128
  24. display_step = 10
  25. # Network Parameters
  26. n_input = 28 # MNIST data input (img shape: 28*28)
  27. n_steps = 28 # timesteps
  28. n_hidden = 128 # hidden layer num of features
  29. n_classes = 10 # MNIST total classes (0-9 digits)
  30. # tf Graph input
  31. x = tf.placeholder("float", [None, n_steps, n_input])
  32. y = tf.placeholder("float", [None, n_classes])
  33. # Define weights
  34. weights = {
  35. # Hidden layer weights => 2*n_hidden because of forward + backward cells
  36. 'out': tf.Variable(tf.random_normal([2*n_hidden, n_classes]))
  37. }
  38. biases = {
  39. 'out': tf.Variable(tf.random_normal([n_classes]))
  40. }
  41. def BiRNN(x, weights, biases):
  42. # Prepare data shape to match `bidirectional_rnn` function requirements
  43. # Current data input shape: (batch_size, n_steps, n_input)
  44. # Required shape: 'n_steps' tensors list of shape (batch_size, n_input)
  45. # Unstack to get a list of 'n_steps' tensors of shape (batch_size, n_input)
  46. x = tf.unstack(x, n_steps, 1)
  47. # Define lstm cells with tensorflow
  48. # Forward direction cell
  49. lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
  50. # Backward direction cell
  51. lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
  52. # Get lstm cell output
  53. try:
  54. outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
  55. dtype=tf.float32)
  56. except Exception: # Old TensorFlow version only returns outputs not states
  57. outputs = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
  58. dtype=tf.float32)
  59. # Linear activation, using rnn inner loop last output
  60. return tf.matmul(outputs[-1], weights['out']) + biases['out']
  61. pred = BiRNN(x, weights, biases)
  62. # Define loss and optimizer
  63. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
  64. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
  65. # Evaluate model
  66. correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
  67. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
  68. # Initializing the variables
  69. init = tf.global_variables_initializer()
  70. # Launch the graph
  71. with tf.Session() as sess:
  72. sess.run(init)
  73. step = 1
  74. # Keep training until reach max iterations
  75. while step * batch_size < training_iters:
  76. batch_x, batch_y = mnist.train.next_batch(batch_size)
  77. # Reshape data to get 28 seq of 28 elements
  78. batch_x = batch_x.reshape((batch_size, n_steps, n_input))
  79. # Run optimization op (backprop)
  80. sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
  81. if step % display_step == 0:
  82. # Calculate batch accuracy
  83. acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
  84. # Calculate batch loss
  85. loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
  86. print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
  87. "{:.6f}".format(loss) + ", Training Accuracy= " + \
  88. "{:.5f}".format(acc))
  89. step += 1
  90. print("Optimization Finished!")
  91. # Calculate accuracy for 128 mnist test images
  92. test_len = 128
  93. test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))
  94. test_label = mnist.test.labels[:test_len]
  95. print("Testing Accuracy:", \
  96. sess.run(accuracy, feed_dict={x: test_data, y: test_label}))