google / google/qkeras

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

Open
#130 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
584
Forks
108
PR merge metrics
No merged PRs in 30d

Description

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
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.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example with TensorFlow 2.16.1, QKeras 0.9.0, and tf_keras 2.16.0, then inspect qkeras/qlayers.py around QDense.call and qkeras/quantizers.py around the quantizer call. Compare this behavior with the reported working TensorFlow and tf_keras 2.15.1 setup. Done means the example runs without missing-module errors or the eager-execution/numpy error on the newer versions, with regression coverage for the compatibility case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.