qkeras icon indicating copy to clipboard operation
qkeras copied to clipboard

QKeras fails due to missing modules and numpy error message with latest TensorFlow version 2.16.1

Open lp247 opened this issue 1 year ago • 2 comments

I have the following code:

#!/usr/bin/env python
# coding: utf-8
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Flatten, Input
from tensorflow.keras.utils import to_categorical
from qkeras.qlayers import QDense
from qkeras.quantizers import quantized_bits

(train_data, train_labels), (test_data, test_labels) = mnist.load_data()
train_data = train_data.astype('float32') / 255.0
test_images = test_data.astype('float32') / 255.0
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model = Sequential([
    Input(name="Input", shape=(28, 28)),
    Flatten(name="Flatten"),
    QDense(32, name="QDense1", kernel_quantizer=quantized_bits(2, 0), bias_quantizer=quantized_bits(2, 0)),
    Activation("softmax", name="Softmax")
])
model.compile()
model.fit(train_data, train_labels)

I installed tensorflow (2.16.1) and QKeras (0.9.0). Trying to run this code gives me ModuleNotFoundError: No module named 'pyparsing'.. After installing that module (3.1.2) I get ModuleNotFoundError: No module named 'tf_keras'.. After installing that (2.16.0) I get the following error:

Traceback (most recent call last):
  File "/home/me/wtf/test.py", line 22, in <module>
    model.fit(train_data, train_labels)
  File "/home/me/.pyenv/versions/3.11.8/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/me/.pyenv/versions/3.11.8/lib/python3.11/site-packages/qkeras/qlayers.py", line 629, in call
    quantized_kernel = self.kernel_quantizer_internal(self.kernel)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/me/.pyenv/versions/3.11.8/lib/python3.11/site-packages/qkeras/quantizers.py", line 558, in __call__
    x = K.cast_to_floatx(x)
        ^^^^^^^^^^^^^^^^^^^
NotImplementedError: Exception encountered when calling QDense.call().

numpy() is only available when eager execution is enabled.

Arguments received by QDense.call():
  • inputs=tf.Tensor(shape=(32, 784), dtype=float32)

Using python 3.11.8 with pip 24.0 on Ubuntu 22.04.4 LTS.

Note: It is working with tensorflow==2.15.1 and tf_keras==2.15.1.

lp247 avatar Mar 27 '24 20:03 lp247