I have the following structure on my code:
src
|_subdir_1
|_ main.py
|_subdir_2
|_ utils.py
There is also an empty __init__.py file in each directory
main.py is defined as follows:
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from subdir_2.utils import load_mnist, create_model
(x_train, y_train), (x_test, y_test) = load_mnist()
model = create_model()
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
I've appended the path to src to the system path because of an error when I tried doing a relative import, but that's another problem utils.py is defined as follows:
import tensorflow as tf
@tf.autograph.experimental.do_not_convert
def load_mnist():
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
return (x_train, y_train), (x_test, y_test)
@tf.autograph.experimental.do_not_convert
def create_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
the @tf.autograph.experimental.do_not_convert decorator was added as an attempt to solve the issue, but with or without it the error persists. When I run in python the code works normally, but when I try to create an executable file with nuitka I get the following error:
Traceback (most recent call last):
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\pyct\parser.py", line 148, in parse_entity
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\pyct\inspect_utils.py", line 124, in getimmediatesource
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\inspect.py", line 835, in findsource
OSError: could not get source code
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\impl\api.py", line 427, in converted_call
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\impl\api.py", line 269, in _convert_actual
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\pyct\transpiler.py", line 282, in transform
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\pyct\transpiler.py", line 466, in transform_function
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\pyct\transpiler.py", line 342, in transform_function
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\pyct\parser.py", line 150, in parse_entity
tensorflow.python.autograph.pyct.errors.InaccessibleSourceCodeError: Unable to locate the source code of <compiled_function Model.make_train_function.<locals>.trai
n_function at 0x000001A4790A2480>. Note that functions defined in certain environments, like the interactive Python shell, do not expose their source code. If that
is the case, you should define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.experimental.do_not_c
onvert. Original error: could not get source code
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\util\traceback_utils.py", line 150, in error_handler
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\eager\polymorphic_function\polymorphic_function.py", line 880, in __call__
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\eager\polymorphic_function\polymorphic_function.py", line 928, in _call
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\eager\polymorphic_function\polymorphic_function.py", line 749, in _initialize
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\eager\polymorphic_function\tracing_compiler.py", line 162, in _get_concrete_function_internal_
garbage_collected
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\eager\polymorphic_function\tracing_compiler.py", line 157, in _maybe_define_concrete_function
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\eager\polymorphic_function\tracing_compiler.py", line 360, in _maybe_define_function
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\eager\polymorphic_function\tracing_compiler.py", line 284, in _create_concrete_function
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\framework\func_graph.py", line 1283, in func_graph_from_py_func
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\eager\polymorphic_function\polymorphic_function.py", line 645, in wrapped_fn
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\framework\func_graph.py", line 1258, in autograph_handler
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\impl\api.py", line 434, in converted_call
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\impl\api.py", line 484, in _fall_back_unconverted
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\impl\api.py", line 458, in _call_unconverted
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\engine\training.py", line 1160, in train_function
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\engine\training.py", line 1146, in step_function
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\distribute\distribute_lib.py", line 1316, in run
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\distribute\distribute_lib.py", line 2895, in call_for_each_replica
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\distribute\distribute_lib.py", line 3696, in _call_for_each_replica
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\impl\api.py", line 689, in wrapper
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\impl\api.py", line 377, in converted_call
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\autograph\impl\api.py", line 458, in _call_unconverted
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\engine\training.py", line 1135, in run_step
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\engine\training.py", line 997, in train_step
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\optimizers\optimizer_v2\optimizer_v2.py", line 579, in minimize
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\optimizers\optimizer_v2\optimizer_v2.py", line 735, in apply_gradients
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\optimizers\optimizer_v2\optimizer_v2.py", line 530, in _aggregate_gradients
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\optimizers\optimizer_v2\utils.py", line 35, in all_reduce_sum_gradients
AttributeError: 'NoneType' object has no attribute 'strategy_supports_no_merge_call'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\utils\traceback_utils.py", line 65, in error_handler
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\engine\training.py", line 1564, in fit
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\util\traceback_utils.py", line 152, in error_handler
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\tensorflow\python\util\traceback_utils.py", line 111, in _process_traceback_frames
SystemError: \python\traceback.c:43: bad argument to internal function
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\main.py", line 7, in <module>
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\utils\traceback_utils.py", line 67, in error_handler
File "C:\Users\lfbjc\AppData\Local\Temp\ONEFIL~1\keras\utils\traceback_utils.py", line 49, in _process_traceback_frames
SystemError: \python\traceback.c:43: bad argument to internal function
for creating the executable I entered the src folder and ran the following command
nuitka --include_package=subdir_2 --onefile --follow-imports subdir_1/main.py