简体   繁体   中英

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?

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]]>

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]]>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM