TFLite SPARSE_TO_DENSE dimension mismatch issue when doing prediction
Hi @Lixiaoyun1993 

Could you please provide the tflite model to reproduce the issue? 

Also, can you please test in latest 2.12 version and let us know if the issue still persists?

Thanks. This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.
### 1. System information

- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Jupyter notebook on a server, CPU run
- TensorFlow installation (pip package or built from source): 
- TensorFlow library (version, if pip package or github SHA, if built from source):  2.4.0

### 2. Code

```
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_path)

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS, 
  tf.lite.OpsSet.SELECT_TF_OPS 
]
tflite_quant_model = converter.convert()

with open("dynamic_range_int8_model.tflite", 'wb') as f:
  f.write(tflite_quant_model)

interpreter = tf.lite.Interpreter('dynamic_range_int8_model.tflite')
interpreter.allocate_tensors()

predictions = []     # inference using tflite model
output_index = interpreter.get_output_details()[0]["index"]

for i in range(len(data_list)):
    for required_input in interpreter.get_input_details():
        input_index = required_input["index"]
        print(input_index, required_input["name"], required_input["shape"], data_list[i][required_input["name"]].shape)
        interpreter.resize_tensor_input(input_index, data_list[i][required_input["name"]].shape, strict = False)
        interpreter.allocate_tensors()
        interpreter.set_tensor(input_index, data_list[i][required_input["name"]])
        
    interpreter.invoke()
    predictions.append(interpreter.get_tensor(output_index)[0][0])
```


Remark: data_list is the list of n data samples, each being a dict. Here I just want to loop over every data point and record the predictions. The inner loop is over the 23 * 3 = 69 model input tensors.

### 3. Failure after conversion

SPARSE_TP_DENSE produces some strange error.


### 5. (optional) Any other info / logs

Hi, I'm trying to do inference using TFLite model in a jupyter notebook. I have successfully converted the model to 'dynamic_range_int8_model.tflite'. However, when I use the model to do prediction using the above code, I get the following error with the interpreter.allocate_tensors() function:

RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_255/4027393990.py in <module>
     16         # else:
     17         interpreter.resize_tensor_input(input_index, data_list[i][required_input["name"]].shape, strict = False)
---> 18         interpreter.allocate_tensors()
     19         interpreter.set_tensor(input_index, data_list[i][required_input["name"]])
     20 

/export/apps/python/3.7/lib/python3.7/site-packages-custom/tensorflow/lite/python/interpreter.py in allocate_tensors(self)
    257   def allocate_tensors(self):
    258     self._ensure_safe()
--> 259     return self._interpreter.AllocateTensors()
    260 
    261   def _safe_to_run(self):

RuntimeError: tensorflow/lite/kernels/sparse_to_dense.cc:66 SizeOfDimension(indices, 1) != NumElements(output_shape) (2 != 3)Node number 0 (SPARSE_TO_DENSE) failed to prepare.


I will describe the data first. The data contains 23 features in sparse form. The original data is something like: 'Feature 1' : {'index0': [0,1] , 'index1': [2,4] , 'values': [1.34, 0.97] }, 'Feature 2' : {'index0': [1,4,5] , 'index1': [1,0,2] , 'values': [5, 2, 1.5]} ....

My code is to first reshape each TFLite input tensor to the correct size of the model input, according to the last reply of [this issue](https://github.com/tensorflow/tensorflow/issues/42157). The strange thing is that, while I have many input features (interpreter.get_input_details() gives a dict of size 69 = 23 * 3), the for loop goes through until the 42th tensor. However, all the input tensors are generated in the same way (by splitting features into indices, shapes, and values), and I can check that all the 42 input tensors (including the 42th one) are successfully reshaped to the correct input shape (for example. the shape of [1, 2] is reshaped to (35, 2), etc). 

I have transformed the data into the required format of TFlite: "serving_default_"+feature_name + "_indices:0", "serving_default_"+feature_name + "_shape:0", "serving_default_"+feature_name + "_values:0"

I can see that the tensor is reshaped. Here is the result of interpreter.get_input_details()[42] before reshaping

{'name': 'serving_default_XXXXX/indices:0',
 'index': 42,
 'shape': array([1, 3], dtype=int32),
 'shape_signature': array([-1,  3], dtype=int32),
 'dtype': numpy.int64,
 'quantization': (0.0, 0),
 'quantization_parameters': {'scales': array([], dtype=float32),
  'zero_points': array([], dtype=int32),
  'quantized_dimension': 0},
 'sparsity_parameters': {}}

After the 'resize_tensor_input' function (this line in the code went through since the error is on allocateTensor() ):

{'name': 'serving_default_XXXXX/indices:0',
 'index': 42,
 'shape': array([1, 2], dtype=int32),
 'shape_signature': array([-1,  3], dtype=int32),
 'dtype': numpy.int64,
 'quantization': (0.0, 0),
 'quantization_parameters': {'scales': array([], dtype=float32),
  'zero_points': array([], dtype=int32),
  'quantized_dimension': 0},
 'sparsity_parameters': {}}

See the 'shape' is changed from 3 to 2.... The error says SizeOfDimension(indices, 1) != NumElements(output_shape) (2 != 3), what is the 'output_shape' how can I change it? 

Thanks very much for your help and suggestion. Please let me know if more information is needed.