Tensorflow Keras doesn't work with ragged tensor input

Viewed 2919

I'm using Tensorflow 2.3.

If I use a normal tf tensor input, the example below works fine:

import tensorflow as tf
text_input = tf.keras.Input([None], dtype=tf.string, name="text_input", ragged=False)
predictions = tf.gather(text_input, 0, axis=-1)
model = tf.keras.Model(inputs=[text_input], outputs=[predictions])
model(tf.constant([['A1', 'A2', 'A3'], ['B1', 'B2', 'B3']]))

<tf.Tensor: shape=(2,), dtype=string, numpy=array([b'A1', b'B1'], dtype=object)>

However, if I change the input to a ragged tensor, I got an error while trying to create the model.

import tensorflow as tf
ragged_input = tf.keras.Input([None], dtype=tf.string, name="ragged_input", ragged=True)
padded_input = ragged_input.to_tensor('')
predictions = tf.gather(padded_input, 0, axis=-1)
model = tf.keras.Model(inputs=[ragged_input], outputs=[predictions])

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-201-9adaf4aae2b5> in <module>()
      3 padded_input = ragged_input.to_tensor('')
      4 predictions = tf.gather(padded_input, 0, axis=-1)
----> 5 model = tf.keras.Model(inputs=[ragged_input], outputs=[predictions])

13 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  You must feed a value for placeholder tensor 'Placeholder_38' with dtype int64 and shape [?]
     [[node Placeholder_38 (defined at <ipython-input-201-9adaf4aae2b5>:5) ]] [Op:__inference_keras_scratch_graph_136790]

Function call stack:
keras_scratch_graph
2 Answers

Looks like a bug to me because the RaggedTensor support for Keras isn't the best (see e.g. here). I'm not exactly sure what is causing it but the ragged conversion is failing for the placeholders.

If you can, it's probably best to use all RaggedTensor functionality before passing it as an input and setting ragged=False. This is not an issue if you only want to use it for convenient padding and if all graph operations are based on non-ragged tensors (which is the case for your example):

import tensorflow as tf
ragged_input = tf.keras.Input([None], dtype=tf.string, name="ragged_input", ragged=False)
# padded_input = ragged_input.to_tensor('')
predictions = tf.gather(ragged_input, 0, axis=-1)

model = tf.keras.Model(inputs=[ragged_input], outputs=[predictions])
padded_input = tf.ragged.constant([['A1', 'A2'], ['B1', 'B2', 'B3']]).to_tensor('')
result = model(padded_input)
print(result)
# >>> tf.Tensor([b'A1' b'B1'], shape=(2,), dtype=string)

You can try creating a tf.keras.Sequential model if you have single inputs and outputs.

Related