1

I have several RaggedTensors that I want to concatenate; I am using Keras. Vanilla Tensorflow will happily concatenate them, so I tried the code:

card_feature = layers.concatenate([ragged1, ragged2, ragged3])

but it gave the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 925, in __call__
    return self._functional_construction_call(inputs, args, kwargs,
  File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1084, in _functional_construction_call
    base_layer_utils.create_keras_history(inputs)
  File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 191, in create_keras_history
    _, created_layers = _create_keras_history_helper(tensors, set(), [])
  File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 222, in _create_keras_history_helper
    raise ValueError('Tensorflow ops that generate ragged or sparse tensor '
ValueError: Tensorflow ops that generate ragged or sparse tensor outputs are currently not supported by Keras automatic op wrapping. Please wrap these ops in a Lambda layer: 

```

      weights_mult = lambda x: tf.sparse.sparse_dense_matmul(x, weights)
      output = tf.keras.layers.Lambda(weights_mult)(input)
      
```

so then I tried:

concat_lambda = lambda xs: tf.concat(xs, axis=2)
card_feature = layers.Lambda(concat_lambda)([ragged1, ragged2, ragged3])

but it gave the exact same error, even though I had wrapped it. Is this a bug / is there a workaround?

Alex Meiburg
  • 655
  • 5
  • 20

1 Answers1

0

Code to concatenate 3 Ragged Tensors is shown below:

import tensorflow as tf

print(tf.__version__)

Ragged_Tensor1 = tf.ragged.constant([[3, 1, 4, 1], [], [5, 9, 2], [6], []])
Ragged_Tensor2 = tf.ragged.constant([[5, 3]])
Ragged_Tensor3 = tf.ragged.constant([[6,7,8], [9,10]])
print(tf.concat([Ragged_Tensor1, Ragged_Tensor2, Ragged_Tensor3], axis=0))

Output is shown below:

2.3.0
<tf.RaggedTensor [[3, 1, 4, 1], [], [5, 9, 2], [6], [], [5, 3], [6, 7, 8], [9, 10]]>

But it looks like you are trying to concatenate Ragged Tensor Operations. Please share your complete code so that we can try to help you.