This tutorial, as its name, will show you common AttributeError: module has no attribute errors and how to fix them.
You can use Ctrl
+ F
to search for the error you are facing.
For your reference:
Table of Contents
- AttributeError: module 'keras.backend' has no attribute 'set_session'
- AttributeError: module 'tensorflow' has no attribute 'Session'
- AttributeError: module 'tensorflow' has no attribute 'placeholder'
- AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
- AttributeError: module 'torch' has no attribute '_six'
AttributeError: module 'keras.backend' has no attribute 'set_session'
-
Solution: Change
keras.backend.set_session
totf.compat.v1.keras.backend.set_session
. -
Example:
beforeimport tensorflow as tf import keras.backend as K
afterimport tensorflow as tf import tensorflow.compat.v1.keras.backend as K
AttributeError: module 'tensorflow' has no attribute 'Session'
-
Solution: Change
tf.Session
totf.compat.v1.Session
. -
Example:
beforeimport tensorflow as tf sess = tf.Session()
afterimport tensorflow as tf sess = tf.compat.v1.Session()
AttributeError: module 'tensorflow' has no attribute 'placeholder'
-
Solution: Change
tf.placeholder
totf.compat.v1.placeholder
. Sometimes will need to addtf.compat.v1.disable_eager_execution()
. -
Example:
beforeimport tensorflow as tf x = tf.placeholder(tf.float32, shape=(None, 784))
afterimport tensorflow as tf tf.compat.v1.disable_eager_execution() x = tf.compat.v1.placeholder(tf.float32, shape=(None, 784))
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
-
Solution: Change
tf.get_default_graph
totf.compat.v1.get_default_graph
. -
Example:
beforeimport tensorflow as tf graph = tf.get_default_graph()
afterimport tensorflow as tf graph = tf.compat.v1.get_default_graph()
AttributeError: module 'torch' has no attribute '_six'
- Solution: Update
torch
to the latest version and restart the notebook kernel. - To update
torch
: runpip install --upgrade torch torchvision
.
Conclusion
Thank you for reading. I will try to put a search bar so that you can search through the errors better.