[AttributeError] module ‘tensorflow’ has no attribute ‘Session’
When I was trying the exercise of TensorFlow Programming Concepts, an error “AttributeError module ‘tensorflow’ has no attribute ‘Session'” occurred.
Error Detail
The code run in Google Colaboratory is the following.Generic HighlightingEnlighterJS Syntax Highlighter
Cause
tf.Session can be used in tensorflow 1.x version, while it cannot be used in 2.x version.
Condinf style of TensorFlow ver 2.x are different from ver 1.x. Version 2.x is more pythonic than 1.x. Please check “Effective TensorFlow 2” for details.
Checklist
- Check if the version of tensorflow imported is 1.x or not.
Solution
solution 1. Change the version of tensorflow to 1.x
In Google Colaboratory, you can change the version of tensorflow with “%tensorflow_version” as below.Generic HighlightingEnlighterJS Syntax Highlighter
This method doesn’t work after importing tensorflow once, so restart runtime and change the version before import tensorflow.
solution 2. Use tf.compat.v1.Session()
Use tf.compat.v1.Session() instead of tf.Session().
Reference: Migrate your TensorFlow 1 code to TensorFlow 2Generic HighlightingEnlighterJS Syntax Highlighter