Skip to content

Commit 461e3e3

Browse files
liyunlu0618tensorflower-gardener
authored andcommitted
Converter API change to enable optimization with sparsity.
To enable optimization with sparsity, use: converter.optimizations = [tf.lite.Optimize.SPARSITY] converter.convert() Note: 1) This feature is experimental 2) It requires the use of during training pruning to be effective. 3) Not all kernels have been optimized for sparse execution, so the initial benefit will primarily be in the model size on disk. PiperOrigin-RevId: 351245576
1 parent 80ccd9a commit 461e3e3

File tree

1 file changed

+3
-4
lines changed
  • tensorflow_model_optimization/python/examples/sparsity/keras/mnist

1 file changed

+3
-4
lines changed

tensorflow_model_optimization/python/examples/sparsity/keras/mnist/mnist_e2e.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ def main(unused_argv):
119119
with open(tflite_model_path, 'wb') as f:
120120
f.write(tflite_model_dense)
121121

122-
# The _experimental_sparsify_model is to enable sparse tensor encoding,
123-
# otherwise the model is converted as dense.
124-
converter._experimental_sparsify_model = True
122+
# Enable sparse tensor encoding, otherwise the model is converted as dense.
123+
converter.optimizations = {tf.lite.Optimize.EXPERIMENTAL_SPARSITY}
125124

126125
tflite_model = converter.convert()
127126

@@ -152,7 +151,7 @@ def main(unused_argv):
152151
model = train(model, x_train, y_train, x_test, y_test)
153152

154153
converter = tf.lite.TFLiteConverter.from_keras_model(model)
155-
converter._experimental_sparsify_model = True
154+
converter.optimizations = {tf.lite.Optimize.EXPERIMENTAL_SPARSITY}
156155

157156
tflite_model = converter.convert()
158157
# Check the model is compressed

0 commit comments

Comments
 (0)