3

Suppose you have stacked two sequences of 3-dimensional embeddings into a single ragged tensor:

import tensorflow as tf

def foo(*args):
    n_elements = tf.reduce_prod(args)
    return tf.range(n_elements, dtype=tf.float32).reshape(args)

c = tf.ragged.stack((foo(2, 3), foo(5, 3)), axis=0)
assert c.shape == [2, None, None]

How to cast c to shape [2, None, 3] (because you know this tensor is of this shape)?

AloneTogether
  • 25,814
  • 5
  • 20
  • 39
Mike Land
  • 436
  • 4
  • 15
  • Did you try using `tf.RaggedTensor.from_row_splits`? – AloneTogether Jun 08 '22 at 07:12
  • 1
    At some point I figured out that `RaggedTensors` are a completely unusable and unsupported feature of Tensorflow in their present state and dropped using them altogether. But thanks for your input. `from_row_lengths` also works fine, btw – Mike Land Jun 08 '22 at 13:38
  • Hmm that is unfortunate..they can be sometimes tricky but are really useful actually. – AloneTogether Jun 08 '22 at 13:48

1 Answers1

1

Try using tf.RaggedTensor.from_row_splits:

tf.RaggedTensor.from_row_splits(
    values=c.merge_dims(0, 1).to_tensor(),
    row_splits=[0, 3, 7]).shape
(2, None, 3)
AloneTogether
  • 25,814
  • 5
  • 20
  • 39