解决AttributeError: module ‘tensorflow’ has no attribute ‘Session’
Problem Description:
TensorFlow version is 2.8.0, execute the following code:
sess = tf.Session()
The error is reported as follows:
AttributeError: module 'tensorflow' has no attribute 'Session'
Cause Analysis:
The module has been removed from TensorFlow 2.0 and above Session
.
solution:
tf.compat.v1.Session()
Just replace with tf.Session()
.
# sess = tf.Session()
sess = tf.compat.v1.Session()
If errors are reported in many parts of the code due to version issues, you can change to a lower version of TensorFlow and install it with pip.
pip uninstall tensorflow
pip install tensorflow==1.14