Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update quantizers file #124

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions tests/autoqkeras_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
# TODO: Update to new optimizer API
from tensorflow.keras.optimizers.legacy import Adam
from tensorflow.keras.utils import to_categorical

from qkeras.autoqkeras import AutoQKerasScheduler

np.random.seed(42)
tf.random.set_seed(42)
tf.config.experimental.enable_op_determinism()


def dense_model():
"""Creates test dense model."""
Expand All @@ -52,13 +57,20 @@ def dense_model():
x = Activation("softmax", name="softmax")(x)

model = Model(inputs=x_in, outputs=x)
return model

# Manually set the weights for each layer. Needed for test determinism.
for layer in model.layers:
if isinstance(layer, Dense):
weights_shape = layer.get_weights()[0].shape
bias_shape = layer.get_weights()[1].shape
weights = np.random.RandomState(42).randn(*weights_shape)
bias = np.random.RandomState(42).randn(*bias_shape)
layer.set_weights([weights, bias])

return model

def test_autoqkeras():
"""Tests AutoQKeras scheduler."""
np.random.seed(42)
tf.random.set_seed(42)

x_train, y_train = load_iris(return_X_y=True)

Expand Down Expand Up @@ -104,7 +116,7 @@ def test_autoqkeras():

model = dense_model()
model.summary()
optimizer = Adam(lr=0.01)
optimizer = Adam(learning_rate=0.015)
model.compile(optimizer=optimizer, loss="categorical_crossentropy",
metrics=["acc"])

Expand Down Expand Up @@ -140,14 +152,12 @@ def test_autoqkeras():

qmodel = autoqk.get_best_model()

optimizer = Adam(lr=0.01)
optimizer = Adam(learning_rate=0.015)
qmodel.compile(optimizer=optimizer, loss="categorical_crossentropy",
metrics=["acc"])
history = qmodel.fit(x_train, y_train, epochs=5, batch_size=150,
_ = qmodel.fit(x_train, y_train, epochs=5, batch_size=150,
validation_split=0.1)

quantized_acc = history.history["acc"][-1]

if __name__ == "__main__":
pytest.main([__file__])