Module ‘tensorflow’ has no attribute ‘log’

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

I will explain three approaches to solving that error: downgrade the tensorflow version, use the compatibility mode, and use the tf.math submodule of TensorFlow version 2 to fix the error.

Let’s solve the error,

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

The error Attributeerror: Module ‘tensorflow’ has no attribute ‘log’, which says you are trying to access the attribute log from the TensorFlow library, which doesn’t exist.

  • The tensorflow supports several elementary mathematical operations. The Tensorflow function tf.log() supports the natural logarithmic function. It anticipates input as floating point values or complex numbers in the a+bi format.

Let me show you the cause of the error. Run the code below.

import tensorflow as tf

tf.compat.v1.disable_eager_execution()
new_tens = tf.constant([-2.3, -5.3, 1.7, 1.4, 9], dtype = tf.float32)
result= tf.log(new_tens)
with tf.Session() as val:
    new_output=val.run(result)
    print(new_output)
Attributeerror Module 'tensorflow' has no attribute 'log'

The error appears when you directly access TensorFlow’s attribute log(). Your code is correct here, but you use it in the tensorFlow version 2 environment. This means that in TensorFlow version 1, you can directly access the log() from the library.

However, this is not possible in the TensorFlow 2 because of the change in the API. In simpler words, your code is based on TensorFlow version 1, and you have updated tensorflow version 2 and then ran this code, which is invalid for TensorFlow 2.

You have three options to solve that error: either downgrade the TensorFlow version, use the tensorflow 1 compatibility mode in the TensorFlow version 2 environment, or use the latest API method of TensorFlow 2.

READ:  How to Solve Attributeerror: module ‘tensorflow’ has no attribute ‘py_function’

Let’s look at each solution one by one.

First, create a separate environment that env installs a specific version of TensorFlow, such as version 1.13 or 1.4, etc.

pip install -upgrade tensorflow==1.13

After downgrading, when you run the code, it doesn’t show the error.

The next solution is to use TensorFlow version 1 in the current environment version 2 using the tf.compat.v1 module.

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

new_tens = tf.constant([-2.3, -5.3, 1.7, 1.4, 9], dtype = tf.float32)
result= tf.compat.v1.log(new_tens)
with tf.compat.v1.Session() as val:
    new_output=val.run(result)
    print(new_output)
First Solution to Attributeerror Module 'tensorflow' has no attribute 'log'

Using the tf.compat.v1 module, you have successfully accessed the log() function in the TensorFlow version 2 environment.

Finally, you can use the submodule tf.math to access the log() attribute; in the latest version of tensorflow, all the mathematical functions have been moved to the submodule tf.math.

So, rerun the code but access the log() attribute from the tf.math submodule, as shown in the code below.

import tensorflow as tf

new_tens = tf.constant([-2.3, -5.3, 1.7, 1.4, 9], dtype = tf.float32)
result= tf.math.log(new_tens)
print(result.numpy())
Second Solution to Attributeerror Module 'tensorflow' has no attribute 'log'

In the above code, accessing the log() attribute from the tf.math, you also don’t need to use session; in the TensorFlow 2, this session is handled by default. As a result, you don’t get that error again.

The above approach is a recommended solution that you should follow to avoid errors in future.

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

Conclusion

In this TensorFlow tutorial, you fixed the error Attributeerror: Module ‘tensorflow’ has no attribute ‘log’.

You solved the error using three different methods: downgrading the tensorflow version and using the tf. compact.v1 module and by using the tf.math submodule.

READ:  np.savetxt() function in Python [5 Examples]

You may like to read: