2

When i define a model in keras an error is shown as follows AttributeError: 'Tensor' object has no attribute '_keras_shape'

the code producing the error is

vocab_size = 10000
MAX_SEQUENCE_LENGTH = 256
sequence_input = keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
embedding=keras.layers.Embedding(vocab_size, 16, input_length  = MAX_SEQUENCE_LENGTH)(sequence_input)
x=keras.layers.GlobalAveragePooling1D()(embedding)
x=keras.layers.Dense(16, activation=tf.nn.relu)(x)

preds = keras.layers.Dense(1, activation=tf.nn.sigmoid)(x)

model = Model(inputs=sequence_input, outputs=preds)
model.compile(optimizer=tf.train.AdamOptimizer(),
          loss='binary_crossentropy',
          metrics=['accuracy'])

complete error message is shown below

AttributeError                            Traceback (most recent call last)
<ipython-input-5-1c6ea41c38e1> in <module>()
      1 from keras.models import Model
----> 2 model = Model(inputs=sequence_input, outputs=preds)
      3 model.compile(optimizer=tf.train.AdamOptimizer(),
      4               loss='binary_crossentropy',
      5               metrics=['accuracy'])

~/Datacube/datacube_env/lib/python3.5/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name +
     90                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~/Datacube/datacube_env/lib/python3.5/site-packages/keras/engine/network.py in __init__(self, *args, **kwargs)
     89                 'inputs' in kwargs and 'outputs' in kwargs):
     90             # Graph network
---> 91             self._init_graph_network(*args, **kwargs)
     92         else:
     93             # Subclassed network

~/Datacube/datacube_env/lib/python3.5/site-packages/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name)
    249              input_masks=[None for _ in self.inputs],
    250              output_masks=[None for _ in self.outputs],
--> 251              input_shapes=[x._keras_shape for x in self.inputs],
    252              output_shapes=[x._keras_shape for x in self.outputs])
    253 

~/Datacube/datacube_env/lib/python3.5/site-packages/keras/engine/network.py in <listcomp>(.0)
    249              input_masks=[None for _ in self.inputs],
    250              output_masks=[None for _ in self.outputs],
--> 251              input_shapes=[x._keras_shape for x in self.inputs],
    252              output_shapes=[x._keras_shape for x in self.outputs])
    253 

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

can anyone tell me how to solve this

2
  • 1
    I tried running your code, its working for me Commented Jul 25, 2018 at 10:05
  • as @HariKrishnan said the code you posted is running without error. From the error message it can be seen that you are not executing the code you posted rather different code(from keras.models import Model is before model = Model(inputs=seque ... ). Make sure that you are not executing different file and/ or if there is .pyc file try to remove it. Commented Jul 26, 2018 at 11:01

1 Answer 1

1

I have noticed this can happen if you mix up regular keras imports and tensorflow imports. Avoid mixing imports like this:

# Mixed imports, one is regular keras, other is TF's keras
import keras
from tensorflow.keras.model import Model

sequence_input = keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
...
model = Model(inputs=sequence_input, outputs=preds)

Instead, import everything from TF for instance:

from tensorflow import keras
from tensorflow.keras.model import Model

sequence_input = keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
...
model = Model(inputs=sequence_input, outputs=preds)

Or use Keras exclusively:

import keras
from keras.model import Model

sequence_input = keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
...
model = Model(inputs=sequence_input, outputs=preds)

Cannot be sure it's the root cause of your problem, but this might help some folks

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

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.