TensorFlow
Error Debugging
Machine Learning
Data Science
Neural Networks

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.

Practice ML system design

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:

python
1import tensorflow as tf
2
3x = tf.constant([10, 20, 30])
4print(x.shape)

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:

python
1import tensorflow as tf
2
3a = tf.constant([1, 2, 3])
4b = tf.constant([4, 5, 6])
5
6tf.concat([a, b], 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:

python
result = tf.concat([a, b], axis=0)
print(result)

If you truly want to concatenate across columns, first reshape them into rank-2 tensors:

python
1a2 = tf.reshape(a, (3, 1))
2b2 = tf.reshape(b, (3, 1))
3
4result = tf.concat([a2, b2], axis=1)
5print(result)

How to Debug It Quickly

Whenever you see this error, inspect the tensor shape before the failing operation:

python
print(x.shape)
print(tf.rank(x))

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:

python
tf.debugging.assert_rank(x, 2)

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:

python
1features = tf.constant([[1.0, 2.0], [3.0, 4.0]])
2single_row = features[0]
3
4print(features.shape)   # (2, 2)
5print(single_row.shape) # (2,)

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:

python
values = tf.constant([1, 2, 3])
print(tf.reduce_sum(values, axis=0))

If the data should be two-dimensional, add or preserve the missing dimension:

python
values = tf.constant([1, 2, 3])
column = tf.expand_dims(values, axis=1)
print(column.shape)

Now axis 1 exists because the shape becomes (3, 1).

For labels, many training bugs are fixed by being explicit:

python
y = tf.constant([0, 1, 1, 0], dtype=tf.float32)
y = tf.reshape(y, (-1, 1))
print(y.shape)

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, and squeeze deliberately
  • 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 1 when it only has axis 0.
  • 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 -1 and 0 are 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 reshape or expand_dims when you truly need an extra axis.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.