Add None dimension in tensorflow 2.0
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, None in a shape does not mean "insert a value into the tensor". It means "this dimension is unknown or variable at graph-building time", which is why the answer depends on whether you want a flexible shape in a model definition or an actual extra axis in data.
None Is a Shape Placeholder, Not Data
You often see None in Keras or TensorFlow shape declarations:
The printed shape looks like:
That leading None is the batch dimension. It means the model can accept any batch size at runtime. You are not adding literal None values to the tensor.
If You Want an Actual New Axis, Use tf.expand_dims
Suppose you have a tensor shaped (3, 4) and you want (1, 3, 4) or (3, 4, 1). That is not about None. That is about adding a real dimension.
Output:
This is the correct tool when you mean "add an axis".
If You Want a Variable Dimension in a Model, Declare It in the Shape
When the goal is flexibility, None belongs in a shape specification.
For example, variable-length sequences:
This means:
- batch size is variable
- sequence length is variable
- feature size is fixed at
8
That is a model-definition concept, not a data-manipulation operation.
You Cannot Fill a Tensor with Shape None at Runtime
A common misunderstanding is trying to "convert a dimension to None" on an already existing tensor. TensorFlow tensors at runtime have concrete shapes, even if some dimensions were declared abstractly when building a model or function signature.
So this mental model is correct:
- '
Noneis for shape specification' - '
tf.expand_dimsis for adding an axis' - '
tf.reshapeis for reorganizing concrete dimensions'
tf.reshape Does Something Different
tf.reshape changes the concrete layout of known elements. It does not make a dimension abstract or variable.
If you need a new axis, use expand_dims. If you need a flexible input signature, use None in the declared shape.
Typical Real Examples
Add a batch dimension before inference:
Define a model that accepts variable batch size:
Define a model that accepts variable sequence length:
These look related because they both involve shape thinking, but they solve different problems.
Common Pitfalls
- Thinking
Noneis a runtime tensor value rather than a symbolic shape placeholder. - Using
reshapewhen the real goal is to add an axis withexpand_dims. - Expecting an already materialized tensor to keep an abstract
Nonedimension. - Confusing variable batch size with variable feature size. Those are different parts of the shape.
- Using
Nonein the wrong place in a model definition and then getting incompatible-layer errors later.
Summary
- In TensorFlow,
Nonein a shape means "unknown or variable size", not an actual tensor element. - Use
tf.expand_dimswhen you want to add a real axis to data. - Use
NoneinInputor shape declarations when you want flexible model inputs. - '
tf.reshapechanges concrete dimensions but does not create symbolic flexibility.' - The key question is whether you are defining a model signature or changing actual tensor data.
Related reading
- Add Tensorflow pre-processing to existing Keras model for use in Tensorflow Serving
- Add weights to .pb file exported by TensorFlow
- Adding a preprocessing layer to keras model and setting tensor values
- Adding a variable into Keras/TensorFlow CNN dense layer
- Adding a variable into Keras/TensorFlow CNN dense layer
- Adding an additional value to a Convolutional Neural Network Input?
- Adding an extra hidden layer using Google's TensorFlow
- Adding Attention on top of simple LSTM layer in Tensorflow 2.0
.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.