• 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 dynamic_rnn."""
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_dynamic_rnn_tests")
29@test_util.enable_control_flow_v2
30def make_dynamic_rnn_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, 2],
37          "time_step_size": [4, 3],
38          "input_vec_size": [3, 2],
39          "num_cells": [4, 2],
40      },
41  ]
42
43  def build_graph(parameters):
44    """Build a simple graph with BasicLSTMCell."""
45    num_batches = parameters["num_batches"]
46    time_step_size = parameters["time_step_size"]
47    input_vec_size = parameters["input_vec_size"]
48    num_cells = parameters["num_cells"]
49    input_shape = (num_batches, time_step_size, input_vec_size)
50
51    input_tensor = tf.placeholder(dtype=parameters["dtype"], shape=input_shape)
52    lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_cells, activation=tf.nn.relu)
53
54    output, _ = rnn.dynamic_rnn(
55        lstm_cell, input_tensor, dtype=parameters["dtype"])
56    return [input_tensor], [output]
57
58  def build_inputs(parameters, sess, inputs, outputs):
59    """Feed inputs, assign variables, and freeze graph."""
60    sess.run(tf.global_variables_initializer())
61
62    num_batches = parameters["num_batches"]
63    time_step_size = parameters["time_step_size"]
64    input_vec_size = parameters["input_vec_size"]
65    input_shape = (num_batches, time_step_size, input_vec_size)
66    input_value = create_tensor_data(parameters["dtype"], input_shape)
67
68    output_values = sess.run(
69        outputs, feed_dict=dict(zip(inputs, [input_value])))
70    return [input_value], output_values
71
72  make_zip_of_tests(
73      options,
74      test_parameters,
75      build_graph,
76      build_inputs,
77      use_frozen_graph=True)
78