Isaac Breen
Isaac Breen

Reputation: 160

Creating a ragged tensor from a list of tensors

I want to create a ragged tensor from a list of tensors in TensorFlow 2.0, something like this:

a = tf.convert_to_tensor([1,2])
b = tf.convert_to_tensor([1,2,3])
tf.ragged.constant([a, b])

But this throws ValueError: TypeError: Scalar tensor has no `len()`. On the other hand, the following code, which creates a ragged tensor from a list of lists, works just fine.

a = [1,2]
b = [1,2,3]
tf.ragged.constant([a,b])

Is there any way to create a ragged tensor directly from a list of tensors without first converting the tensors into python lists?

Upvotes: 9

Views: 5421

Answers (2)

Edward Loper
Edward Loper

Reputation: 15944

You can use tf.ragged.stack:

>>> a = tf.convert_to_tensor([1,2])
>>> b = tf.convert_to_tensor([1,2,3])
>>> tf.ragged.stack([a, b])
<tf.RaggedTensor [[1, 2], [1, 2, 3]]>

Upvotes: 12

javidcf
javidcf

Reputation: 59681

You can construct ragged tensors with the different from_* methods in tf.RaggedTensor. For your case, you can use for example from_row_lengths:

import tensorflow as tf

def stack_ragged(tensors):
    values = tf.concat(tensors, axis=0)
    lens = tf.stack([tf.shape(t, out_type=tf.int64)[0] for t in tensors])
    return tf.RaggedTensor.from_row_lengths(values, lens)

a = tf.convert_to_tensor([1, 2])
b = tf.convert_to_tensor([1, 2, 3])
print(stack_ragged([a, b]))
# <tf.RaggedTensor [[1, 2], [1, 2, 3]]>

Upvotes: 4

Related Questions