Tensorflow 2.11 error: AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'
Question:
I had to update Tensorflow to the currently latest version 2.11. when importing i get "AttributeError: module ‘tensorflow._api.v2.compat.v2.internal‘ has no attribute ‘register_load_context_function’". I have also completely reinstalled a full anaconda environment and downgraded Python to the version compatible with the latest of Tensorflow and then "pip3 install Tensorflow==2.11". Got the same error. I have no other ideas.
The full error log is the following
import tensorflow as tf
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~AppDataLocalTempipykernel_4323752927832.py in <module>
----> 1 import tensorflow as tf
~AppDataRoamingPythonPython310site-packagestensorflow__init__.py in <module>
467 if hasattr(_current_module, "keras"):
468 try:
--> 469 _keras._load()
470 except ImportError:
471 pass
~AppDataRoamingPythonPython310site-packagestensorflowpythonutillazy_loader.py in _load(self)
39 """Load the module and insert it into the parent's globals."""
40 # Import the target module and insert it into the parent's namespace
---> 41 module = importlib.import_module(self.__name__)
42 self._parent_module_globals[self._local_name] = module
43
~anaconda3envsmltrade2libimportlib__init__.py in import_module(name, package)
124 break
125 level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)
127
128
~anaconda3envsmltrade2libsite-packageskeras__init__.py in <module>
19 """
20 from keras import distribute
---> 21 from keras import models
22 from keras.engine.input_layer import Input
23 from keras.engine.sequential import Sequential
~anaconda3envsmltrade2libsite-packageskerasmodels__init__.py in <module>
16
17
---> 18 from keras.engine.functional import Functional
19 from keras.engine.sequential import Sequential
20 from keras.engine.training import Model
~anaconda3envsmltrade2libsite-packageskerasenginefunctional.py in <module>
32 from keras.engine import input_spec
33 from keras.engine import node as node_module
---> 34 from keras.engine import training as training_lib
35 from keras.engine import training_utils
36 from keras.saving.legacy import serialization
~anaconda3envsmltrade2libsite-packageskerasenginetraining.py in <module>
43 from keras.saving.experimental import saving_lib
44 from keras.saving.legacy import hdf5_format
---> 45 from keras.saving.legacy import save
46 from keras.saving.legacy import saving_utils
47 from keras.saving.legacy import serialization
~anaconda3envsmltrade2libsite-packageskerassavinglegacysave.py in <module>
22 from keras.saving.legacy import serialization
23 from keras.saving.legacy.saved_model import load as saved_model_load
---> 24 from keras.saving.legacy.saved_model import load_context
25 from keras.saving.legacy.saved_model import save as saved_model_save
26 from keras.utils import traceback_utils
~anaconda3envsmltrade2libsite-packageskerassavinglegacysaved_modelload_context.py in <module>
66
67
---> 68 tf.__internal__.register_load_context_function(in_load_context)
AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'
Answers:
It seems TensorFlow has not installed in your system properly. Please verify if you have followed the step by step instructions mentioned in this link to install TensorFlow as per your system OS.
conda create --name tf #Create a new conda environment named tf
conda activate tf #activate the tf environment
pip install --upgrade pip # upgrade the pip
pip install tensorflow # install tensorflow using pip
Verify if the tensorflow installed properly:
python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
NOTE: Tensorflow 2.11
is not supported on the GPU on Windows Native.
TensorFlow 2.10 was the last TensorFlow release that supported GPU on
native-Windows. Starting with TensorFlow 2.11, you will need to
install TensorFlow in WSL2, or install tensorflow-cpu and, optionally,
try the TensorFlow-DirectML-Plugin
Rebuilding a new Anaconda environment and reinstalling Tensorflow was not enough, the problem has been fixed also creating a new Jupyter kernel. Somehow the new installation of Tensroflow was not synchronized with the kernel under use. Hope this will help others experiencing the same frustration in the future.
I had to update Tensorflow to the currently latest version 2.11. when importing i get "AttributeError: module ‘tensorflow._api.v2.compat.v2.internal‘ has no attribute ‘register_load_context_function’". I have also completely reinstalled a full anaconda environment and downgraded Python to the version compatible with the latest of Tensorflow and then "pip3 install Tensorflow==2.11". Got the same error. I have no other ideas.
The full error log is the following
import tensorflow as tf
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~AppDataLocalTempipykernel_4323752927832.py in <module>
----> 1 import tensorflow as tf
~AppDataRoamingPythonPython310site-packagestensorflow__init__.py in <module>
467 if hasattr(_current_module, "keras"):
468 try:
--> 469 _keras._load()
470 except ImportError:
471 pass
~AppDataRoamingPythonPython310site-packagestensorflowpythonutillazy_loader.py in _load(self)
39 """Load the module and insert it into the parent's globals."""
40 # Import the target module and insert it into the parent's namespace
---> 41 module = importlib.import_module(self.__name__)
42 self._parent_module_globals[self._local_name] = module
43
~anaconda3envsmltrade2libimportlib__init__.py in import_module(name, package)
124 break
125 level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)
127
128
~anaconda3envsmltrade2libsite-packageskeras__init__.py in <module>
19 """
20 from keras import distribute
---> 21 from keras import models
22 from keras.engine.input_layer import Input
23 from keras.engine.sequential import Sequential
~anaconda3envsmltrade2libsite-packageskerasmodels__init__.py in <module>
16
17
---> 18 from keras.engine.functional import Functional
19 from keras.engine.sequential import Sequential
20 from keras.engine.training import Model
~anaconda3envsmltrade2libsite-packageskerasenginefunctional.py in <module>
32 from keras.engine import input_spec
33 from keras.engine import node as node_module
---> 34 from keras.engine import training as training_lib
35 from keras.engine import training_utils
36 from keras.saving.legacy import serialization
~anaconda3envsmltrade2libsite-packageskerasenginetraining.py in <module>
43 from keras.saving.experimental import saving_lib
44 from keras.saving.legacy import hdf5_format
---> 45 from keras.saving.legacy import save
46 from keras.saving.legacy import saving_utils
47 from keras.saving.legacy import serialization
~anaconda3envsmltrade2libsite-packageskerassavinglegacysave.py in <module>
22 from keras.saving.legacy import serialization
23 from keras.saving.legacy.saved_model import load as saved_model_load
---> 24 from keras.saving.legacy.saved_model import load_context
25 from keras.saving.legacy.saved_model import save as saved_model_save
26 from keras.utils import traceback_utils
~anaconda3envsmltrade2libsite-packageskerassavinglegacysaved_modelload_context.py in <module>
66
67
---> 68 tf.__internal__.register_load_context_function(in_load_context)
AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'
It seems TensorFlow has not installed in your system properly. Please verify if you have followed the step by step instructions mentioned in this link to install TensorFlow as per your system OS.
conda create --name tf #Create a new conda environment named tf
conda activate tf #activate the tf environment
pip install --upgrade pip # upgrade the pip
pip install tensorflow # install tensorflow using pip
Verify if the tensorflow installed properly:
python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
NOTE: Tensorflow 2.11
is not supported on the GPU on Windows Native.
TensorFlow 2.10 was the last TensorFlow release that supported GPU on
native-Windows. Starting with TensorFlow 2.11, you will need to
install TensorFlow in WSL2, or install tensorflow-cpu and, optionally,
try the TensorFlow-DirectML-Plugin
Rebuilding a new Anaconda environment and reinstalling Tensorflow was not enough, the problem has been fixed also creating a new Jupyter kernel. Somehow the new installation of Tensroflow was not synchronized with the kernel under use. Hope this will help others experiencing the same frustration in the future.