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

In this TensorFlow tutorial, I will explain how to solve the error Attributeerror: Module ‘tensorflow’ has no attribute ‘trainable_variables’.

I will show how to use the compatibility mode of TensorFlow version 1.x into the environment of TensorFlow version 2.x to solve the error. Especially you will learn how to use the tf.compat.v1 module to fix that error.

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

The Attributeerror: Module ‘tensorflow’ has no attribute ‘trainable_variables’, indicating that you are trying to access the attribute trainable_variable from the tensorflow, which doesn’t exist.

The trainable variables are the model’s parameters, which are updated during the training process. These are the variables if you have heard about weight and bias in the model. They are called trainable variables.

By adjusting these variables, the model learns from the input data.

Here is the example code that shows the error.

import tensorflow as tf

tensor = tf.trainable_variables(scope=None)
print(tensor)
Attributeerror Module 'tensorflow' has no attribute 'trainable_variables'

When you run the above code, it tries to access the tainable_variables attribute from the tf (tensorflow) directly. This attribute does not directly exist in the TensorFlow namespace; instead, it is a property of the TensorFlow model and layer.

This error arises because your code is compatible with TensorFlow version 1.x, and you have upgraded tensorflow to version 2.x, where this trainable function doesn’t exist. You can directly access the trainable_variables attribute in TensorFlow version 1.x.

So the solution for this error is to use the compatibility mode of the Tensorflow version 1.x in the Tensorflow version 2.x.

READ:  Python program to print the duplicate elements of an array [4 Methods]

Use the tf.compat.v1 module to access the trainables_variables() attribute. The complete syntax is given below.

tf.compat.v1.trainable_variables(
    scope=None
)

Where parameter:

  • scope: It is an optional parameter, and if it is provided, re.match is used to filter the output list so that it only contains objects whose name attribute matches scope. If a scope is given, items without a name attribute are never returned. Because of the selection of re.match, a scope without unique tokens is filtered by the prefix.

Again, rerun the above code using the tf.compat.v1 module is shown in the code below.

import tensorflow as tf

tensor = tf.compat.v1.trainable_variables(scope=None)
print(tensor)
Solution to Attributeerror Module 'tensorflow' has no attribute 'trainable_variables'

After running the code, you can access and use the trainable_variables() from the tf.compat.v1 module in TensorFlow version 2, which you can see in the output.

The above example uses the compatibility mode. Let’s also see how to access the trainable variables of the model in TensorFlow version 2.

First, import tensorflow.

import tensorflow as tf

Define the model using the code below.

model = tf.keras.Sequential([
	tf.keras.layers.Dense(10, activation='relu', input_shape=(5,)),
	tf.keras.layers.Dense(1)
])

After defining the model, you can call the trainable_variables attribute on the model as shown below.

model.trainable_variables
Second Solution to Attributeerror Module 'tensorflow' has no attribute 'trainable_variables'

Look when you call the attribute trainable_variables on the model; it shows all the trainable variables, which are weights and biases.

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

Conclusion

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

You learned two solutions; in the first, you used the tf. compat.v1 module to access the trainable_variables() attribute; in the second, you called the trainable_variables() on the model to list all the trainable variables adjusted while training the model.

READ:  How to use PyTorch Cat function

You may like to read: