|
| 1 | +# http://www.wildml.com/2016/08/rnns-in-tensorflow-a-practical-guide-and-undocumented-features/ |
| 2 | +# http://learningtensorflow.com/index.html |
| 3 | +# http://suriyadeepan.github.io/2016-12-31-practical-seq2seq/ |
| 4 | + |
| 5 | +import tensorflow as tf |
| 6 | +import numpy as np |
| 7 | +from tensorflow.contrib import rnn |
| 8 | +import pprint |
| 9 | +pp = pprint.PrettyPrinter(indent=4) |
| 10 | +tf.reset_default_graph() |
| 11 | +sess = tf.InteractiveSession() |
| 12 | + |
| 13 | + |
| 14 | +# One cell RNN input_dim (3) -> output_dim (5) |
| 15 | +hidden_size = 5 |
| 16 | +cell = tf.contrib.rnn.BasicRNNCell(num_units=hidden_size) |
| 17 | +print(cell.output_size, cell.state_size) |
| 18 | + |
| 19 | +x_data = np.array([[[1, 2, 3]]], dtype=np.float32) |
| 20 | +outputs, _states = tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32) |
| 21 | + |
| 22 | +sess.run(tf.global_variables_initializer()) |
| 23 | +pp.pprint(outputs.eval()) |
| 24 | +sess.close() |
| 25 | + |
| 26 | +states = 0 |
| 27 | + |
| 28 | +tf.reset_default_graph() |
| 29 | +sess = tf.InteractiveSession() |
| 30 | + |
| 31 | +# One cell RNN input_dim (3) -> output_dim (5). sequence: 2 |
| 32 | +hidden_size = 5 |
| 33 | +cell = tf.contrib.rnn.BasicRNNCell(num_units=hidden_size) |
| 34 | +x_data = np.array([[[1, 2, 3], |
| 35 | + [4, 5, 6]]], dtype=np.float32) |
| 36 | +outputs, states = tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32) |
| 37 | +sess.run(tf.global_variables_initializer()) |
| 38 | +pp.pprint(outputs.eval()) |
| 39 | +sess.close() |
| 40 | + |
| 41 | + |
| 42 | +tf.reset_default_graph() |
| 43 | +sess = tf.InteractiveSession() |
| 44 | +# One cell RNN input_dim (3) -> output_dim (5). sequence: 2 |
| 45 | +cell = rnn.BasicLSTMCell(num_units=5, state_is_tuple=True) |
| 46 | +x_data = np.array([[[1, 2, 3], |
| 47 | + [4, 5, 6]], |
| 48 | + |
| 49 | + [[7, 8, 9], |
| 50 | + [10, 11, 12]], |
| 51 | + |
| 52 | + [[13, 14, 15], |
| 53 | + [16, 17, 18]], ], dtype=np.float32) |
| 54 | +outputs, _states = tf.nn.dynamic_rnn( |
| 55 | + cell, x_data, sequence_length=[1,2,1], dtype=tf.float32) |
| 56 | +sess.run(tf.global_variables_initializer()) |
| 57 | +pp.pprint(outputs.eval()) |
| 58 | +sess.close() |
| 59 | + |
| 60 | + |
| 61 | +tf.reset_default_graph() |
| 62 | +sess = tf.InteractiveSession() |
| 63 | +# One cell RNN input_dim (3) -> output_dim (5). sequence: 2 |
| 64 | + |
| 65 | +batch_size = 3 |
| 66 | +cell = rnn.BasicLSTMCell(num_units=5, state_is_tuple=True) |
| 67 | +initial_state = cell.zero_state(batch_size, tf.float32) |
| 68 | + |
| 69 | +x_data = np.array([[[1, 2, 3], |
| 70 | + [4, 5, 6]], |
| 71 | + |
| 72 | + [[7, 8, 9], |
| 73 | + [10, 11, 12]], |
| 74 | + |
| 75 | + [[13, 14, 15], |
| 76 | + [16, 17, 18]], ], dtype=np.float32) |
| 77 | +outputs, _states = tf.nn.dynamic_rnn(cell, x_data, |
| 78 | + initial_state=initial_state, dtype=tf.float32) |
| 79 | +sess.run(tf.global_variables_initializer()) |
| 80 | +pp.pprint(outputs.eval()) |
| 81 | +sess.close() |
| 82 | + |
| 83 | + |
| 84 | +tf.reset_default_graph() |
| 85 | +sess = tf.InteractiveSession() |
| 86 | +# Create input data |
| 87 | +x_data = np.arange(24, dtype=np.float32).reshape(2, 4, 3) |
| 88 | +pp.pprint(x_data) # batch, sequence_length, input size |
| 89 | + |
| 90 | +# Make rnn |
| 91 | +cell = rnn.BasicLSTMCell(num_units=5, state_is_tuple=True) |
| 92 | +cell = rnn.MultiRNNCell([cell] * 3, state_is_tuple=True) |
| 93 | + |
| 94 | +# rnn in/out |
| 95 | +outputs, _states = tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32) |
| 96 | +print("dynamic rnn: ", outputs) |
| 97 | +sess.run(tf.global_variables_initializer()) |
| 98 | +pp.pprint(outputs.eval()) # batch size, unrolling (time), hidden_size |
| 99 | + |
| 100 | +cell = rnn.BasicLSTMCell(num_units=5, state_is_tuple=True) |
| 101 | +outputs, _states = tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32, |
| 102 | + sequence_length=[1, 2]) |
| 103 | +print("dynamic rnn: ", outputs) |
| 104 | +sess.run(tf.global_variables_initializer()) |
| 105 | +pp.pprint(outputs.eval()) # batch size, unrolling (time), hidden_size |
| 106 | +sess.close() |
| 107 | + |
| 108 | + |
| 109 | +tf.reset_default_graph() |
| 110 | +sess = tf.InteractiveSession() |
| 111 | +# bi-directional rnn |
| 112 | +cell_fw = rnn.BasicLSTMCell(num_units=5, state_is_tuple=True) |
| 113 | +cell_bw = rnn.BasicLSTMCell(num_units=5, state_is_tuple=True) |
| 114 | + |
| 115 | +outputs, states = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, x_data, |
| 116 | + sequence_length=[2, 3], |
| 117 | + dtype=tf.float32) |
| 118 | + |
| 119 | +sess.run(tf.global_variables_initializer()) |
| 120 | +pp.pprint(sess.run(outputs)) |
| 121 | +pp.pprint(sess.run(states)) |
| 122 | + |
| 123 | +# Broadcasting based softmax |
| 124 | +softmax_w = np.arange(12, dtype=np.float32).reshape(4, 3) |
| 125 | +outputs = x_data * softmax_w |
| 126 | +pp.pprint(softmax_w) |
| 127 | +pp.pprint(outputs) |
| 128 | +outputs = x_data * softmax_w + [1, 2, 3] |
| 129 | +pp.pprint(outputs) |
| 130 | + |
| 131 | +# [batch_size, sequence_length, emb_dim ] |
| 132 | +prediction1 = tf.constant([[[0, 1], [0, 1], [0, 1]]], dtype=tf.float32) |
| 133 | +prediction2 = tf.constant([[[1, 0], [1, 0], [1, 0]]], dtype=tf.float32) |
| 134 | +prediction3 = tf.constant([[[0, 1], [1, 0], [0, 1]]], dtype=tf.float32) |
| 135 | + |
| 136 | +# [batch_size, sequence_length] |
| 137 | +y_data = tf.constant([[1, 1, 1]]) |
| 138 | + |
| 139 | +# [batch_size * sequence_length] |
| 140 | +weights = tf.constant([[1, 1, 1]], dtype=tf.float32) |
| 141 | + |
| 142 | +sequence_loss1 = tf.contrib.seq2seq.sequence_loss(prediction1, y_data, weights) |
| 143 | +sequence_loss2 = tf.contrib.seq2seq.sequence_loss(prediction2, y_data, weights) |
| 144 | +sequence_loss3 = tf.contrib.seq2seq.sequence_loss(prediction3, y_data, weights) |
| 145 | + |
| 146 | +sess.run(tf.global_variables_initializer()) |
| 147 | +print("Loss1: ", sequence_loss1.eval(), |
| 148 | + "Loss2: ", sequence_loss2.eval(), |
| 149 | + "Loss3: ", sequence_loss3.eval()) |
0 commit comments