TensorFlow 2 has integrated deep-learning Keras API as tensorflow.keras. If you try to import from the standalone Keras API with a Tensorflow 2 installed on your system, this can raise incompatibility issues, and you may raise the AttributeError: module ‘tensorflow.python.framework.ops’ has no attribute ‘_TensorLike’.

To solve this error, instead of using

from keras import ...

Use the TensorFlow integrated API:

from tensorflow.keras import ...

This tutorial will go through how to solve the error with code examples.


AttributeError: module ‘tensorflow.python.framework.ops’ has no attribute ‘_TensorLike’

TensorFlow is an open-source library for building machine learning models at scale. Keras is a high-level neural networks API written in Python and is capable of running with TensorFlow, Theano or CNTK backends. TensorFlow 2 provides access to the Keras API under tensorflow.keras. If you have Keras installed and TensorFlow 2 installed and try to use the Keras standalone API instead of the integrated API, you can encounter incompatibility issues. The AttributeError module ‘tensorflow.python.framework.ops’ has no attribute ‘_TensorLike’ can arise when creating a Keras model with TensorFlow 2.x.

Example

Let’s look at an example where we are building a neural network for a binary classification problem. We will create the neural network using the Sequential API.

from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense

# define the keras model
model = Sequential()

model.add(Dense(12, input_dim=8, activation='relu'))

model.add(Dense(8, activation='relu'))

model.add(Dense(1, activation='sigmoid'))

# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

Let’s run the code to see what happens:

Using TensorFlow backend.

~/opt/anaconda3/lib/python3.8/site-packages/keras/backend/tensorflow_backend.py in is_tensor(x)
    701 
    702 def is_tensor(x):
--> 703     return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x)
    704 
    705 

AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'

We can see that Keras is using the TensorFlow backend. There is an incompatibility issue between keras and tensorflow.keras. If we are using TensorFlow 2, we should use the tensorflow.keras integrated API.

Solution

To solve this, we need to change the import statement to use tensorflow.keras. Let’s look at the revised code:

from numpy import loadtxt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# define the keras model

model = Sequential()

model.add(Dense(12, input_dim=8, activation='relu'))

model.add(Dense(8, activation='relu'))

model.add(Dense(1, activation='sigmoid'))

# compile the keras model

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

We find the model compiles with no issues.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: module ‘tensorflow.python.framework.ops’ has no attribute ‘_TensorLike’ occurs when you use the Keras standalone API instead of the integrated tensorflow.keras API. To solve this error, ensure that you do your Keras API imports with tensorflow.keras import ... instead of from keras import ....

For further reading on TensorFlow, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!