Quick start

This page shows a minimal end-to-end example using quantized layers.

Tip

Always set the backend before importing: export KERAS_BACKEND=tensorflow

Minimal quantized model

import tensorflow as tf
from keras import layers, models
from qkeras import QDense, quantized_bits

model = models.Sequential(
    [
        layers.Input(shape=(128,)),
        QDense(
            64,
            activation="relu",
            kernel_quantizer=quantized_bits(8, 0, 1),
            bias_quantizer=quantized_bits(8, 0, 1),
        ),
        QDense(
            10,
            activation="softmax",
            kernel_quantizer=quantized_bits(8, 0, 1),
            bias_quantizer=quantized_bits(8, 0, 1),
        ),
    ]
)

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.summary()

Train briefly

import numpy as np

x = np.random.randn(512, 128).astype("float32")
y = np.random.randint(0, 10, size=(512,), dtype="int32")

model.fit(x, y, epochs=1, batch_size=32)

Next steps

  • Browse the Examples for runnable scripts.

  • Read the Notebooks section for tutorial-style walkthroughs.

  • See the API Reference for the full API reference.