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

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

I will show you the methods to solve this error; in the first method, you will use the tf. compat.v1 module, and the second, you will use the Tensorflow version 2 shape and dimension attribute.

Additionally, you will use the tensorflow version 1.14 to solve that error.

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

The error message Attributeerror: Module ‘tensorflow’ has no attribute ‘dimension’ means you are trying to access an attribute in the TensorFlow that either doesn’t exist or had been deprecated in the version of Tensorflow you are currently using.

  • Tensors are used in every computation of Tensorflow. A tensor is an n-dimensional vector or matrix that can represent any data. A tensor’s values all have the same data type and known (or at least partially known) shape.
  • The geometry of the data determines the dimensions of the matrix or array.

Let me show you the code that causes this error.

import tensorflow as tf

tens_1 = tf.constant([[36, 56, 21],[12,56,7]])
result=tf.Dimension(tens_1)
print(result)
Attributeerror Module 'tensorflow' has no attribute 'dimension'

To fix that error, use tensorflow.compat.v1. Then, call the function Dimension() from the tensorflow.compat.v1.

For example, use the code below.

import tensorflow.compat.v1 as tf
tens_1 = tf.constant(36)
result=tf.Dimension(tens_1)
print(result)
Solution to Attributeerror Module 'tensorflow' has no attribute 'dimension'

Now, the function Diemsion() is working.

You can follow another approach here if you have updated your tensorflow version, then downgrade to version 1.14.0.

Here, you need to downgrade the version because of changes in API to a new version of tensorflow.

READ:  How to create a function-based view in Django

First, uninstall the current version of tensorflow.

pip uninstall tensorflow
pip install tensorflow==1.14.0

Now you will run the below code; you won’t get that error.

import tensorflow as tf

tens_1 = tf.constant([[36, 56, 21],[12,56,7]])
result=tf.Dimension(tens_1)
print(result)

Also, if you are trying to get the dimensions or shape of the tensors, here’s how you can do it in Tensorflow version 2.0

Run the code in your cell.

import tensorflow as tf
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])

tensor_shape = tensor.shape
print('Shape:', tensor_shape)

dim0 = tensor_shape[0]
print('Dimension:', dim0)
Second Solution to Attributeerror Module 'tensorflow' has no attribute 'dimension'

Here in the code, tensor.shape returns the shape of the tensor, and tensor_shape[0] returns the specific dimension of the tensor.

To learn more about Tensor shape and dimension, visit this tutorial Tensor in TensorFlow. This is how to solve the error Attributeerror: Module ‘tensorflow’ has no attribute ‘dimension’.

Conclusion

In this Tensorflow tutorial, you learned how to fix the error Attributeerror: Module ‘tensorflow’ has no attribute ‘dimension’.

You have learned two approaches to solve that error; in the first approach, you used the Dimension() function from the tensorflow.compat.v1 module, in the second, you have used the tensorflow version 1.14.0.

Lastly, in the third, you have used tensor.shape and tensor.shape[0] to access the shape and a specific tensor dimension.

You may like to read: