tensorflow / tensorflow/datasets
Composite tensor support
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.6k
- Forks
- 1.6k
- Avg merge
- 3h 54m
- Merged PRs (30d)
- 1
Description
Is your feature request related to a problem? Please describe.
Composite tensors (e.g. RaggedTensor, SparseTensor) are seeing increased usage in tf applications, but it is not clear how these should be implemented in tfds. Critically, TensorInfo does not seem to support tf.SparseTensors, and it is not clear how sequence_rank can be related to ragged_rank for encoding information related to tf.RaggedTensor.
Describe the solution you'd like
features.RaggedTensor / features.SparseTensor similar to features.Tensor to wrap tf.RaggedTensor and tf.SparseTensor respectively.
This may be aided by changing TensorInfo to be a simple wrapper around tensor_spec and default_value (and maybe sequence_rank?). This is arguably a separate issue, but there seems to be a lot of redundancy here, and trying to understand duplicate APIs is annoying.
Describe alternatives you've considered
The below implements RaggedTensor, but not being familiar with Sequence implementation I doubt it would work nicely there...
class RaggedTensor(tfds.core.features.FeatureConnector):
def __init__(
self,
flat_shape,
dtype: tf.DType = tf.float32,
ragged_rank: int = 1,
row_splits_dtype: tf.DType = tf.int64,
):
self._ragged_rank = ragged_rank
self._values_dtype = dtype
self._row_splits_dtype = row_splits_dtype
self._flat_shape = tuple(flat_shape)
rs = tfds.core.features.Tensor(shape=(None,), dtype=row_splits_dtype)
self._base = tfds.core.features.FeaturesDict(
{
"flat_values": tfds.core.features.Tensor(
shape=self._flat_shape, dtype=dtype
),
**{f"nested_row_splits_{i}": rs for i in range(ragged_rank)},
}
)
def encode_example(self, example: tf.RaggedTensor):
if example.ragged_rank != self._ragged_rank:
raise ValueError(
f"Expected ragged_rank {self._ragged_rank} but example has "
f"{example.ragged_rank}"
)
components = {
f"nested_row_splits_{i}": rs
for i, rs in enumerate(example.nested_row_splits)
}
components["flat_values"] = example.flat_values
return self._base.encode_example(components)
def decode_example(self, tfexample_data):
components_dict = self._base.decode_example(tfexample_data)
return tf.RaggedTensor.from_nested_row_splits(
components_dict["flat_values"],
[
components_dict[f"nested_row_splits_{i}"]
for i in range(self._ragged_rank)
],
)
def get_serialized_info(self):
return self._base.get_serialized_info()
def get_tensor_info(self):
return tfds.core.features.TensorInfo(
shape=self._flat_shape,
dtype=self._values_dtype,
sequence_rank=self._ragged_rank,
)
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.
Research direction
Start by reading the existing TensorInfo and Sequence implementations, then compare them with the proposed FeatureConnector-based RaggedTensor design. Determine how RaggedTensor and SparseTensor should be represented, encoded, decoded, and exposed through TensorInfo; done requires a settled design that supports both composite types without unresolved API duplication.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, tensorflow
- Domain
- data
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100