Ask questions[RNN] [TFLiteConverter.] Input tensors containing unknown dimensions fails when coupled with LSTM

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux-4.19.104+-x86_64-with-Ubuntu-18.04-bionic (Google Colab's default environment)
  • TensorFlow installed from (source or binary): pip install tf-nightly
  • TensorFlow version (or github SHA if from source): 2.2.0-dev20200421

Command used to run the converter or code if you’re using the Python API If possible, please share a link to Colab/Jupyter/any notebook.

from tensorflow.lite.python import lite
from tensorflow.python import keras
import numpy as np

input_a = keras.layers.Input(shape=(3,3,), name='input_a')
interm_b = tf.keras.layers.LSTM(4, name='interm_1')(input_a)
output_c = keras.layers.Dense(1, name='dense_1')(interm_b)

model = tf.keras.models.Model(inputs=[input_a], outputs=[output_c])
model.compile(optimizer='sgd', loss='mean_squared_error')

batch_size = 10
sample_input = np.ones((batch_size,3,3),dtype=np.float32)

expected_value = model.predict(sample_input)

converter = lite.TFLiteConverterV2.from_keras_model(model = model)
converter.experimental_new_converter = True
with open("model.tflite", "wb") as f:

interpreter = lite.Interpreter(model_path="model.tflite")
interpreter.resize_tensor_input(0,[batch_size, 3,3])
interpreter.set_tensor(0, sample_input)

The output from the converter invocation

Model: "model_2"
Layer (type)                 Output Shape              Param #   
input_a (InputLayer)         [(None, 3, 3)]            0         
interm_1 (LSTM)              (None, 4)                 128       
dense_1 (Dense)              (None, 1)                 5         
Total params: 133
Trainable params: 133
Non-trainable params: 0
[{'name': 'input_a', 'index': 0, 'shape': array([1, 3, 3], dtype=int32), 'shape_signature': array([-1,  3,  3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-492497848c68> in <module>()
     27 interpreter.allocate_tensors()
     28 interpreter.set_tensor(0, sample_input)
---> 29 interpreter.invoke()
     30 interpreter.get_tensor(interpreter.get_output_details()[0]["index"])

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/ in invoke(self)
    512     """
    513     self._ensure_safe()
--> 514     self._interpreter.Invoke()
    516   def reset_all_variables(self):

RuntimeError: tensorflow/lite/kernels/ t->dims->data[d] != t0->dims->data[d] (10 != 1)Node number 50 (CONCATENATION) failed to prepare.
Node number 10 (WHILE) failed to invoke.

Failure details The conversion is successful, but the generated model cannot be resized to variable batch size. Input tensors containing unknown dimensions fails when coupled with LSTM Same script would work just fine if one removes creation of interm_b and pass input_a as the input to generate the output_c.


Answer questions renjie-liu

our fused ops will need the dimensions to be known because the states are maintained inside the kernel. so it is wai. :(


Related questions

ModuleNotFoundError: No module named 'tensorflow.contrib' hot 9
Tf.Keras metrics issue hot 8
Error occurred when finalizing GeneratorDataset iterator hot 7
Error loading tensorflow hot 6
module 'tensorflow' has no attribute 'ConfigProto' hot 6
TF 2.0 'Tensor' object has no attribute 'numpy' while using .numpy() although eager execution enabled by default hot 6
tensorflow-gpu CUPTI errors
Lossy conversion from float32 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.
ModuleNotFoundError: No module named 'tensorflow.contrib'
When importing TensorFlow, error loading Hadoop
OSError: SavedModel file does not exist at: saved_model_dir/{saved_model.pbtxt|saved_model.pb}
AttributeError: module &#39;tensorflow.python.framework.op_def_registry&#39; has no attribute &#39;register_op_list&#39;
tf.keras.layers.Conv1DTranspose ?
[TF 2.0] tf.keras.optimizers.Adam hot 4
TF2.0 AutoGraph issue hot 4
Github User Rank List