Append elements to Ragged tensors?

745 Views Asked by At

Can I append elements or extend in some way Ragged Tensors as I can do nested lists ?

In [158]: l=[[1,2],[3],[4,5,6]]                                                                                                                                              

In [159]: l                                                                                                                                                                  
Out[159]: [[1, 2], [3], [4, 5, 6]]

In [160]: l[1]                                                                                                                                                               
Out[160]: [3]

In [161]: l[1].append(9)                                                                                                                                                     

In [162]: l                                                                                                                                                                  
Out[162]: [[1, 2], [3, 9], [4, 5, 6]]

Or for that matter can any Tensor be dynamically resized (not reshaped)

1

There are 1 best solutions below

0
Lescurel On

RaggedTensors are not dynamic. However, you can use tf.concat with another RaggedTensor containing your update to create a new RaggedTensor:

>>> rt = tf.ragged.constant([[1,2],[3],[4,5,6]])
>>> update = tf.ragged.constant([[],[9],[]])
>>> tf.concat([rt,update],axis=1)
<tf.RaggedTensor [[1, 2], [3, 9], [4, 5, 6]]>