tensorflow / tensorflow/tensorflow
tf.ragged.constant does not detect dense dimensions
@Venkat6871 is already working on this.
Since May 19, 2026.
- Dominant language
- C++
- Stars
- 200k
- Forks
- 76.9k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 433
Description
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10 x64
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: NA
- TensorFlow installed from (source or binary): Binary
- TensorFlow version (use command below): 2.2.0
- Python version: 3.7.6
- Bazel version (if compiling from source): NA
- GCC/Compiler version (if compiling from source): NA
- CUDA/cuDNN version: NA
- GPU model and memory: NA
Describe the current behavior
Note: I am reporting this as a bug but I am not sure if it may actually be a feature request, as I am not entirely sure if the described behaviour is fully expected or not.
tf.ragged.constant does not properly detect which dimensions should be ragged from the given Python list. By default, only the outermost dimension is considered as dense, even if other coherent dimensions exist in the data. One can use ragged_rank and/or inner_shape to mark a number of innermost dimensions as dense, but it does not seem to be possible to do the opposite, that is, marking some outermost dimensions after the first one as dense. And, in general, it does not detect nor allow to make a ragged tensor with an arbitrary combination of ragged and dense dimensions (even though it is possible to build such ragged tensors in other ways).
Describe the expected behavior
I would expect that all coherent dimensions of a Python nested list are detected as dense dimensions:
import tensorflow as tf
print(tf.ragged.constant([[[1], [2, 3], [4]], [[5, 6], [], [7]]]).shape)
# (2, 3, None)
As a feature addition, having the possibility to specify which arbitrary dimensions are ragged or not would also be nice, although it would have to be with a different API. Maybe I could have for example:
import tensorflow as tf
tf.ragged.constant([[[1], [2, 3], [4]], [[5, 6], [], [7]]], shape=[2, -1, None])
With -1 meaning "detect automatically from data" and None meaning ragged dimension.
Standalone code to reproduce the issue
import tensorflow as tf
# Dense inner dimensions are not detected
print(tf.ragged.constant([[[1], [2, 3], [4]], [[5, 6], [], [7]]]).shape)
# (2, None, None)
# The outermost dimension is always dense
print(tf.ragged.constant([[1], [2, 3], [4]]).shape)
# (3, None)
# But simply adding a couple of brackets makes the dimension ragged
print(tf.ragged.constant([[[1], [2, 3], [4]]]).shape)
# (1, None, None)
Other info / logs
NA
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.