Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with add method in tensorflow : AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'

Tags:

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'

like image 499
ConfusedProgrammer Avatar asked Nov 03 '18 20:11

ConfusedProgrammer


2 Answers

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

like image 53
Ricky Avina Avatar answered Oct 11 '22 12:10

Ricky Avina


You can use the following import command:

from tensorflow.keras.layers import ...  

instead of the 'old' one:

from keras.layers import .... 

As described here.

like image 30
Rene B. Avatar answered Oct 11 '22 12:10

Rene B.