Open khatchad opened 1 month ago
There's a fundamental reason why numpy calls are disallowed; it has to do with eager vs. deferred execution.
An example of making the code amenable to hybridization can be found at:
https://github.com/ponder-lab/tensorflow-yolov4-tflite/tree/hyb_amenable
There are also TF equivalents to numpy calls, e.g.:
diff --git a/tests/keras/test_din.py b/tests/keras/test_din.py
index fda09be..69b2b76 100644
--- a/tests/keras/test_din.py
+++ b/tests/keras/test_din.py
@@ -16,8 +16,8 @@ class TestDIN(tf.test.TestCase, parameterized.TestCase):
def test_activation_unit_noiteract(self):
- x = np.random.normal(size=(3, 5))
- y = np.random.normal(size=(3, 5))
+ x = tf.random.normal(shape=(3, 5))
+ y = tf.random.normal(shape=(3, 5))
activation_unit = din.ActivationUnit(10, kernel_init="ones")
outputs = activation_unit(x, y)
@@ -31,8 +31,8 @@ class TestDIN(tf.test.TestCase, parameterized.TestCase):
def test_activation_unit_iteract(self):
- x = np.random.normal(size=(3, 5))
- y = np.random.normal(size=(3, 5))
+ x = tf.random.normal(shape=(3, 5))
+ y = tf.random.normal(shape=(3, 5))
interacter = tf.keras.layers.Subtract()
And, there's the tf.experimental.numpy
module.
Not only does the function definition itself should not have numpy calls, but transitively there should be no numpy calls.
The numpy API is not directly compatible with
tf.function
. The code needs to be restructured first. A first step to deal with this is to make no numpy calls a refactoring precondition. Otherwise, we can get an error after running the refactored code, e.g.:The code for this example is at;
https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/9f16748aa3f45ff240608da4bd9b1216a29127f5/train.py#L98-L110.
The enclosing function should fail refactoring preconditions.