• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Test configs for static_rnn_with_control_flow_v2."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import tensorflow.compat.v1 as tf
21from tensorflow.lite.testing.zip_test_utils import create_tensor_data
22from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests
23from tensorflow.lite.testing.zip_test_utils import register_make_test_function
24from tensorflow.python.framework import test_util
25from tensorflow.python.ops import rnn
26
27
28@register_make_test_function("make_static_rnn_with_control_flow_v2_tests")
29@test_util.enable_control_flow_v2
30def make_static_rnn_with_control_flow_v2_tests(options):
31  """Make a set of tests to do basic Lstm cell."""
32
33  test_parameters = [
34      {
35          "dtype": [tf.float32],
36          "num_batches": [4],
37          "time_step_size": [4],
38          "input_vec_size": [3],
39          "num_cells": [4],
40          "use_sequence_length": [True, False],
41      },
42  ]
43
44  def build_graph(parameters):
45    """Build a simple graph with BasicLSTMCell."""
46
47    num_batches = parameters["num_batches"]
48    time_step_size = parameters["time_step_size"]
49    input_vec_size = parameters["input_vec_size"]
50    num_cells = parameters["num_cells"]
51    inputs_after_split = []
52    for i in range(time_step_size):
53      one_timestamp_input = tf.placeholder(
54          dtype=parameters["dtype"],
55          name="split_{}".format(i),
56          shape=[num_batches, input_vec_size])
57      inputs_after_split.append(one_timestamp_input)
58    lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(
59        num_cells, activation=tf.nn.relu, state_is_tuple=True)
60    sequence_length = None
61
62    if parameters["use_sequence_length"]:
63      # Using different sequence length in each bach, like [1, 2, 3, 3...].
64      sequence_length = [
65          min(i + 1, time_step_size) for i in range(num_batches)
66      ]
67    cell_outputs, _ = rnn.static_rnn(
68        lstm_cell,
69        inputs_after_split,
70        dtype=tf.float32,
71        sequence_length=sequence_length)
72    out = cell_outputs[-1]
73    return inputs_after_split, [out]
74
75  def build_inputs(parameters, sess, inputs, outputs):
76    """Feed inputs, assign variables, and freeze graph."""
77
78    with tf.variable_scope("", reuse=True):
79      kernel = tf.get_variable("rnn/basic_lstm_cell/kernel")
80      bias = tf.get_variable("rnn/basic_lstm_cell/bias")
81      kernel_values = create_tensor_data(parameters["dtype"],
82                                         [kernel.shape[0], kernel.shape[1]], -1,
83                                         1)
84      bias_values = create_tensor_data(parameters["dtype"], [bias.shape[0]], 0,
85                                       1)
86      sess.run(tf.group(kernel.assign(kernel_values), bias.assign(bias_values)))
87
88    num_batches = parameters["num_batches"]
89    time_step_size = parameters["time_step_size"]
90    input_vec_size = parameters["input_vec_size"]
91    input_values = []
92    for _ in range(time_step_size):
93      tensor_data = create_tensor_data(parameters["dtype"],
94                                       [num_batches, input_vec_size], 0, 1)
95      input_values.append(tensor_data)
96    out = sess.run(outputs, feed_dict=dict(zip(inputs, input_values)))
97    return input_values, out
98
99  make_zip_of_tests(
100      options,
101      test_parameters,
102      build_graph,
103      build_inputs,
104      use_frozen_graph=True)
105