tensorflow / tensorflow/model-optimization

QAT with trainable=False does not work as expected.

Open
#881 7 comments 1 reaction 1 assignee View on GitHub

@fredrec is already working on this.

Since Dec 6, 2021.

bug
Dominant language
Python
Stars
1.6k
Forks
349
Avg merge
3d 2h
Merged PRs (30d)
1

Description

Describe the bug

System information

TensorFlow version (installed from source or binary): 2.6.0

TensorFlow Model Optimization version (installed from source or binary): 0.7.0

Python version: 3.7.10

Describe the expected behavior
After setting trainable=False on layers with a quantisation wrapper applied, the weights in that layer should not change during training.

Describe the current behavior
The loss decreases during training even if all layers are set to be non-trainable.

Code to reproduce the issue

import numpy as np
import tensorflow as tf
import tensorflow_model_optimization as tfmot

A = np.random.uniform(size=(10000, 10, 10))

print('Expected behaviour')
inp = tf.keras.Input(shape=(10, 10), batch_size=10)
out = tf.keras.layers.Dense(10)(inp)
model = tf.keras.Model(inp, out)
for layer in model.layers:
    layer.trainable = False
model.compile(loss='mse')
model.fit(A, A, batch_size=10, epochs=5)
print('{} trainable weights'.format(len(model.layers[1].trainable_weights)))

print('\nQuantised behaviour')
inp = tf.keras.Input(shape=(10, 10), batch_size=10)
out = tfmot.quantization.keras.quantize_annotate_layer(tf.keras.layers.Dense(10))(inp)
quant_model = tfmot.quantization.keras.quantize_apply(tf.keras.Model(inp, out))
for layer in quant_model.layers:
    layer.trainable = False
quant_model.compile(loss='mse')
quant_model.fit(A, A, batch_size=10, epochs=5)
print('{} trainable weights'.format(len(quant_model.layers[2].trainable_weights)))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.