Tensor object has no attribute keras_shape
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The error AttributeError: 'Tensor' object has no attribute '_keras_shape' (or 'keras_shape') occurs when mixing TensorFlow 1.x-style code with TensorFlow 2.x, or when passing raw TensorFlow tensors to Keras layers that expect Keras tensors. In TensorFlow 1.x, Keras tracked shape information using a custom _keras_shape attribute on tensors. TensorFlow 2.x removed this attribute because eager execution makes shape information available directly through tensor.shape. The fix is to use TensorFlow 2.x APIs consistently and avoid mixing raw tf.Tensor objects with Keras symbolic tensors.
Why This Error Happens
In TensorFlow 1.x with standalone Keras, the library attached a _keras_shape attribute to tensors to track shape information through the computation graph:
Fix 1: Use tf.keras Instead of Standalone Keras
The most common cause is importing standalone keras instead of tf.keras:
Uninstall standalone Keras if both are installed:
Fix 2: Use Keras Input Layers Instead of Raw Tensors
Keras layers expect keras.Input() tensors, not raw tf.Tensor objects:
Fix 3: Replace K.placeholder with tf.keras.Input
TensorFlow 2.x deprecated K.placeholder(). Use tf.keras.Input() instead:
Fix 4: Update Custom Layers
Custom layers that access _keras_shape must be updated:
Getting Tensor Shapes in TF2
Static vs Dynamic Shapes
Migration Checklist
Common Pitfalls
- Having both
kerasandtensorflowinstalled: Standalonekerasandtf.kerasconflict. Uninstall standalonekeras(pip uninstall keras) and use onlytensorflow.kerasto avoid attribute errors. - Using
K.placeholder()in TF2:K.placeholder()creates TF1-style placeholders that lack TF2 shape tracking. Replace withtf.keras.Input()which integrates with the Keras functional API. - Passing raw tensors to Keras model-building code: Keras functional API expects
Input()tensors at the start of the graph. Passingtf.constant()ortf.Variabledirectly to layers during model construction causes shape tracking failures. - Confusing static and dynamic shapes:
tensor.shapereturns the static shape (may containNone).tf.shape(tensor)returns the dynamic shape (always concrete at runtime). Use static shapes for layer configuration and dynamic shapes for runtime operations. - Running TF1 code without
tf.compat.v1compatibility mode: Legacy code that depends on_keras_shapecan be run withimport tensorflow.compat.v1 as tf; tf.disable_v2_behavior()as a temporary migration aid, but updating to TF2 APIs is the proper fix.
Summary
- The
_keras_shapeattribute was removed in TensorFlow 2.x — usetensor.shapeinstead - Replace
import keraswithfrom tensorflow import keras(orimport tensorflow.keras) - Use
tf.keras.Input(shape=(...))instead ofK.placeholder() - Access shapes with
tensor.shape(static) ortf.shape(tensor)(dynamic) - Update custom layers to use
input_shapeinbuild()instead of_keras_shape
Related reading
- ''Tensor'' object has no attribute ''lower''
- Tensor with unspecified dimension in tensorflow
- TensorBoard - Plot training and validation losses on the same graph?
- Tensorboard - visualize weights of LSTM
- TensorBoard could not bind to port 6006, it was already in use
- TensorBoard doesn't show all data points
- Tensorboard AttributeError 'Model' object has no attribute '_get_distribution_strategy
- Tensorboard AttributeError 'ModelCheckpoint' object has no attribute 'on_train_batch_begin
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.