Attributeerror: Module ‘tensorflow’ has no attribute ‘global_variables_initializer’

In this tutorial, you will understand how to fix the error Attributeerror: Module ‘tensorflow’ has no attribute ‘global_variables_initializer’.

In TensorFlow, I initialize the variables using the attribute global_variables_initalizer, but as Tensorflow got upgraded to version 2, you don’t need to use that attribute anymore to initialise variables.

So, if you have also upgraded your tensorflow version, you might get the same error. Here, I will explain how to fix that error using TensorFlow, whether you use TensorFlow version 1.x or 2.x.

Attributeerror: Module ‘tensorflow’ has no attribute ‘global_variables_initializer’

The Tensorflow error Attributeerror: Module ‘tensorflow’ has no attribute ‘global_variables_initializer’, indicating that you are trying to access the attribute global_variable_initializer, which doesn’t exist in the Tensorflow.

Let me show you how I got this error. Run the code below.

import tensorflow as tf

var1 = tf.Variable(4.0)
var2 = tf.Variable(5.0)

init_op = tf.global_variables_initializer()

with tf.Session() as sess:
	sess.run(init_op)
Attributeerror Module 'tensorflow' has no attribute 'global_variables_initializer'

The error appears when you try to access the global_variables_initializer attribute from the tensorflow.

The reason is the version of tensorflow that you are using; the code is compatible with version 1.x, and you are trying to access the attribute using Tensorflow 2.x.

To fix this code, there are two solutions: to use the tf.compat.v1 module and the other is to follow the latest version of TensorFlow API.

Let’s see with tf.compat.v1 module allows you to use the TensorFlow version in the environment of TensorFlow version 2.0.

Import the tensorflow and disable the eager execution using the code below.

import tensorflow as tf
tf.compat.v1.disable_eager_execution()

Define and initialize the variable using the below code.

var1 = tf.Variable(4.0)
var2 = tf.Variable(5.0)

init_op = tf.compat.v1.global_variables_initializer()

with tf.compat.v1.Session() as sess:
	sess.run(init_op)
First Solution to Attributeerror Module 'tensorflow' has no attribute 'global_variables_initializer'

When you run the above code to initialize the variables, it runs without any error because you use the version compatibility mode in the current Tensorflow version 2.

READ:  Python Screen Capture

Next, use the latest version of Tensorflow 2.x to initialize the variables. In the latest version of Tensorflow, you don’t have to use the global_variables_initializer() attribute. Simply initialize a variable using tf.Variable() constructor as shown below.

import tensorflow as tf

var1 = tf.Variable(4.0)
var2 = tf.Variable(5.0)

print("Value of var1",var1.numpy())
print("Value of var2", var2.numpy())
Second Solution to Attributeerror Module 'tensorflow' has no attribute 'global_variables_initializer'

Look, you have successfully initialized two variables, var1 and var2, with values 4.0 and 5.0, respectively, using the tf.Variable() constructor.

This is how to fix the error Attributeerror: Module ‘tensorflow’ has no attribute ‘global_variables_initializer’.

Conclusion

You learned how to resolve the error Attributeerror: Module ‘tensorflow’ has no attribute ‘global_variables_initializer’.

To fix the error you learned, you can use the tf.compat.v1 module to access that attribute to initialize all the global variables and tf.Variable() in Tensorflow version 2.x.

You may like to read: