Tensor with unspecified dimension in tensorflow
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 TensorFlow, an unspecified dimension usually appears as None in a shape. It means TensorFlow knows the rank and most dimensions, but one dimension will be determined later at runtime. This is common for batch size, sequence length, or any axis whose exact length is not fixed when the graph or model is defined.
What None Means in a Tensor Shape
When you see a shape such as (None, 128), TensorFlow is saying:
- this tensor has rank 2
- the second dimension is always
128 - the first dimension is unknown at definition time
The most common unknown dimension is the batch axis. A model can accept batches of size 1, 16, or 64 without redefining the computation graph.
In Keras, this is normal:
The printed input shape is typically (None, 128). Keras leaves the batch dimension unspecified by default.
Use Unspecified Dimensions in TensorSpec
tf.TensorSpec is often used in tf.function signatures and dataset pipelines. It can describe a tensor whose size is partially known.
That specification accepts any two-dimensional float tensor whose second dimension is 4.
You can use it in a traced function:
This works for any number of rows, as long as each row has length 4.
Unspecified Does Not Mean Arbitrary Structure
An unspecified dimension is still a single dense dimension. It does not mean each row can have a different length. For example, (None, 4) allows many rows, but each row must still have four values.
If you need variable-length rows, you are dealing with ragged data, not just an unspecified dense dimension. That is where tf.RaggedTensor or padded batches become relevant.
That solves a different problem from a dense tensor with one unknown axis size.
Common Places You See None
You will most often encounter unspecified dimensions in these situations:
- model input shapes where batch size is not fixed
- sequence models where time length varies
- dataset signatures defined with
TensorSpec - functions traced with
tf.function
Example with a variable-length time axis:
Here the batch dimension is unspecified, and the sequence-length dimension is also unspecified. The feature size, 16, stays fixed.
Static Shape Versus Runtime Shape
TensorFlow tracks both a static shape and a runtime shape. Static shape is what TensorFlow can infer when building the graph. Runtime shape is what the tensor actually has when real data flows through the computation.
You can inspect the runtime shape like this:
x.shape is the static shape metadata visible in Python. tf.shape(x) is a TensorFlow operation that produces the runtime dimensions.
Refine Shape Information When Needed
Sometimes TensorFlow knows less than you do. If a transformation preserves a dimension that TensorFlow cannot infer, you can refine the metadata with set_shape.
Use this carefully. If you assert the wrong shape, later operations can fail in harder-to-debug ways.
Common Pitfalls
One common mistake is thinking None means completely unconstrained data. It only marks a dimension whose exact size is deferred. Another is confusing an unspecified dense dimension with truly variable inner lengths, which requires ragged tensors or padding. Developers also rely on tensor.shape for runtime decisions inside graph code, even though tf.shape is often the correct tool there. Finally, manually forcing shape metadata with set_shape can create misleading assumptions if the underlying data does not actually match.
Summary
- In TensorFlow,
Nonein a shape means the dimension is unknown at definition time. - The most common unspecified dimension is batch size.
- '
TensorSpecand Keras input shapes frequently use unspecified dimensions.' - An unspecified dense dimension is different from ragged variable-length inner data.
- Use
tf.shapeto inspect runtime dimensions when needed. - Refine shape metadata carefully and only when you know the true shape constraints.
Related reading
- Tensorboard - visualize weights of LSTM
- TensorBoard doesn't show all data points
- Tensorboard scalar plotting with epoch number on the horizontal axis
- tensorboard with numpy array
- TensorBoard - Plot training and validation losses on the same graph?
- Tensorboard AttributeError 'Model' object has no attribute '_get_distribution_strategy
- TensorBoard could not bind to port 6006, it was already in use
- Tensorboard doesn't show runtime/memory for all operations
.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.