Sortng issue with from_nested_value_rowids

When I use “tf.RaggedTensor.from_value_rowids”,
args “value_rowids” must be sorted in ascending order as written in the API description.

Then, how can I make a tensor sorted?

My concern is “values” must be sorted as the same index in which the “value_rowids” was sorted order.

ex) tensor [None, 390144, 2]
axis 1st “None” → batch size
axis 2nd “390144” → the number of data
axis 3rd “2” → [value_rowids, values]

You are correct in that (1) the value_rowids need to be in ascending order and (2) the value_rowids must correspond one-to-one with values when using RaggedTensor.

To sort a tensor with multiple dimensions like the one described, you can take advantage of argsort() and gather() to determine the sorted indices along a given axis and apply the new order, respectively.

Below, I illustrate how to do this with a randomly generated tensor:

# Create example tensor
batch_size = 3  # example value
data_size = 10  # 390144
value_size = 2
example_shape = [batch_size, data_size, value_size]
example_tensor = tf.random.uniform(example_shape)

# Determine indices with ascending order
sorted_idx = tf.argsort(example_tensor, axis=1, direction="ASCENDING")
sorted_idx = sorted_idx[:, :, 0]  # Only check value_rowidx

# Order tensor by sorted indices
sorted_tensor = tf.gather(example_tensor, sorted_idx, batch_dims=1, axis=1)

The resulting sorted_tensor should be sorted by the axis with value_rowids if I understood the shape of the tensor you are starting with.

1 Like