def test_dynamic_bigru_outputs_partially_consumed(self): units = 5 batch_size = 6 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): cell1 = GRUCell(units) cell2 = GRUCell(units) (output_fw, _), (_, state_bw) = bidirectional_dynamic_rnn(cell1, cell2, x, dtype=tf.float32) return tf.identity(output_fw, name="output"), tf.identity(state_bw, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_dynamic_bidirectional_but_one_gru_and_state_consumed_only(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): # bigru, no scope cell = GRUCell(units) _, cell_state = bidirectional_dynamic_rnn(cell, cell, x, dtype=tf.float32) return tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-3, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_dynamic_bigru_unknown_batch_size(self): units = 5 batch_size = 6 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): cell1 = GRUCell(units) cell2 = GRUCell(units) _, cell_state = bidirectional_dynamic_rnn( cell1, cell2, x, dtype=tf.float32, ) return tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_dynamic_bigru_output_consumed_only(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): initializer = init_ops.constant_initializer(0.5) # bigru, no scope cell1 = GRUCell(units, kernel_initializer=initializer) cell2 = GRUCell(units, kernel_initializer=initializer) outputs, _ = bidirectional_dynamic_rnn(cell1, cell2, x, dtype=tf.float32) return tf.identity(outputs, name="output") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-3, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_ch_zero_state_initializer(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.], [5., 5.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") # no scope cell = rnn.GRUBlockCell(units) # defining initial state initial_state = cell.zero_state(batch_size, dtype=tf.float32) outputs, cell_state = tf.nn.dynamic_rnn(cell, x, initial_state=initial_state, dtype=tf.float32) _ = tf.identity(outputs, name="output") _ = tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-03, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_ch_zero_state_initializer(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.], [5., 5.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): initializer = init_ops.constant_initializer(0.5) # no scope cell = GRUCell(units, kernel_initializer=initializer) # defining initial state initial_state = cell.zero_state(batch_size, dtype=tf.float32) outputs, cell_state = dynamic_rnn(cell, x, initial_state=initial_state, dtype=tf.float32) return tf.identity(outputs, name="output"), tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-03, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_random_weights2(self): hidden_size = 128 batch_size = 1 x_val = np.random.randn(1, 133).astype('f') x_val = np.stack([x_val] * batch_size) def func(x): initializer = tf.random_uniform_initializer(0.0, 1.0) # no scope cell = GRUCell(hidden_size, kernel_initializer=initializer) outputs, cell_state = dynamic_rnn(cell, x, dtype=tf.float32) return tf.identity(outputs, name="output"), tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, 0.01, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_seq_length_is_not_const(self): for np_dtype in [np.int32, np.int64, np.float32]: units = 5 batch_size = 1 x_val = np.array( [[1., 1.], [2., 2.], [3., 3.], [4., 4.], [5., 5.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) y_val = np.array([5], dtype=np_dtype) def func(x, seq_length): initializer = init_ops.constant_initializer(0.5) # no scope cell = GRUCell(units, kernel_initializer=initializer) outputs, cell_state = dynamic_rnn( cell, x, dtype=tf.float32, sequence_length=tf.identity(seq_length)) return tf.identity(outputs, name="output"), tf.identity( cell_state, name="cell_state") feed_dict = {"input_1:0": x_val, "input_2:0": y_val} input_names_with_port = ["input_1:0", "input_2:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-03, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_placeholder_input(self): units = 5 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) x_val = np.stack([x_val] * 1) def func(x): initializer = init_ops.constant_initializer(0.5) # no scope cell = GRUCell(units, kernel_initializer=initializer) outputs, cell_state = dynamic_rnn( cell, x, dtype=tf.float32) # by default zero initializer is used return tf.identity(outputs, name="output"), tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-03, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_seq_length_is_const(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.], [5., 5.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): # no scope cell = GRUBlockCell(units) outputs, cell_state = dynamic_rnn(cell, x, dtype=tf.float32, sequence_length=[5]) return tf.identity(outputs, name="output"), tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-3, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_random_weights(self): hidden_size = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): # no scope cell = GRUBlockCell(hidden_size) outputs, cell_state = dynamic_rnn(cell, x, dtype=tf.float32) return tf.identity(outputs, name="output"), tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, 0.0001, graph_validator=lambda g: check_gru_count(g, 1))
def test_dynamic_bigru(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") # bigru, no scope cell1 = rnn.GRUBlockCell(units) cell2 = rnn.GRUBlockCell(units) outputs, cell_state = tf.nn.bidirectional_dynamic_rnn(cell1, cell2, x, dtype=tf.float32) _ = tf.identity(outputs, name="output") _ = tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-3, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_random_weights(self): hidden_size = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") initializer = tf.random_uniform_initializer(-1.0, 1.0) # no scope cell = rnn.GRUCell(hidden_size, kernel_initializer=initializer) outputs, cell_state = tf.nn.dynamic_rnn(cell, x, dtype=tf.float32) _ = tf.identity(outputs, name="output") _ = tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, 0.0001, graph_validator=lambda g: check_gru_count(g, 1))
def test_multiple_dynamic_gru(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): gru_output_list = [] gru_cell_state_list = [] # no scope cell = GRUBlockCell(units) outputs, cell_state = dynamic_rnn(cell, x, dtype=tf.float32) gru_output_list.append(outputs) gru_cell_state_list.append(cell_state) # given scope cell = GRUBlockCell(units) with variable_scope.variable_scope("root1") as scope: outputs, cell_state = dynamic_rnn(cell, x, dtype=tf.float32, sequence_length=[4], scope=scope) gru_output_list.append(outputs) gru_cell_state_list.append(cell_state) return tf.identity(gru_output_list, name="output"), tf.identity(gru_cell_state_list, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-3, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 2))
def test_single_dynamic_gru_seq_length_is_not_const(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.], [5., 5.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") y_val = np.array([5], dtype=np.int32) seq_length = tf.placeholder(tf.int32, y_val.shape, name="input_2") # no scope cell = rnn.GRUBlockCell(units) outputs, cell_state = tf.nn.dynamic_rnn( cell, x, dtype=tf.float32, sequence_length=tf.identity(seq_length)) _ = tf.identity(outputs, name="output") _ = tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val, "input_2:0": y_val} input_names_with_port = ["input_1:0", "input_2:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-03, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): # no scope cell = GRUCell(units, activation=None) outputs, cell_state = dynamic_rnn(cell, x, dtype=tf.float32) return tf.identity(outputs, name="output"), tf.identity(cell_state, name="cell_state") input_names_with_port = ["input_1:0"] feed_dict = {"input_1:0": x_val} output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, rtol=1e-03, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_dynamic_multi_bigru_with_same_input_seq_len(self): units = 5 batch_size = 10 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) seq_len_val = np.array([3], dtype=np.int32) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") y1 = tf.placeholder(tf.int32, seq_len_val.shape, name="input_2") seq_len1 = tf.tile(y1, [batch_size]) cell1 = rnn.GRUCell(units) cell2 = rnn.GRUCell(units) outputs_1, cell_state_1 = tf.nn.bidirectional_dynamic_rnn( cell1, cell2, x, sequence_length=seq_len1, dtype=tf.float32, scope="bigru_1") y2 = tf.placeholder(tf.int32, seq_len_val.shape, name="input_3") seq_len2 = tf.tile(y2, [batch_size]) cell1 = rnn.GRUCell(units) cell2 = rnn.GRUCell(units) outputs_2, cell_state_2 = tf.nn.bidirectional_dynamic_rnn( cell1, cell2, x, sequence_length=seq_len2, dtype=tf.float32, scope="bigru_2") _ = tf.identity(outputs_1, name="output_1") _ = tf.identity(cell_state_1, name="cell_state_1") _ = tf.identity(outputs_2, name="output_2") _ = tf.identity(cell_state_2, name="cell_state_2") feed_dict = { "input_1:0": x_val, "input_2:0": seq_len_val, "input_3:0": seq_len_val } input_names_with_port = ["input_1:0", "input_2:0", "input_3:0"] output_names_with_port = [ "output_1:0", "cell_state_1:0", "output_2:0", "cell_state_2:0" ] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-3, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 2))
def test_dynamic_gru_state_consumed_only(self): units = 5 batch_size = 6 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): cell1 = GRUBlockCell(units) _, cell_state = dynamic_rnn(cell1, x, dtype=tf.float32) return tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["cell_state:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, 0.0001, graph_validator=lambda g: check_gru_count(g, 1))
def test_multiple_dynamic_gru(self): units = 5 batch_size = 1 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") _ = tf.placeholder(tf.float32, x_val.shape, name="input_2") gru_output_list = [] gru_cell_state_list = [] # no scope cell = rnn.GRUCell( units, activation=None) outputs, cell_state = tf.nn.dynamic_rnn( cell, x, dtype=tf.float32) gru_output_list.append(outputs) gru_cell_state_list.append(cell_state) # given scope cell = rnn.GRUCell( units, activation=None) with variable_scope.variable_scope("root1") as scope: outputs, cell_state = tf.nn.dynamic_rnn( cell, x, dtype=tf.float32, sequence_length=[4], scope=scope) gru_output_list.append(outputs) gru_cell_state_list.append(cell_state) _ = tf.identity(gru_output_list, name="output") _ = tf.identity(gru_cell_state_list, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-3, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 2))
def test_dynamic_gru_output_consumed_only(self): units = 5 batch_size = 6 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") cell1 = rnn.GRUBlockCell(units) outputs, _ = tf.nn.dynamic_rnn(cell1, x, dtype=tf.float32) _ = tf.identity(outputs, name="output") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, 0.0001, graph_validator=lambda g: check_gru_count(g, 1))
def test_dynamic_gru_output_consumed_only(self): units = 5 batch_size = 6 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) def func(x): initializer = tf.random_uniform_initializer(-1.0, 1.0, seed=42) cell1 = GRUCell(units, kernel_initializer=initializer) outputs, _ = dynamic_rnn(cell1, x, dtype=tf.float32) return tf.identity(outputs, name="output") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0"] self.run_test_case(func, feed_dict, input_names_with_port, output_names_with_port, 0.0001, graph_validator=lambda g: check_gru_count(g, 1))
def test_dynamic_gru_state_consumed_only(self): units = 5 batch_size = 6 x_val = np.array([[1., 1.], [2., 2.], [3., 3.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") initializer = tf.random_uniform_initializer(-1.0, 1.0) cell1 = rnn.GRUCell(units, kernel_initializer=initializer) _, cell_state = tf.nn.dynamic_rnn(cell1, x, dtype=tf.float32) _ = tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["cell_state:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=0.0001, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_random_weights2(self): hidden_size = 128 batch_size = 1 x_val = np.random.randn(1, 133).astype('f') x_val = np.stack([x_val] * batch_size) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") # no scope cell = rnn.GRUBlockCell(hidden_size) outputs, cell_state = tf.nn.dynamic_rnn(cell, x, dtype=tf.float32) _ = tf.identity(outputs, name="output") _ = tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, 0.01, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_seq_length_is_not_const(self): for np_dtype, tf_dtype in [[np.int32, tf.int32], [np.int64, tf.int64], [np.float32, tf.float32]]: tf.reset_default_graph() units = 5 batch_size = 1 x_val = np.array( [[1., 1.], [2., 2.], [3., 3.], [4., 4.], [5., 5.]], dtype=np.float32) x_val = np.stack([x_val] * batch_size) x = tf.placeholder(tf.float32, x_val.shape, name="input_1") initializer = init_ops.constant_initializer(0.5) y_val = np.array([5], dtype=np_dtype) seq_length = tf.placeholder(tf_dtype, y_val.shape, name="input_2") # no scope cell = cudnn_rnn.CudnnCompatibleGRUCell( units, kernel_initializer=initializer) outputs, cell_state = tf.nn.dynamic_rnn( cell, x, dtype=tf.float32, sequence_length=tf.identity(seq_length)) _ = tf.identity(outputs, name="output") _ = tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val, "input_2:0": y_val} input_names_with_port = ["input_1:0", "input_2:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-03, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))
def test_single_dynamic_gru_placeholder_input(self): units = 5 x_val = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]], dtype=np.float32) x_val = np.stack([x_val] * 1) x = tf.placeholder(tf.float32, shape=(None, 4, 2), name="input_1") # no scope cell = rnn.GRUBlockCell(units) outputs, cell_state = tf.nn.dynamic_rnn( cell, x, dtype=tf.float32) # by default zero initializer is used _ = tf.identity(outputs, name="output") _ = tf.identity(cell_state, name="cell_state") feed_dict = {"input_1:0": x_val} input_names_with_port = ["input_1:0"] output_names_with_port = ["output:0", "cell_state:0"] self.run_test_case(feed_dict, input_names_with_port, output_names_with_port, rtol=1e-3, atol=1e-06, graph_validator=lambda g: check_gru_count(g, 1))