. Advertisement .
..3..
. Advertisement .
..4..
Error “module tensorflow.compat.v2.__internal__ has no attribute tf2” is a common error faced by students while installing and configuring TensorFlow. This article will help you to understand the error in detail and also provide you with a solution to the error.
How To Deal With The Error “module ‘tensorflow.compat.v2.__internal__’ has no attribute ‘tf2’”?
Today, I’m attempting to use TensorFlow as a backend. Yesterday, I was able to do so, but today when I attempt to import Keras, I get an error message. It displays the following error:
<ipython-input-3-9c78a7be919d> in <module>()
5
6 import cv2
----> 7 import keras
8 import numpy as np
9 import matplotlib.pyplot as plt
8 frames
/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py in populate_deserializable_objects()
47
48 LOCAL.ALL_OBJECTS = {}
---> 49 LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
50
51 # Compatibility aliases (need to exist in both V1 and V2).
AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'
The reason for not displaying the TensorFlow backend is that in previous versions of Keras, there were three backed, one of which was tf (AFAIK), however after tf 2.x, there is just one backed, which is tf. That’s why the message is now obsolete.
Our Approach
Instead of what you’re doing now, try this workaround.
!pip install -U -q segmentation-models
!pip install -q tensorflow==2.1
!pip install -q keras==2.3.1
!pip install -q tensorflow-estimator==2.1.
## Imports libs
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ["SM_FRAMEWORK"] = "tf.keras"
from tensorflow import keras
import segmentation_models as sm
Conclusion
We hope you enjoyed our article about dealing with the error “module ‘tensorflow.compat.v2.__internal__’ has no attribute ‘tf2’”. We are confident that you will make the most of your Java development experience with this information.
If you still have any further concerns or problems, please feel free to leave us comments. Thank you for reading; please make sure to share this helpful article with others!
Leave a comment