• 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 lstm."""
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 ExtraTocoOptions
23from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests
24from tensorflow.lite.testing.zip_test_utils import register_make_test_function
25from tensorflow.python.ops import rnn
26
27
28@register_make_test_function()
29def make_lstm_tests(options):
30  """Make a set of tests to do basic Lstm cell."""
31
32  test_parameters = [
33      {
34          "dtype": [tf.float32],
35          "num_batchs": [1],
36          "time_step_size": [1],
37          "input_vec_size": [3],
38          "num_cells": [4],
39          "split_tflite_lstm_inputs": [False],
40      },
41  ]
42
43  def build_graph(parameters):
44    """Build a simple graph with BasicLSTMCell."""
45
46    num_batchs = parameters["num_batchs"]
47    time_step_size = parameters["time_step_size"]
48    input_vec_size = parameters["input_vec_size"]
49    num_cells = parameters["num_cells"]
50    inputs_after_split = []
51    for i in range(time_step_size):
52      one_timestamp_input = tf.compat.v1.placeholder(
53          dtype=parameters["dtype"],
54          name="split_{}".format(i),
55          shape=[num_batchs, input_vec_size])
56      inputs_after_split.append(one_timestamp_input)
57    # Currently lstm identifier has a few limitations: only supports
58    # forget_bias == 0, inner state activation == tanh.
59    # TODO(zhixianyan): Add another test with forget_bias == 1.
60    # TODO(zhixianyan): Add another test with relu as activation.
61    lstm_cell = tf.compat.v1.nn.rnn_cell.BasicLSTMCell(
62        num_cells, forget_bias=0.0, state_is_tuple=True)
63    cell_outputs, _ = rnn.static_rnn(
64        lstm_cell, inputs_after_split, dtype=tf.float32)
65    out = cell_outputs[-1]
66    return inputs_after_split, [out]
67
68  def build_inputs(parameters, sess, inputs, outputs):
69    """Feed inputs, assign variables, and freeze graph."""
70
71    with tf.compat.v1.variable_scope("", reuse=True):
72      kernel = tf.get_variable("rnn/basic_lstm_cell/kernel")
73      bias = tf.get_variable("rnn/basic_lstm_cell/bias")
74      kernel_values = create_tensor_data(parameters["dtype"],
75                                         [kernel.shape[0], kernel.shape[1]], -1,
76                                         1)
77      bias_values = create_tensor_data(parameters["dtype"], [bias.shape[0]], 0,
78                                       1)
79      sess.run(tf.group(kernel.assign(kernel_values), bias.assign(bias_values)))
80
81    num_batchs = parameters["num_batchs"]
82    time_step_size = parameters["time_step_size"]
83    input_vec_size = parameters["input_vec_size"]
84    input_values = []
85    for _ in range(time_step_size):
86      tensor_data = create_tensor_data(parameters["dtype"],
87                                       [num_batchs, input_vec_size], 0, 1)
88      input_values.append(tensor_data)
89    out = sess.run(outputs, feed_dict=dict(zip(inputs, input_values)))
90    return input_values, out
91
92  # TODO(zhixianyan): Automatically generate rnn_states for lstm cell.
93  extra_toco_options = ExtraTocoOptions()
94  extra_toco_options.rnn_states = (
95      "{state_array:rnn/BasicLSTMCellZeroState/zeros,"
96      "back_edge_source_array:rnn/basic_lstm_cell/Add_1,size:4},"
97      "{state_array:rnn/BasicLSTMCellZeroState/zeros_1,"
98      "back_edge_source_array:rnn/basic_lstm_cell/Mul_2,size:4}")
99
100  make_zip_of_tests(
101      options,
102      test_parameters,
103      build_graph,
104      build_inputs,
105      extra_toco_options,
106      use_frozen_graph=True)
107