Constructing Ragged Tensors

Here, we will learn how to create a ragged tensor with Tensorflow.

1. Using tf.ragged.constant()

We use tf.ragged.constant() to create a ragged tensor from nested Python lists. Each nested list represents a sequence of varying length. The resulting ragged tensor accommodates these variable-length sequences.

Python
import tensorflow as tf
# Creating a ragged tensor from a nested Python list
ragged_tensor = tf.ragged.constant([[1, 2], [3, 4, 5], [6]])
print(ragged_tensor)

Output:

<tf.RaggedTensor [[1, 2], [3, 4, 5], [6]]>

RaggedTensors are multi-dimensional tensors that can have rows of different lengths. There are multiple ways to put them together.

Pairing flat values (holding all values in a flattened list) with a row-partitioning tensor that indicates how to divide those values into rows using factory classmethods like tf.RaggedTensor.from_value_rowidstf.RaggedTensor.from_row_lengths, and tf.RaggedTensor.from_row_splits.

2. Using tf.RaggedTensor.from_value_rowids

  1. Values Tensor: Create a flat tensor containing all the words.
  2. Value Rows Tensor: This tensor tells us which row (sentence) each word belongs to. It should have the same length as the values tensor. Here, 0 represents the first sentence, 2 represents the second sentence, and 3 represents the third sentence (since it’s a single word).
Python3
import tensorflow as tf
words = [3, 1, 4, 1, 5, 9, 2]
row_ids = [0, 0, 0, 0, 2, 2, 3]

ragged_tensor = tf.RaggedTensor.from_value_rowids(values=words, value_rowids=row_ids)
print(ragged_tensor)

Output:

<tf.RaggedTensor [[3, 1, 4, 1], [], [5, 9], [2]]>

3. Using tf.RaggedTensor.from_row_lengths

row_lengths: This is constant tensor containing information about the number of elements in each row of the desired RaggedTensor. Below, [2, 2, 2, 2] specifies that we want four rows, each with two elements.

Python3
import tensorflow as tf
# Convert from row lengths
values = tf.constant([1, 2, 3, 0, 4, 0, 5, 6])
row_lengths = tf.constant([2, 2, 2, 2])
from_row_lengths_ragged = tf.RaggedTensor.from_row_lengths(values, row_lengths)

print(from_row_lengths_ragged)

Output:

<tf.RaggedTensor [[1, 2],
[3, 0],
[4, 0],
[5, 6]]>

4. Using tf.RaggedTensor.from_row_splits

row_splits: This is a constant tensor, but instead of lengths, it contains information about where each row starts in the values tensor. Below, [0, 2, 4, 6, 8] indicates the starting index for each row: 0 (first element), 2 (third element), 4 (fifth element), and 6 (seventh element).

Python3
import tensorflow as tf
# Convert from row splits
values = tf.constant([1, 2, 3, 0, 4, 0, 5, 6])
row_splits = tf.constant([0, 2, 4, 6, 8])
from_row_splits_ragged = tf.RaggedTensor.from_row_splits(values, row_splits)
print(from_row_splits_ragged)

Output:

<tf.RaggedTensor [[1, 2],
[3, 0],
[4, 0],
[5, 6]]>

Ragged tensors in TensorFlow

Ragged tensors are a fundamental data structure in TensorFlow, especially in scenarios where data doesn’t conform to fixed shapes, such as sequences of varying lengths or nested structures. In this article, we’ll understand what ragged tensors are, why they’re useful, and provide hands-on coding examples to illustrate their usage.

Table of Content

  • What are Ragged Tensors?
  • Why Use Ragged Tensors?
  • Constructing Ragged Tensors
  • Operations on Ragged Tensors
  • Passing Ragged Tensors for Training

Similar Reads

What are Ragged Tensors?

In TensorFlow, tensors are the basic building blocks for data representation. A tensor is essentially a multi-dimensional array, where each dimension represents a different mode of indexing. Ragged tensors, however, deviate from this notion by allowing for variable lengths along certain dimensions....

Why Use Ragged Tensors?

Ragged tensors offer immense utility across diverse scenarios where data exhibits irregularities and fails to adhere to fixed shapes. Their versatility becomes apparent in various domains:...

Constructing Ragged Tensors

Here, we will learn how to create a ragged tensor with Tensorflow....

Operations on Ragged Tensors

Ragged tensors support various standard operations similar to regular tensors in TensorFlow:...

Passing Ragged Tensors for Training

Keras Makes Ragged Tensors Easy for Training with:...