| 48 | |
| 49 | |
| 50 | def RNN(X, weights, biases): |
| 51 | # hidden layer for input to cell |
| 52 | ######################################## |
| 53 | |
| 54 | # transpose the inputs shape from |
| 55 | # X ==> (128 batch * 28 steps, 28 inputs) |
| 56 | X = tf.reshape(X, [-1, n_inputs]) |
| 57 | |
| 58 | # into hidden |
| 59 | # X_in = (128 batch * 28 steps, 128 hidden) |
| 60 | X_in = tf.matmul(X, weights['in']) + biases['in'] |
| 61 | # X_in ==> (128 batch, 28 steps, 128 hidden) |
| 62 | X_in = tf.reshape(X_in, [-1, n_steps, n_hidden_units]) |
| 63 | |
| 64 | # cell |
| 65 | ########################################## |
| 66 | |
| 67 | # basic LSTM Cell. |
| 68 | if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1: |
| 69 | cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden_units, forget_bias=1.0, state_is_tuple=True) |
| 70 | else: |
| 71 | cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units) |
| 72 | # lstm cell is divided into two parts (c_state, h_state) |
| 73 | init_state = cell.zero_state(batch_size, dtype=tf.float32) |
| 74 | |
| 75 | # You have 2 options for following step. |
| 76 | # 1: tf.nn.rnn(cell, inputs); |
| 77 | # 2: tf.nn.dynamic_rnn(cell, inputs). |
| 78 | # If use option 1, you have to modified the shape of X_in, go and check out this: |
| 79 | # https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/recurrent_network.py |
| 80 | # In here, we go for option 2. |
| 81 | # dynamic_rnn receive Tensor (batch, steps, inputs) or (steps, batch, inputs) as X_in. |
| 82 | # Make sure the time_major is changed accordingly. |
| 83 | outputs, final_state = tf.nn.dynamic_rnn(cell, X_in, initial_state=init_state, time_major=False) |
| 84 | |
| 85 | # hidden layer for output as the final results |
| 86 | ############################################# |
| 87 | # results = tf.matmul(final_state[1], weights['out']) + biases['out'] |
| 88 | |
| 89 | # # or |
| 90 | # unpack to list [(batch, outputs)..] * steps |
| 91 | if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1: |
| 92 | outputs = tf.unpack(tf.transpose(outputs, [1, 0, 2])) # states is the last outputs |
| 93 | else: |
| 94 | outputs = tf.unstack(tf.transpose(outputs, [1,0,2])) |
| 95 | results = tf.matmul(outputs[-1], weights['out']) + biases['out'] # shape = (128, 10) |
| 96 | |
| 97 | return results |
| 98 | |
| 99 | |
| 100 | pred = RNN(x, weights, biases) |