title: Converting Keras RNN model to TensorFlow Lite model in TF2
body: <p>I am currently trying to convert a RNN model to TF lite. After multiple failed attempts I tried running the example given in the repository found <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/experimental/examples/lstm/TensorFlowLite_LSTM_Keras_Tutorial.ipynb" rel="nofollow noreferrer">here</a>. This threw errors too due to changes in the layer definition location. Once fixed to the code below</p>
  
  <pre class="lang-py prettyprint-override"><code>import os
  os.environ['TF_ENABLE_CONTROL_FLOW_V2'] = '1'
  
  import tensorflow as tf
  import numpy as np
  from tensorflow_core.lite.experimental.examples.lstm.rnn_cell import TFLiteLSTMCell
  from tensorflow_core.lite.experimental.examples.lstm.rnn import dynamic_rnn
  
  # Step 1: Build the MNIST LSTM model.
  def buildLstmLayer(inputs, num_layers, num_units):
    """Build the lstm layer.
  
    Args:
      inputs: The input data.
      num_layers: How many LSTM layers do we want.
      num_units: The unmber of hidden units in the LSTM cell.
    """
    lstm_cells = []
    for i in range(num_layers):
      lstm_cells.append(
          # tf.lite.experimental.nn.TFLiteLSTMCell(
          #     num_units, forget_bias=0, name='rnn{}'.format(i)))
          TFLiteLSTMCell(
              num_units, forget_bias=0, name='rnn{}'.format(i)))
    lstm_layers = tf.keras.layers.StackedRNNCells(lstm_cells)
    # Assume the input is sized as [batch, time, input_size], then we're going
    # to transpose to be time-majored.
    transposed_inputs = tf.transpose(
        inputs, perm=[1, 0, 2])
    # outputs, _ = tf.lite.experimental.nn.dynamic_rnn(
    outputs, _ = dynamic_rnn(
        lstm_layers,
        transposed_inputs,
        dtype='float32',
        time_major=True)
    unstacked_outputs = tf.unstack(outputs, axis=0)
    return unstacked_outputs[-1]
  
  # tf.reset_default_graph()
  model = tf.keras.models.Sequential([
    tf.keras.layers.Input(shape=(28, 28), name='input'),
    tf.keras.layers.Lambda(buildLstmLayer, arguments={'num_layers' : 2, 'num_units' : 64}),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax, name='output')
  ])
  model.compile(optimizer='adam',
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])
  model.summary()
  
  # Step 2: Train &amp; Evaluate the model.
  (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
  x_train, x_test = x_train / 255.0, x_test / 255.0
  
  # Cast x_train &amp; x_test to float32.
  x_train = x_train.astype(np.float32)
  x_test = x_test.astype(np.float32)
  
  model.fit(x_train, y_train, epochs=5)
  model.evaluate(x_test, y_test)
  
  
  # Step 3: Convert the Keras model to TensorFlow Lite model.
  sess = tf.keras.backend.get_session()
  input_tensor = sess.graph.get_tensor_by_name('input:0')
  output_tensor = sess.graph.get_tensor_by_name('output/Softmax:0')
  converter = tf.lite.TFLiteConverter.from_session(
      sess, [input_tensor], [output_tensor])
  tflite = converter.convert()
  print('Model converted successfully!')
  
  # Step 4: Check the converted TensorFlow Lite model.
  interpreter = tf.lite.Interpreter(model_content=tflite)
  
  try:
    interpreter.allocate_tensors()
  except ValueError:
    assert False
  
  MINI_BATCH_SIZE = 1
  correct_case = 0
  for i in range(len(x_test)):
    input_index = (interpreter.get_input_details()[0]['index'])
    interpreter.set_tensor(input_index, x_test[i * MINI_BATCH_SIZE: (i + 1) * MINI_BATCH_SIZE])
    interpreter.invoke()
    output_index = (interpreter.get_output_details()[0]['index'])
    result = interpreter.get_tensor(output_index)
    # Reset all variables so it will not pollute other inferences.
    interpreter.reset_all_variables()
    # Evaluate.
    prediction = np.argmax(result)
    if prediction == y_test[i]:
      correct_case += 1
  
  print('TensorFlow Lite Evaluation result is {}'.format(correct_case * 1.0 / len(x_test)))
  </code></pre>
  
  <p>I continue to get errors</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/variable_scope.py", line 2346, in _enter_scope_uncached
      entered_pure_variable_scope = pure_variable_scope.__enter__()
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/variable_scope.py", line 1912, in __enter__
      constraint=self._constraint)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/variable_scope.py", line 1073, in __init__
      raise NotImplementedError("Caching devices is not yet supported "
  NotImplementedError: Caching devices is not yet supported when eager execution is enabled.
  </code></pre>
  
  <p>and after disabling the eager execution using <code>tf.compat.v1.disable_eager_execution()</code> I get the error</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/xyz/genesis-dnn-se/TFLite_example.py", line 46, in &lt;module&gt;
      tf.keras.layers.Dense(10, activation=tf.nn.softmax, name='output')
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
      result = method(self, *args, **kwargs)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/sequential.py", line 114, in __init__
      self.add(layer)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
      result = method(self, *args, **kwargs)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/sequential.py", line 196, in add
      output_tensor = layer(self.outputs[0])
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 847, in __call__
      outputs = call_fn(cast_inputs, *args, **kwargs)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/core.py", line 795, in call
      return self.function(inputs, **arguments)
    File "/home/xyz/genesis-dnn-se/TFLite_example.py", line 37, in buildLstmLayer
      time_major=True)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/lite/experimental/examples/lstm/rnn.py", line 266, in dynamic_rnn
      dtype=dtype)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/rnn.py", line 916, in _dynamic_rnn_loop
      swap_memory=swap_memory)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/control_flow_ops.py", line 2675, in while_loop
      back_prop=back_prop)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/while_v2.py", line 198, in while_loop
      add_control_dependencies=add_control_dependencies)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 915, in func_graph_from_py_func
      func_outputs = python_func(*func_args, **func_kwargs)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/while_v2.py", line 176, in wrapped_body
      outputs = body(*_pack_sequence_as(orig_loop_vars, args))
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/rnn.py", line 884, in _time_step
      (output, new_state) = call_cell()
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/rnn.py", line 870, in &lt;lambda&gt;
      call_cell = lambda: cell(input_t, state)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 847, in __call__
      outputs = call_fn(cast_inputs, *args, **kwargs)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/recurrent.py", line 137, in call
      inputs, states = cell.call(inputs, states, **kwargs)
    File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/lite/experimental/examples/lstm/rnn_cell.py", line 440, in call
      if input_size.value is None:
  AttributeError: 'int' object has no attribute 'value'
  </code></pre>
  
  <p>Does anyone have a working example of converting a RNN (LSTM, GRU, CustomRNN) to TFLite in TensorFlow 2.0. And I am using TF version 2.0.0.</p>