Home//

All In One: AttributeError: module 'a' has no attribute 'b'

All In One: AttributeError: module 'a' has no attribute 'b'

Minh Vu

By Minh Vu

Updated Nov 02, 2023

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:

AttributeError: module 'keras.backend' has no attribute 'set_session'

  • Solution: Change keras.backend.set_session to tf.compat.v1.keras.backend.set_session.

  • Example:

    before
    import tensorflow as tf import keras.backend as K
    after
    import tensorflow as tf import tensorflow.compat.v1.keras.backend as K

AttributeError: module 'tensorflow' has no attribute 'Session'

  • Solution: Change tf.Session to tf.compat.v1.Session.

  • Example:

    before
    import tensorflow as tf sess = tf.Session()
    after
    import tensorflow as tf sess = tf.compat.v1.Session()

AttributeError: module 'tensorflow' has no attribute 'placeholder'

  • Solution: Change tf.placeholder to tf.compat.v1.placeholder. Sometimes will need to add tf.compat.v1.disable_eager_execution().

  • Example:

    before
    import tensorflow as tf x = tf.placeholder(tf.float32, shape=(None, 784))
    after
    import 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 to tf.compat.v1.get_default_graph.

  • Example:

    before
    import tensorflow as tf graph = tf.get_default_graph()
    after
    import 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: run pip 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.

You can search for other posts at home page.
Minh Vu

Minh Vu

Software Engineer

Hi guys, I'm the author of WiseCode Blog. I mainly work with the Elastic Stack and build AI & Python projects. I also love writing technical articles, hope you guys have good experience reading my blog!