Module ‘tensorflow’ has no attribute ‘get_variable’

In this TensorFlow tutorial, I will show you how to fix the error Attributeerror: module ‘tensorflow’ has no attribute ‘get_variable’.

I will show you three solutions to resolve this error: first, downgrade the tensorflow version; second, use the compatibility mode; and third, follow the API of Tensorflow version 2.x.

Let’s begin,

Attributeerror: module ‘tensorflow’ has no attribute ‘get_variable’

This error Attributeerror: module ‘tensorflow’ has no attribute ‘get_variable’. There is no attribute named ‘get_variable’ in Tensorflow, meaning it doesn’t exist.

So, what code is responsible for this error? The code is given below.

import tensorflow as tf

new_tens = tf.get_variable(name='tens',shape=[2],dtype=tf.int32)
print(new_tens)
Attributeerror module 'tensorflow' has no attribute 'get_variable'

When executing the above code, the error shows ‘Attributeerror module ‘tensorflow’ has no attribute ‘get_variable’‘ you can see. so ‘What is the reason behind this error?’, there are two reasons.

First, maybe you are trying to access the get_variable() attribute from the incorrect path or module.

The second reason is the incompatible version of tensorflow you are using, which means I see here that the code is compatible with tensorflow version 1.x. But the current tensorflow version is 2.x, so this attribute doesn’t exist in the latest version of tensorflow.

To resolve this error, you have three solutions: first, downgrade the tensorflow version; second, use the compatibility mode; and third, follow the API of Tensorflow version 2.x.

First, to resolve the issue, downgrade the current version of Tensorflow to 1.14 or 1.15.

pip install --upgrade tensorflow==1.14 // use this in your terminal

!pip istall --upgrade tensorflow==1.14 // use this in your Notebook environment

Here, using the code, downgrading the tensorflow version to 1.14. After downgrading, your code will work, and you won’t get that error again.

READ:  Scipy Integrate + Examples

The second solution is to use the compatibility mode in the environment of TensorFlow version 2.x.

So use the tf.compat.v1 module and access the get_variable() attribute. The complete syntax is given below.

tf.compat.v1.get_variable(
    name,
    shape=None,
    dtype=None,
    initializer=None,
    regularizer=None,
    trainable=None,
    collections=None,
    caching_device=None,
    partitioner=None,
    validate_shape=True,
    use_resource=None,
    custom_getter=None,
    constraint=None,
    synchronization=tf.VariableSynchronization.AUTO,
    aggregation=tf.compat.v1.VariableAggregation.NONE
)

Where parameters are:

  • name: It specifies the name of the operation.
  • shape: By default, it takes no value and defines the shape of the input tensor.
  • dtype: It defines the datatype of the input tensor.
  • initializer: If a variable initializer is created, it will be used. can either be a Tensor or an initializer object. Its shape must be known if it’s a Tensor unless the validated shape is False.
  • regularizer: tf.GraphKeys will receive the outcome of applying it to a freshly formed variable. You can regularise using REGULARIZATION LOSSES.
  • trainable: Add the variable to the GraphKeys collection if True. TRAINABLE VARIABLES (see tf.Variable).
  • collection: List of graph collection keys to add the Variable.

Now rerun the code using the tf.compat.v1 module as shown below.

import tensorflow as tf

new_tens = tf.compat.v1.get_variable(name='new_tens',shape=[2],dtype=tf.int32)
print(new_tens)
Second Solution to Attributeerror module 'tensorflow' has no attribute 'get_variable'

As you can see in the output, you can access the get_variable() attribute from the tf.compat.v1 module in the current environment TensorFlow version 2.

Lastly, you can use the latest TensorFlow API., where you have to replace the get_variable with the Variable() constructor of TensorFlow.

If you want to know how to create variables in TensorFlow, I recommend this tutorial TensorFlow Variable.

import tensorflow as tf

new_tens = tf.Variable([1,1],name='new_tens',dtype=tf.int32)
print(new_tens)
Third Solution to Attributeerror module 'tensorflow' has no attribute 'get_variable'

When the above code is executed, it creates a new variable new_tens of type integer using the tf.Variable() function. Here, I suggest you stick with this solution because this is the latest method for creating variables in the newest version of Tensorflow.

READ:  NumPy Reset Index of an Array in Python [3 Methods]

This is how you can resolve the error Attributeerror: module ‘tensorflow’ has no attribute ‘get_variable’.

Conclusion

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

You have used three methods to fix that error; in the first method, you downgraded the tensorflow version, and in the second, you used the tf.compat.v1 module, and in the third, you have used the tf.Variable() of Tensorflow.

You may like to read: