|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +"""Unit Tests for lstm block cell.""" |
| 5 | + |
| 6 | +from __future__ import absolute_import |
| 7 | +from __future__ import division |
| 8 | +from __future__ import print_function |
| 9 | +from __future__ import unicode_literals |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import tensorflow as tf |
| 13 | + |
| 14 | +from tensorflow.contrib import rnn |
| 15 | +from backend_test_base import Tf2OnnxBackendTestBase |
| 16 | +from common import unittest_main, check_tf_min_version, check_opset_min_version |
| 17 | + |
| 18 | + |
| 19 | +# pylint: disable=missing-docstring,invalid-name,unused-argument,using-constant-test |
| 20 | + |
| 21 | + |
| 22 | +class LSTMBlockTests(Tf2OnnxBackendTestBase): |
| 23 | + @check_opset_min_version(8, "Scan") |
| 24 | + def test_single_dynamic_lstm(self): |
| 25 | + units = 5 |
| 26 | + batch_size = 6 |
| 27 | + x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) |
| 28 | + x_val = np.stack([x_val] * batch_size) |
| 29 | + |
| 30 | + x = tf.placeholder(tf.float32, x_val.shape, name="input_1") |
| 31 | + |
| 32 | + # no scope |
| 33 | + cell = rnn.LSTMBlockCell(units, use_peephole=False) |
| 34 | + outputs, cell_state = tf.nn.dynamic_rnn( |
| 35 | + cell, |
| 36 | + x, |
| 37 | + dtype=tf.float32) |
| 38 | + |
| 39 | + _ = tf.identity(outputs, name="output") |
| 40 | + _ = tf.identity(cell_state, name="cell_state") |
| 41 | + |
| 42 | + input_names_with_port = ["input_1:0"] |
| 43 | + feed_dict = {"input_1:0": x_val} |
| 44 | + |
| 45 | + output_names_with_port = ["output:0", "cell_state:0"] |
| 46 | + self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-06, atol=1e-07) |
| 47 | + |
| 48 | + # ============================================================================================== |
| 49 | + # NOTE: the unittest above should be converted into a single LSTM op, while following unittests |
| 50 | + # should be first converted into a Scan op with LSTMBlockCell, then decoupled into several ops. |
| 51 | + # ============================================================================================== |
| 52 | + |
| 53 | + @check_opset_min_version(8, "Scan") |
| 54 | + def test_single_dynamic_lstm_with_peephole(self): |
| 55 | + units = 5 |
| 56 | + batch_size = 6 |
| 57 | + x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) |
| 58 | + x_val = np.stack([x_val] * batch_size) |
| 59 | + |
| 60 | + x = tf.placeholder(tf.float32, x_val.shape, name="input_1") |
| 61 | + |
| 62 | + # no scope |
| 63 | + cell = rnn.LSTMBlockCell(units, use_peephole=True) |
| 64 | + outputs, cell_state = tf.nn.dynamic_rnn( |
| 65 | + cell, |
| 66 | + x, |
| 67 | + dtype=tf.float32) |
| 68 | + |
| 69 | + _ = tf.identity(outputs, name="output") |
| 70 | + _ = tf.identity(cell_state, name="cell_state") |
| 71 | + |
| 72 | + input_names_with_port = ["input_1:0"] |
| 73 | + feed_dict = {"input_1:0": x_val} |
| 74 | + |
| 75 | + output_names_with_port = ["output:0", "cell_state:0"] |
| 76 | + self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-06, atol=1e-07) |
| 77 | + |
| 78 | + @check_opset_min_version(8, "Scan") |
| 79 | + def test_single_dynamic_lstm_with_cell_clip(self): |
| 80 | + units = 5 |
| 81 | + batch_size = 6 |
| 82 | + x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) |
| 83 | + x_val = np.stack([x_val] * batch_size) |
| 84 | + |
| 85 | + x = tf.placeholder(tf.float32, x_val.shape, name="input_1") |
| 86 | + |
| 87 | + # no scope |
| 88 | + cell = rnn.LSTMBlockCell(units, cell_clip=0.05) |
| 89 | + outputs, cell_state = tf.nn.dynamic_rnn( |
| 90 | + cell, |
| 91 | + x, |
| 92 | + dtype=tf.float32) |
| 93 | + |
| 94 | + _ = tf.identity(outputs, name="output") |
| 95 | + _ = tf.identity(cell_state, name="cell_state") |
| 96 | + |
| 97 | + input_names_with_port = ["input_1:0"] |
| 98 | + feed_dict = {"input_1:0": x_val} |
| 99 | + |
| 100 | + output_names_with_port = ["output:0", "cell_state:0"] |
| 101 | + self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-06, atol=1e-07) |
| 102 | + |
| 103 | + @check_opset_min_version(8, "Scan") |
| 104 | + @check_tf_min_version("1.8") |
| 105 | + def test_attention_wrapper_lstm_encoder(self): |
| 106 | + size = 5 |
| 107 | + time_step = 3 |
| 108 | + input_size = 4 |
| 109 | + attn_size = size |
| 110 | + |
| 111 | + batch_size = 9 |
| 112 | + |
| 113 | + # shape [batch size, time step, size] |
| 114 | + # attention_state: usually the output of an RNN encoder. |
| 115 | + # This tensor should be shaped `[batch_size, max_time, ...]` |
| 116 | + encoder_time_step = time_step |
| 117 | + encoder_x_val = np.random.randn(encoder_time_step, input_size).astype('f') |
| 118 | + encoder_x_val = np.stack([encoder_x_val] * batch_size) |
| 119 | + encoder_x = tf.placeholder(tf.float32, encoder_x_val.shape, name="input_1") |
| 120 | + encoder_cell = rnn.LSTMBlockCell(size) |
| 121 | + output, attr_state = tf.nn.dynamic_rnn(encoder_cell, encoder_x, dtype=tf.float32) |
| 122 | + _ = tf.identity(output, name="output_0") |
| 123 | + attention_states = output |
| 124 | + attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(attn_size, |
| 125 | + attention_states) |
| 126 | + |
| 127 | + match_input_fn = lambda curr_input, state: tf.concat([curr_input, state], axis=-1) |
| 128 | + cell = rnn.LSTMBlockCell(size) |
| 129 | + match_cell_fw = tf.contrib.seq2seq.AttentionWrapper(cell, |
| 130 | + attention_mechanism, |
| 131 | + attention_layer_size=attn_size, |
| 132 | + cell_input_fn=match_input_fn, |
| 133 | + output_attention=False) |
| 134 | + |
| 135 | + decoder_time_step = 6 |
| 136 | + decoder_x_val = np.random.randn(decoder_time_step, input_size).astype('f') |
| 137 | + decoder_x_val = np.stack([decoder_x_val] * batch_size) |
| 138 | + |
| 139 | + decoder_x = tf.placeholder(tf.float32, decoder_x_val.shape, name="input_2") |
| 140 | + output, attr_state = tf.nn.dynamic_rnn(match_cell_fw, decoder_x, dtype=tf.float32) |
| 141 | + |
| 142 | + _ = tf.identity(output, name="output") |
| 143 | + _ = tf.identity(attr_state.cell_state, name="final_state") |
| 144 | + |
| 145 | + feed_dict = {"input_1:0": encoder_x_val, "input_2:0": decoder_x_val} |
| 146 | + input_names_with_port = ["input_1:0", "input_2:0"] |
| 147 | + output_names_with_port = ["output_0:0", "output:0", "final_state:0"] |
| 148 | + self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, 0.1) |
| 149 | + |
| 150 | + @check_opset_min_version(8, "Scan") |
| 151 | + def test_multi_rnn_lstm(self): |
| 152 | + units = 5 |
| 153 | + batch_size = 6 |
| 154 | + x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) |
| 155 | + x_val = np.stack([x_val] * batch_size) |
| 156 | + |
| 157 | + x = tf.placeholder(tf.float32, x_val.shape, name="input_1") |
| 158 | + |
| 159 | + cell_0 = rnn.LSTMBlockCell(units) |
| 160 | + |
| 161 | + cell_1 = rnn.LSTMBlockCell(units) |
| 162 | + |
| 163 | + cell_2 = rnn.LSTMBlockCell(units) |
| 164 | + |
| 165 | + cells = rnn.MultiRNNCell([cell_0, cell_1, cell_2], state_is_tuple=True) |
| 166 | + outputs, cell_state = tf.nn.dynamic_rnn(cells, |
| 167 | + x, |
| 168 | + dtype=tf.float32) |
| 169 | + |
| 170 | + _ = tf.identity(outputs, name="output") |
| 171 | + _ = tf.identity(cell_state, name="cell_state") |
| 172 | + |
| 173 | + input_names_with_port = ["input_1:0"] |
| 174 | + feed_dict = {"input_1:0": x_val} |
| 175 | + |
| 176 | + output_names_with_port = ["output:0", "cell_state:0"] |
| 177 | + self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-06, atol=1e-07) |
| 178 | + |
| 179 | +if __name__ == '__main__': |
| 180 | + unittest_main() |
0 commit comments