# Problem with quantization of GRU model

**URL:** <https://discuss.tinyml.seas.harvard.edu/t/problem-with-quantization-of-gru-model/759>\
**Category:** Programming Q&A\
**Created:** [June 20, 2021, 5:45pm UTC](https://discuss.tinyml.seas.harvard.edu/t/problem-with-quantization-of-gru-model/759 "2021-06-20T17:45:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yahia\_ma](https://avatars.discourse-cdn.com/v4/letter/y/f0a364/32.png) [@yahia\_ma](https://discuss.tinyml.seas.harvard.edu/u/yahia_ma)\
**Post date:** [June 20, 2021, 5:45pm UTC](https://discuss.tinyml.seas.harvard.edu/t/problem-with-quantization-of-gru-model/759/1 "2021-06-20T17:45:33Z")

</div>

hii TinyML community,

## After a long search in other forums for my problem, any solution fund won’t work for me. So, I remember my membership in Tiny Machine Learning Community, I hope that you can help me to overcome this problem.

## the problem is while doing **post-training integer quantization of a GRU model** , it gives me the following error :

ValueError: Failed to parse the model: pybind11::init(): factory function returned nullptr.

 ![quantization problem](https://canada1.discourse-cdn.com/flex027/uploads/tinyml1/original/1X/e635f8fa7888338d5ec7ea4b5ad39f938888f1b5.png)

I think the problem is with my representative\_dataset function.

info :  
the input shape of my model is **(number of samples, time\_steps, features)** (where number of samples = 52000, time\_steps = 20 , features = 3)

```auto
# Convert the model to the TensorFlow Lite format without quantization
num_calibration_steps=1

converter = tf.lite.TFLiteConverter.from_saved_model(GRUMODEL_TF)
converter.optimizations = [tf.lite.Optimize.DEFAULT]

# Save the model to disk
#open(MODEL_NO_QUANT_TFLITE, "wb").write(model_no_quant_tflite)

def representative_dataset_gen():
	for sample in XX_data:
	    sample = np.expand_dims(sample.astype(np.float32), axis=0)
	    yield [sample]

# Set the optimization flag.
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# Enforce integer only quantization
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
# Provide a representative dataset to ensure we quantize correctly.
converter.representative_dataset = repr_data_gen
model_tflite = converter.convert()

# Save the model to disk
open(GRUMODEL_TFLITE, "wb").write(model_tflite)

```

---

<div class="post-metadata">

**Author:** ![yogeshwarb404](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.tinyml.seas.harvard.edu/yogeshwarb404/32/49_2.png) [@yogeshwarb404](https://discuss.tinyml.seas.harvard.edu/u/yogeshwarb404)\
**Post date:** [June 28, 2021, 10:24am UTC](https://discuss.tinyml.seas.harvard.edu/t/problem-with-quantization-of-gru-model/759/2 "2021-06-28T10:24:13Z")

</div>

Have you checked [this github issue](https://github.com/tensorflow/tensorflow/issues/44091)? Someone suggested running the model without quantization and apparently it worked for the poster.

---

<div class="post-metadata">

**Author:** ![yahia\_ma](https://avatars.discourse-cdn.com/v4/letter/y/f0a364/32.png) [@yahia\_ma](https://discuss.tinyml.seas.harvard.edu/u/yahia_ma)\
**Post date:** [June 28, 2021, 5:49pm UTC](https://discuss.tinyml.seas.harvard.edu/t/problem-with-quantization-of-gru-model/759/3 "2021-06-28T17:49:05Z")

</div>

thanks @ [yogeshwarb404](https://discuss.tinyml.seas.harvard.edu/u/yogeshwarb404) for your reply,

I have already checked it, but my goal is to compare the performance of the full integer quantized model with the Non-quantized tflite model and check the effect of quantization on the model degradation.

---

<div class="post-metadata">

**Author:** ![yogeshwarb404](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.tinyml.seas.harvard.edu/yogeshwarb404/32/49_2.png) [@yogeshwarb404](https://discuss.tinyml.seas.harvard.edu/u/yogeshwarb404)\
**Post date:** [June 29, 2021, 7:23am UTC](https://discuss.tinyml.seas.harvard.edu/t/problem-with-quantization-of-gru-model/759/4 "2021-06-29T07:23:14Z")

</div>

They’ve also mentioned it working on the nightly version of tensorflow, which is backed up by [this StackOverflow answer](https://stackoverflow.com/a/66748275/8140117). Maybe that could help?
