#Reshape to ragged tensor shape

1 messages · Page 1 of 1 (latest)

ripe swallow
#

Hello, I have two models, one outputs a ragged tensor:

[[
  [1, 2, 1],
 ],
 [
  [2, 0, 0],
  [1, 1, 0]
]]

while the second model outputs a tensor:

[
  [5, 4],
  [6, 7]
]

I need to "expand" the second models ouput to concatenate it with the first model. Basically I want to add a dimension and copy the result as many times as there are in the ragged tensor, like:

[[
  [5, 4],
 ],
 [
  [6, 7],
  [6, 7]
]]

Does anyone know how to do this? This example would have a batch size of 2.

steep heart
#

how should the output look like?

ripe swallow
#

Like the last code block. But I think I got something that work for my model architecture with

class RaggedExpansionLayer(keras.layers.Layer):
    def __init__(self, **kwargs):
        super(RaggedExpansionLayer, self).__init__(**kwargs)

    def call(self, inputs):
        ragged_tensor, dense_tensor = inputs
        row_lengths = ragged_tensor.row_lengths()
        batch_indices = tf.range(tf.shape(dense_tensor)[0])
        repeated_indices = tf.repeat(batch_indices, row_lengths)
        gathered = tf.gather(dense_tensor, repeated_indices)
        ragged = tf.RaggedTensor.from_row_lengths(gathered, row_lengths)

        return tf.concat([ragged_tensor, ragged], axis=-1)
#

It doesn't solve my issue:

    AttributeError: 'SymbolicTensor' object has no attribute 'row_lengths'


Call arguments received by layer "ragged_expansion_layer" (type RaggedExpansionLayer):
  • inputs=['tf.Tensor(shape=(None, None, 100), dtype=float32)', 'tf.Tensor(shape=(None, 4000), dtype=float32)']
steep heart
#

I mean how does the concatenated output of your example tensors look like?

#

the code constructs a graph rather than eagerly evaluate things, that's why it's a symbolic tensor and length can't find computed

ripe swallow
#

The output should look like:

[[
  [1, 2, 1, 5, 4],
 ],
 [
  [2, 0, 0, 6, 7],
  [1, 1, 0, 6, 7]
]]
ripe swallow
#

I am currently working on it. I will come back to you if cant figure it out at all and then provide a minimal working example. Dont want to waste your time 🙂