InvalidArgumentError Expected dimension in the range -1, 1 but got 1
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

