29
import keras as K
from keras.models import Sequential
from keras.layers import Dense
from tensorflow import set_random_seed

for hidden_neuron in hidden_neurons:
  model = Sequential()

model.add(Dense(hidden_neuron, input_dim=61, activation='relu'))

-> i am getting error at this line. I am not really sure what am i missing here.

Traceback (most recent call last):

File "PycharmProjects/HW2/venv/bin/hw3q4.py", line 46, in model.add(Dense(hidden_neuron, input_dim=61, activation='relu')) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/sequential.py", line 165, in add layer(x) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/base_layer.py", line 414, in call self.assert_input_compatibility(inputs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/base_layer.py", line 279, in assert_input_compatibility K.is_keras_tensor(x) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 472, in is_keras_tensor if not is_tensor(x): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 480, in is_tensor return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x) AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'

8 Answers 8

56

For me, the fix was importing

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, Flatten, Dense

instead of

from keras import Sequential
from keras.layers import Conv2D, Flatten, Dense

There seems to be some weird compatibility issues between keras and tensorflow.keras

Sign up to request clarification or add additional context in comments.

1 Comment

Why not sync tf and keras and import properly from keras?
6

You can use the following import command:

from tensorflow.keras.layers import ... 

instead of the 'old' one:

from keras.layers import ....

As described here.

1 Comment

Great! You made my day!
4

For those who stumbled on to this, reinstalling Keras and Tensorflow fixes the issue.

1 Comment

Actually, that is not entirely true. It depends on the combination of keras and tensorflow versions as keras was integrated into tf as tensorflow.keras. Nevertheless you could get lucky by updating.
4

It is due to version incompatibility.
Update keras to the latest version compatible with tensorflow:

pip install --upgrade keras==x.x.x

Comments

4
!pip uninstall tensorflow 
!pip install tensorflow==1.14

!pip uninstall keras 
!pip install keras==2.2.4

Installing the above versions of keras and tensorflow have solved the problem for me.

1 Comment

Can't find that version of tensorflow available anymore (only from 2.2.0 onwards via pip)
1

Simply updating both TensorFlow as well as Keras might fix the issue

Comments

1

It depends on how you are importing the preliminaries. If importing tensorflow as tf and importing keras within the tensorflow, you should start with tf.keras. otherwise, if you are importing directly keras.models then you can just start off with Input() or Conv().

Comments

0

Many people saying about updating TensorFlow or Keras, which is a good solution but in a few cases it might not be applicable, (depends on the situation). The idea of uninstalling and re-installing Keras and Tensorflow, by installing first TensorFlow, is one of the approaches that can solve the issue. For the ones that have the problem in Jupyter notebook, you may wish to close and reopen your Jupyter notebook, when you have installed the libraries, because sometimes it doesn't update properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.