InvalidArgumentError Expected dimension in the range -1, 1 but got 1
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
InvalidArgumentError: Expected dimension in the range [-1, 1) but got 1 usually means you passed an axis that does not exist for the tensor you are working with. In plain terms, your code is treating a one-dimensional tensor like it had a second dimension.
What the Error Range Means
Tensor operations often take an axis argument. TensorFlow validates that axis against the tensor rank.
If the allowed range is [-1, 1), the only valid axes are:
- '
-1, meaning the last axis' - '
0, meaning the first axis'
Axis 1 is invalid because a rank-1 tensor has only one dimension. There is no second axis.
For example, this tensor:
has shape (3,), so it is rank 1. Valid axes are only 0 and -1.
A Typical Way to Trigger the Error
One common mistake is trying to concatenate one-dimensional tensors along axis 1:
This fails because a and b are vectors, not matrices. They do not have an axis 1.
The correct version for vectors is:
If you truly want to concatenate across columns, first reshape them into rank-2 tensors:
How to Debug It Quickly
Whenever you see this error, inspect the tensor shape before the failing operation:
Then compare that rank to the axis you passed. If rank is 1, axis 1 is invalid. If rank is 2, axis 1 is fine. This simple check usually reveals the problem faster than reading the full stack trace.
It is also useful to add assertions in data pipelines:
That gives you a clearer failure earlier in the pipeline if your code expects a matrix but receives a vector.
Why This Happens in Model Code
This error often appears when preprocessing steps accidentally drop a dimension. A few common examples:
- selecting one item from a batch and losing the batch axis
- using
tf.squeeze()too aggressively - reading labels as shape
(batch,)while the model expects(batch, 1) - flattening tensors earlier than intended
Consider this example:
After indexing with features[0], the result is rank 1. If later code still assumes a two-dimensional tensor and uses axis 1, the error appears.
Fixing It the Right Way
The fix depends on what shape you actually want.
If the data should stay one-dimensional, use axis 0:
If the data should be two-dimensional, add or preserve the missing dimension:
Now axis 1 exists because the shape becomes (3, 1).
For labels, many training bugs are fixed by being explicit:
This turns a rank-1 label vector into a column-shaped rank-2 tensor.
Build Shape Awareness into Your Workflow
Tensor bugs are easier to prevent than to debug. A few habits help a lot:
- print shapes at the boundaries of your pipeline
- keep batch dimensions explicit
- use
reshape,expand_dims, andsqueezedeliberately - read API docs for each operation's allowed axis range
Once you start thinking in ranks and shapes, this error message becomes much less mysterious.
Common Pitfalls
- Assuming a vector has axis
1when it only has axis0. - Losing a dimension through indexing or
squeeze()and not noticing. - Trying to fix the error by changing the axis randomly instead of checking the intended shape.
- Reshaping blindly without confirming that the new shape matches the model's expectations.
- Debugging only the failing line instead of the earlier preprocessing step that changed the rank.
Summary
- This TensorFlow error means the axis argument is out of bounds for the tensor rank.
- A range of
[-1, 1)means only axes-1and0are valid. - The most common cause is treating a rank-1 tensor like a rank-2 tensor.
- Check tensor shapes before the failing operation and preserve dimensions intentionally.
- Use
reshapeorexpand_dimswhen you truly need an extra axis.
Related reading
- InvalidArgumentError indicesi,0 x is not in 0, x in keras
- InvalidArgumentError input_10 is both fed and fetched
- InvalidArgumentError input depth must be evenly divisible by filter depth 4 vs 3
- InvalidArgumentError input must be 4-dimensional8,6171,4
- InvalidArgumentError Input to reshape is a tensor with 178802 values, but the requested shape has 89401
- InvalidArgumentError Mismatch between the current graph and the graph from the checkpoint
- Invert MinMaxScaler from scikit_learn
- ipython notebook clear cell output in code
.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.