Registry /
ai-ml / tensorflow-model-optimization
Install & Compatibility
Where this runs
tested against v0.8.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
build_error
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 36.9s · import 13.520s · 2355.2MB
2355MB installed
● package 2355MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
quantize_model
✓ import tensorflow_model_optimization as tfmot
✗ from tensorflow_model_optimization import quantize_model
Direct import may lead to submodule not found errors; use tfmot.quantization.keras.quantize_model
prune_low_magnitude
✓ from tensorflow_model_optimization.sparsity.keras import prune_low_magnitude
✗ from tfmot.sparsity import prune_low_magnitude
Incorrect alias; use full package path
tfmot
✓ import tensorflow_model_optimization as tfmot
Standard alias, no common mistake observed.
Demonstrates quantization-aware training with a simple model, then converts to TFLite.
import tensorflow as tf
import tensorflow_model_optimization as tfmot
# Quantization aware training example
def get_model():
model = tf.keras.Sequential([
tf.keras.layers.Dense(32, activation='relu', input_shape=(784,)),
tf.keras.layers.Dense(10, activation='softmax')
])
return model
model = get_model()
quantize_model = tfmot.quantization.keras.quantize_model
q_aware_model = quantize_model(model)
# Compile and train
q_aware_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
import numpy as np
train_images = np.random.random((100, 784)).astype('float32')
train_labels = np.random.randint(10, size=(100,))
q_aware_model.fit(train_images, train_labels, epochs=1, verbose=0)
# Convert to TFLite
converter = tf.lite.TFLiteConverter.from_keras_model(q_aware_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
print('TFLite model size:', len(tflite_model))
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'tensorflow_model_optimization.quantization.keras.quantize_model'
The submodule may not be imported correctly; the function is inside a deeper package.
fixUse: from tensorflow_model_optimization.quantization.keras import quantize_model
ValueError: Unknown layer: QuantizeWrapper. Please ensure this layer is imported.
When loading a quantized model, the custom QuantizeWrapper class is not registered.
fixUse: with tfmot.quantization.keras.quantize_scope(): model = tf.keras.models.load_model('path') AttributeError: module 'tensorflow_model_optimization' has no attribute 'sparsity'
Importing tfmot.sparsity directly fails because the package is not fully imported.
fixUse: from tensorflow_model_optimization.sparsity.keras import prune_low_magnitude
Upgrade
Version history
0.8.1latest on PyPI · released May 12, 2026
Audit
Dependencies
tensorflowrequiredCore dependency; the toolkit works with TensorFlow 2.x.