TensorFlow AttributeError 'Tensor' object has no attribute '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
In modern TensorFlow, a real tf.Tensor normally does have a .shape attribute. So if you see AttributeError: 'Tensor' object has no attribute 'shape', the usual cause is not that TensorFlow forgot the attribute. The usual cause is that the object is not the kind of tensor you think it is, or that your code really needs runtime shape logic rather than static shape metadata.
Static Shape Versus Runtime Shape
TensorFlow exposes shape in two different ways:
- '
tensor.shapefor static metadata known to Python' - '
tf.shape(tensor)for runtime shape computed inside TensorFlow'
Example:
If your code is building layers or validating dimensions in Python, .shape is usually appropriate. If your code runs inside a traced graph or depends on dynamic batch sizes, tf.shape(...) is often the right tool instead.
Verify the Object Type First
A large share of these errors comes from one simple bug: the value is not actually a native TensorFlow tensor.
Start with:
If the object came from:
- a wrapper library
- an older compatibility layer
- a custom pipeline object
- a symbolic placeholder in unexpected form
then .shape may not behave the way you expect.
A quick diagnostic helper:
This separates "not a TensorFlow tensor" from "dynamic shape confusion" very quickly.
TensorFlow 1.x Compatibility Code
In older graph-mode code, get_shape() is common and still valid in compatibility paths.
If you are maintaining TensorFlow 1 style code, do not assume every modern eager-mode pattern applies directly. Graph-mode code often needs a different mental model.
Keras Symbolic Tensors
Inside Keras model-building code, tensors can be symbolic. They may expose a shape, but not every dimension is a concrete Python integer.
This works, but the batch dimension may remain symbolic. If your code tries to use shape information for dynamic control flow, switch to TensorFlow ops rather than plain Python branching.
Use tf.shape in Graph-Style Logic
When the code depends on actual runtime sizes, prefer tf.shape.
This is especially important in:
- '
@tf.function' - custom layers
- dynamic batching
- dataset pipelines
Static .shape metadata is often not enough in those contexts.
Common Migration Mistake
A frequent migration bug is code that assumes .shape is always a plain tuple-like value that can drive ordinary Python logic. In eager mode that often works. In traced or symbolic code, it may not.
If your logic depends on TensorFlow execution, keep shape handling inside TensorFlow operations. If your logic is just debugging or validation at Python level, .shape is fine.
The key is to decide which of those two worlds your code is actually in.
Common Pitfalls
- Assuming every object named
tensoris a nativetf.Tensor. - Using
.shapewhen the real requirement is runtime shape fromtf.shape(...). - Mixing TensorFlow 1 graph-mode assumptions with TensorFlow 2 eager-style code.
- Treating symbolic Keras dimensions as concrete integers too early.
- Debugging shape issues without printing the actual object type first.
Summary
- A real modern TensorFlow tensor normally supports
.shape. - Use
.shapefor static metadata andtf.shape(...)for runtime dimensions. - If
.shapefails, verify that the object is actually the tensor type you think it is. - TensorFlow 1 compatibility code and symbolic Keras code need slightly different shape reasoning.
- Most fixes come from clarifying object type and whether the code needs static or dynamic shape information.
Related reading
- TensorFlow average gradients over several batches
- Tensorflow AVX Support
- Tensorflow batch_size or steps is required for Tensor or NumPy input data
- TensorFlow, batchwise indexing first dimension and sorting
- Tensorflow, best way to save state in RNNs?
- TensorFlow Blas GEMM launch failed
- tensorflow check if a scalar boolean tensor is True
- tensorflow cifar10_eval.py errorRuntimeError Attempted to use a closed Session.RuntimeError Attempted to use a closed Session
.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.