shantanuo
shantanuo

Reputation: 32296

Tensor object has no attribute keras_shape

While building a model using:

model = Model(inputs=[input_text], outputs=pred)

getting an error:

AttributeError: 'Tensor' object has no attribute '_keras_shape'

The complete notebook that I tried can be found here... https://github.com/shantanuo/pandas_examples/blob/master/tensorflow/tf_learn.ipynb

And the same code in plain text here... https://gist.github.com/shantanuo/4e35baba144ee658e4dd4d1f87e19f3a

I will like to know why am I getting an error even if I am using the exact same code as shown in this blog:

https://towardsdatascience.com/transfer-learning-using-elmo-embedding-c4a7e415103c

The expected output - something like this:

The model summary is: _________________________________________________________________ Layer (type) Output Shape Param #
================================================================= input_2 (InputLayer) (None, 1) 0
_________________________________________________________________ lambda_2 (Lambda) (None, 1024) 0
_________________________________________________________________ dense_3 (Dense) (None, 256) 262400
_________________________________________________________________ dense_4 (Dense) (None, 1) 257

I tried to upgrade tensorflow and keras but getting the same error:

!pip install --upgrade tensorflow

Upvotes: 12

Views: 19302

Answers (2)

Wise Cloud
Wise Cloud

Reputation: 411

I had a similar issue when I changed my computer with a different tensorflow version.

What solved it for me was using .shape instead of ._keras_shape

Depending on the version of tensorflow/keras and/or how you import them (from keras, from tensorflow.keras or from tensorflow.python.keras) it appears that the attributes names can differ.

Upvotes: 18

If you change this:

from keras.models import Model

to this:

from tensorflow.keras.models import Model

your code will be fine.

* or *

Change this:

from tensorflow.python.keras.layers import Input

to this:

from keras.layers import Input

Upvotes: 9

Related Questions